Python fails on math
hobson42 at gmail.com
Tue Feb 22 14:48:41 CET 2011
On 22/02/2011 13:20, christian schulze wrote:
> Hey guys,
> I just found out, how much Python fails on simple math. I checked a
> simple equation for a friend.
>>>> from math import e as e
>>>> from math import sqrt as sqrt
>>>> 2*e*sqrt(3) - 2*e == 2*e*(sqrt(3) - 1)
> So WTF? The equation is definitive equivalent. (See http://mathbin.net/59158)
>>>> 2.0 * e * sqrt(3.0) - 2.0 * e
>>>> 2.0 * e * (sqrt(3.0) -1.0)
> I was wondering what exactly is failing here. The math module? Python,
> or the IEEE specifications?
What has failed you is your understanding of what floating point means.
Both sides of your equation contain e which is an irrational number.
No irrational number and many rational ones cannot be expressed exactly
in IEEE format. (1/3, 1/7)
All that has happened is that the two sides have come out with very
slightly different approximations to numbers that they cannot express
More information about the Python-list