Skip to content Skip to sidebar Skip to footer

Addition Of Floating Point Numbers In Javascript

I've read this Stack Overflow thread, and this blog post, but am still unable to understand why some floating point numbers can be represented exactly in binary, and why some can't

Solution 1:

When converting Number values to strings in JavaScript, the default is to use just enough digits to uniquely distinguish the Number value. This means that when a number is displayed as “0.6”, that does not mean it is exactly 0.6, just that it is closer to 0.6 than any other Number value is, so displaying just “0.6” tells you it is this unique Number value, which is 0.59999999999999997779553950749686919152736663818359375.

When you set Number objects to 0.2 and 0.4, the results are actually 0.200000000000000011102230246251565404236316680908203125 and 0.40000000000000002220446049250313080847263336181640625. When you add these, the result is 0.600000000000000088817841970012523233890533447265625. That is different from 0.59999999999999997779553950749686919152736663818359375, and it is farther from 0.6, so JavaScript displays it as “0.6000000000000001” to show that it is different from the number it displays as “0.6”.

When you set Number objects to 0.1 and 0.5, the results are 0.1000000000000000055511151231257827021181583404541015625 and 0.5. When you add these, the result is 0.59999999999999997779553950749686919152736663818359375, which is that number that JavaScript displays as “0.6”.

Post a Comment for "Addition Of Floating Point Numbers In Javascript"