0

I just randomly discovered the following in Node.js

0.3 + 0.2 === 0.5
true

0.1 + 0.2 === 0.3
false

Anyone does have an explanation for what goes on here? The tested version is Node.js v6.9.1 on a 4.4.0-57-generic kernel. Cheers to anyone able to explain this to me!

Fohlen
  • 192
  • 2
  • 18

1 Answers1

1

Floating point values are represented with powers of two. .1 and .2 can't be represented as sums of a finite number of powers of two, and so there are some inaccuracies with the addition of them. Same goes for .3, but as luck would have it the inaccuracies balanced out in the first case. More detailed description here.
http://0.30000000000000004.com/

E.D.
  • 639
  • 6
  • 14