Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

1.265 * 10000 = 126499.99999999999? [duplicate]

When I multiply 1.265 by 10000 , I get 126499.99999999999 when using Javascript.

Why is this so?

like image 366
vladaruz Avatar asked Jun 08 '09 08:06

vladaruz


2 Answers

Floating point numbers can't handle decimals correctly in all cases. Check out

  • http://en.wikipedia.org/wiki/Floating-point_number#Accuracy_problems
  • http://www.mredkj.com/javascript/nfbasic2.html
like image 177
sunny256 Avatar answered Nov 11 '22 22:11

sunny256


You should be aware that all information in computers is in binary and the expansions of fractions in different bases vary.

For instance 1/3 in base 10= .33333333333333333333333333, while 1/3 in base 3 is equal to .1 and in base 2 is equal to .0101010101010101.

In case you don't have a complete understanding of how different bases work, here's an example:

The base 4 number 301.12. would be equal to 3 * 4^2 + 0 * 4^1 + 1 * 4^0 + 1 * 4^-1 + 2 *4^-2= 3 * 4^2 +1+ 1 * 4^-1 + 2 * 4^-2=49.375 in base 10.

Now the problems with accuracy in floating point comes from a limited number of bits in the significand. Floating point numbers have 3 parts to them, a sign bit, exponent and mantissa, most likely javascript uses 32 or 64 bit IEEE 754 floating point standard. For simpler calculations we'll use 32 bit, so 1.265 in floating point would be

Sign bit of 0 (0 for positive , 1 for negative) exponent of 0 (which with a 127 offset would be, ie exponent+offset, so 127 in unsigned binary) 01111111 (then finally we have the signifcand of 1.265, ieee floating point standard makes use of a hidden 1 representation so our binary represetnation of 1.265 is 1.01000011110101110000101, ignoring the 1:) 01000011110101110000101.

So our final IEEE 754 single (32-bit) representation of 1.625 is:

Sign Bit(+)      Exponent (0)       Mantissa (1.625)
0                 01111111          01000011110101110000101

Now 1000 would be:

Sign Bit (+) Exponent(9) Mantissa(1000) 0 10001000 11110100000000000000000

Now we have to multiply these two numbers. Floating point multiplication consists of re-adding the hidden 1 to both mantissas, multiplying the two mantissa, subtracting the offset from the two exponents and then adding th two exponents together. After this the mantissa has to be normalized again.

First 1.01000011110101110000101*1.11110100000000000000000=10.0111100001111111111111111000100000000000000000 (this multiplication is a pain)

Now obviously we have an exponent of 9 + an exponent of 0 so we keep 10001000 as our exponent, and our sign bit remains, so all that is left is normalization.

We need our mantissa to be of the form 1.000000, so we have to shift it right once which also means we have to increment our exponent bringing us up to 10001001, now that our mantissa is normalized to 1.00111100001111111111111111000100000000000000000. It must be truncated to 23 bits so we are left with 1.00111100001111111111111 (not including the 1, because it will be hidden in our final representation) so our final answer that we are left with is

Sign Bit (+)   Exponent(10)   Mantissa
0              10001001       00111100001111111111111

Finally if we conver this answer back to decimal we get (+) 2^10 * (1+ 2^-3 + 2^-4 +2^-5+2^-6+2^-11+2^-12+2^-13+2^-14+2^-15+2^-16+2^-17+2^-18+2^-19+2^-20+2^-21+2^-22+2^-23)=1264.99987792

While I did simplify the problem multiplying 1000 by 1.265 instead of 10000 and using single floating point, instead of double, the concept stays the same. You use lose accuracy because the floating point representation only has so many bits in the mantissa with which to represent any given number.

Hope this helps.

like image 42
JSchlather Avatar answered Nov 11 '22 22:11

JSchlather