Float128 emulation in Javascript, fastest way how to make 0.1 + 0.2 == 0.3
1–8 of 8 posts
Re: Float128 emulation in Javascript, fastest way how to make 0.1 + 0.2 == 0.3
#2Re: Float128 emulation in Javascript, fastest way how to make 0.1 + 0.2 == 0.3
#3The 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.
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.
Re: Float128 emulation in Javascript, fastest way how to make 0.1 + 0.2 == 0.3
#4The first example
new Double('0.3').sub(new Double('0.1')).toNumber()
does the equivalent of float f = (float)(0.3 - 0.1);
which happens to produce a ‘float32’ that prints as “0.2”, but for ‘float64’ vs ‘float128’.I expect that adding a sufficient number of zeroes, as in
double f = (float128)0.00003 - (float128)0.00001);
(actual number of zeroes is too low here, to prevent wrapping on phone screens) will surface the problem again.Re: Float128 emulation in Javascript, fastest way how to make 0.1 + 0.2 == 0.3
#5This doesn’t solve the general problem that binary floating point cannot exactly represent decimal fractions. The first example new Double('0.3').sub(new Double('0.1')).toNumber() does the equivalent of float f = (float)(0.3 - 0.1); which happens to produce a ‘float32’ that prints as “0.2”, but for ‘float64’ vs ‘float128’. I expect that adding a sufficient number of zeroes, as in double f = (float128)0.00003 - (float…
Re: Float128 emulation in Javascript, fastest way how to make 0.1 + 0.2 == 0.3
#6The 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.
Re: Float128 emulation in Javascript, fastest way how to make 0.1 + 0.2 == 0.3
#7The 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.
Re: Float128 emulation in Javascript, fastest way how to make 0.1 + 0.2 == 0.3
#8Earlier quoted context omitted.
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).