1.265 * 10000 = 126499.99999999999? [duplicate]

2019-01-09 16:21发布

问题:

This question already has an answer here:

  • Is floating point math broken? 28 answers

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

Why is this so?

回答1:

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


回答2:

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.



回答3:

It's a result of floating point representation error. Not all numbers that have finite decimal representation have a finite binary floating point representation.



回答4:

Have a read of this article. Essentially, computers and floating-point numbers do not go together perfectly!



回答5:

On the other hand, 126500 IS equal to 126499.99999999.... :)

Just like 1 is equal to 0.99999999....

Because 1 = 3 * 1/3 = 3 * 0.333333... = 0.99999999....



回答6:

Purely due to the inaccuracies of floating point representation.

You could try using Math.round:

var x = Math.round(1.265 * 10000);


回答7:

You might want to read this.



回答8:

These small errors are usually caused by the precision of the floating points as used by the language. See this wikipedia page for more information about the accuracy problems of floating points.



回答9:

Here's a way to overcome your problem, although arguably not very pretty:

var correct = parseFloat((1.265*10000).toFixed(3));

// Here's a breakdown of the line of code:
var result = (1.265*10000);
var rounded = result.toFixed(3); // Gives a string representation with three decimals
var correct = parseFloat(rounded); // Convert string into a float 
                                   // (doesn't show decimals)


回答10:

If you need a solution, stop using floats or doubles and start using BigDecimal. Check the BigDecimal implementation stz-ida.de/html/oss/js_bigdecimal.html.en



回答11:

Even additions on the MS JScript engine : WScript.Echo(1083.6-1023.6) give 59.9999999



回答12:

Same reason that 1/3 is 0.333333.....