Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain this: 0.2 + 0.1 = 0.30000000000000004? [duplicate]

Duplicates:
How is floating point stored? When does it matter?

Is floating point math broken?

Why does the following occur in the Python Interpreter?

>>> 0.1+0.1+0.1-0.3 5.551115123125783e-17 >>> 0.1+0.1 0.2 >>> 0.2+0.1 0.30000000000000004 >>> 0.3-0.3 0.0 >>> 0.2+0.1 0.30000000000000004 >>>  

Why doesn't 0.2 + 0.1 = 0.3?

like image 860
Musaab Avatar asked Sep 25 '11 10:09

Musaab


People also ask

Why is 0.1 0.2 == 0.3 False and how can you ensure precise decimal arithmetic?

Note that the mantissa is composed of recurring digits of 0011 . This is key to why there is any error to the calculations - 0.1, 0.2 and 0.3 cannot be represented in binary precisely in a finite number of binary bits any more than 1/9, 1/3 or 1/7 can be represented precisely in decimal digits.

Why 0. 1 0. 2 is not 0. 3 JavaScript?

With decimal fractions, this floating-point number system causes some rounding errors in JavaScript. For example, 0.1 and 0.2 cannot be represented precisely. Hence, 0.1 + 0.2 === 0.3 yields false. To really understand why 0.1 cannot be represented properly as a 32-bit floating-point number, you must understand binary.


2 Answers

That's because .1 cannot be represented exactly in a binary floating point representation. If you try

>>> .1 

Python will respond with .1 because it only prints up to a certain precision, but there's already a small round-off error. The same happens with .3, but when you issue

>>> .2 + .1 0.30000000000000004 

then the round-off errors in .2 and .1 accumulate. Also note:

>>> .2 + .1 == .3 False 
like image 164
Fred Foo Avatar answered Sep 20 '22 03:09

Fred Foo


Not all floating point numbers are exactly representable on a finite machine. Neither 0.1 nor 0.2 are exactly representable in binary floating point. And nor is 0.3.

A number is exactly representable if it is of the form a/b where a and b are an integers and b is a power of 2. Obviously, the data type needs to have a large enough significand to store the number also.

I recommend Rob Kennedy's useful webpage as a nice tool to explore representability.

like image 30
David Heffernan Avatar answered Sep 20 '22 03:09

David Heffernan