Python fails on math

Duncan Booth duncan.booth at invalid.invalid
Tue Feb 22 15:25:34 CET 2011


christian schulze <xcr4cx at googlemail.com> wrote:

> Hey guys,
> 
> I just found out, how much Python fails on simple math. I checked a
> simple equation for a friend.
> 
> [code]
>>>> from math import e as e
>>>> from math import sqrt as sqrt
>>>> 2*e*sqrt(3) - 2*e == 2*e*(sqrt(3) - 1)
> False
> [/code]
> 
Try the same in other languages and you'll get the same result. Here's 
Javascript (checked on Chrome and Firefox, IE crashed):

> 2*Math.E*Math.sqrt(3)-2*Math.E==2*Math.E*(Math.sqrt(3)-1)
false

See the Python FAQ: 
http://docs.python.org/faq/design.html#why-are-floating-point-calculations-so-inaccurate


-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list