This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3

Can we link this to the other floating-point error questions?
– SkilldrickJun 8 '09 at 8:25

You might want to calculate with full integers in the first place to avoid this behavior.
– GumboJun 8 '09 at 8:29

1

"Can we link this to..." Sure, if anyone can remember when this was first asked. It gets so many answers so fast every time that I have never felt moved to put in my two cents, so I don't have it...
– dmckeeJun 8 '09 at 13:16

If all you care about is that it PRINTS 12650, regardless of the internal representation, you could try something like this: var myVariable = 1.265 * 10000; document.write (myVariable.toFixed(0));
– Rick ReganJun 8 '09 at 18:21

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.

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.

why was this anonymously downvoted? It's not a direct answer to the question, but it is a mathematical truth, and it partly explains why computers calculate this way.
– Philippe LeybaertJun 8 '09 at 8:33

3

There is no ... in the question, this is not a question about recurring decimal representations being non-unique, but about the accuracy of floating point decimal representations.
– Sam MeldrumJun 8 '09 at 8:34

1

Oh really? Before making such a statement, I would at least do some research on the subject. This proof is 100% mathematically correct
– Philippe LeybaertJun 8 '09 at 8:45

2

Your mathematical statement is correct, activa, but it does not answer the original question.
– Barry BrownJun 8 '09 at 8:53

2

I fully agree with this answer. And it IS an answer, because he was asking 'WHY'. This perfectly explains - why. I was going to post a similar answer, but found that you've already answered it correctly. Thanks!
– ThevsJun 8 '09 at 9:35

My favourite 'short example' for this business is 0.1+0.2-0.3, which generally doesn't come out as zero. .NET gets it wrong; Google gets it right; WolframAlpha gets it half right :)
– AakashMJun 14 '09 at 16:18

Yeah, that's a great example. A partial solution to that is an engine that keeps numerators and denominators separate for as long as possible. So you have {1,10} + {2,10} - {3,10} = {0,10}.
– NosrednaJun 14 '09 at 16:45