0

Possible Duplicate:
Is JavaScript's Math broken?

...and what can I do against that? After the calculation I want to have a String representation of the result. Because of that the zeros (plus the one) are desturburbing. "toFixed()" is not a perfect solution because I want to have all (correct) decimals of the potential result and I do not know the result (and the number of decimals) before. So the calculation shown above is only an example.

Community
  • 1
  • 1
heinob
  • 19,127
  • 5
  • 41
  • 61

2 Answers2

2

The short answer is: computers represent numbers in binary, so they can't represent all base 10 fractions perfectly as JavaScript numbers.

The (slightly) longer answer is that, because JavaScript numbers are 64-bit floating-point (equivalent to the double type in Java, C#, etc.), as Wikipedia describes here, there is a limited number of significand bits. For this reason, the precision of this base-2 number is limited.

As an analogy, consider representing the fraction 1/3 in base 10. Say that you only have so many digits to use. That means that you can never ever ever represent 1/3 exactly in base 10, because 1/3 requires an infinite number of digits to represent in base 10. Similarly, you can never represent 1/10 perfectly in a finite number of bits, because 1/10 requires an infinite number of bits to represent exactly. What you're seeing here is a fraction (58/10) that a computer can't represent exactly in a limited number of bits, so the computer is coming as close as it can.

Adam Mihalcin
  • 14,242
  • 4
  • 36
  • 52
  • 1
    _"so they can't represent all base 10 fractions perfectly"_ - but computers _can_ represent a base 10 decimal number like 5.8 perfectly. It's just that JavaScript uses a number implementation that doesn't necessarily do so so actually 5.7+0.1 will return 5.8, but 5.8*0.1 returns 5.800000000000001. More generally, with an appropriate number implementation computers can represent any non-repeating base 10 decimal number perfectly up to an arbitrary number of significant digits as set by the implementation. – nnnnnn Aug 01 '12 at 06:51
  • 1
    Nice work with the update, but still, the fraction 58/10 or 5.8, _can_ be represented exactly by JavaScript: 58/10 returns 5.8. As compared to 58*0.1, which returns 5.800000000000001. – nnnnnn Aug 01 '12 at 06:59
0

Squeezing infinitely many real numbers into a finite number of bits requires an approximate representation. Although there are infinitely many integers, in most programs the result of integer computations can be stored in 32 bits. In contrast, given any fixed number of bits, most calculations with real numbers will produce quantities that cannot be exactly represented using that many bits. Therefore the result of a floating-point calculation must often be rounded in order to fit back into its finite representation. This rounding error is the characteristic feature of floating-point computation.

Above from http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

jeff
  • 8,300
  • 2
  • 31
  • 43