Hacker News new | past | comments | ask | show | jobs | submit login

The fastest way are probably rational numbers, as they are natively available in many functional languages or Perl6. That is, 0.1 is represented as the integer tupls (1,10) instead of a floating point number.



How many FLOP do you need to add two rationals? We don't have int type in javascript, only bigInt but it not supported in some browsers and probably not fast enough.

UPD: I added Fraction.js to benchmark https://munrocket.github.io/double.js/docs/benchmark.html but it slower and overflows for some reason, .round(31) not helps.


Adding two rationals boils down to adding 2x two integers. Integer arithmetic is in general less complex then floating point arithmetic. But of course that's not true if your hardware supports single instruction double floating point arithmetic (which any relevant 64bit architecture does).


The problems begins when calculations is long and int64/int32 not enough to store full fraction precision. Lol seems that I find a bug in Fraction.js in rounding https://runkit.com/munrocket/fraction-js-test, but it used in popular Math.js library.


Please note that Perl 6 has been renamed to Raku (https://raku.org using the #rakulang tag on social media). So in Perl `say "same" if .1 + .2 == .3` will not say anything, as that condition is not true. In Raku, `say "same" if .1 + .2 == .3` will display "same".




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: