Python fails on math

Grant Edwards invalid at invalid.invalid
Tue Feb 22 12:27:56 EST 2011

On 2011-02-22, christian schulze <xcr4cx at> wrote:
> Hey guys,
> I just found out, how much Python fails on simple math.

Python doesn't do math.

It does floating point operations.

They're different.  Seriously.

On all of the platforms I know of, it's IEEE 754 (base-2) floating

> 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]
> So WTF?

Python doesn't do equations.  Python does floating point operations.

[And it does them in _base_2_ -- which is important, because it makes
things even more difficult.]

> The equation is definitive equivalent. (See

But, the two floating point expressions you provided are not

Remember, you're not doing math with Python.

You're doing binary floating point operations.

> #1:
>>>> 2.0 * e * sqrt(3.0) - 2.0 * e
> 3.9798408154464964
> #2:
>>>> 2.0 * e * (sqrt(3.0) -1.0)
> 3.979840815446496
> I was wondering what exactly is failing here. The math module?
> Python, or the IEEE specifications?

I'm afraid it's the user that's failing. Unfortunately, in many
situations using floating point is neither intuitive nor easy to get

Grant Edwards               grant.b.edwards        Yow! I like your SNOOPY
                                  at               POSTER!!

More information about the Python-list mailing list