Python Forum
Confusing Math - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Confusing Math (/thread-7642.html)



Confusing Math - DrJu - Jan-18-2018

I'm trying to understand why python has issues with simple math.
If you try to subtract 0.92 from 1 (1-.92) the answer you get is 0.07999999999999996 rather than 0.08
Anybody know why?


RE: Confusing Math - Gribouillis - Jan-18-2018

It is because the number cannot be represented exactly in base 2. This page explains the issue. By the way, this is not specific to python. Every system that stores floating numbers according to the ieee-754 standard has the same limitations.


RE: Confusing Math - Windspar - Jan-18-2018

More About Floating Points
If you want to be precise. Then you have to use fractions.Fraction.
from fractions import Fraction
a = Fraction(1)
b = Fraction(92, 100)
print(float(a - b))