Strange result with math.atan2()

Tim Peters tim.peters at gmail.com
Tue May 2 21:27:43 EDT 2006


[Vedran Furač]
> I think that this results must be the same:
>
> In [3]: math.atan2(-0.0,-1)
> Out[3]: -3.1415926535897931

Whether -0.0 and 0.0 are different floats internally depends on your
hardware floating-point; on most machines today, they are different
floats, but _compare_ equal to each other.

> In [4]: math.atan2(-0,-1)
> Out[4]: 3.1415926535897931

"0" and "1" are integer literals, and there is no current hardware on
which -0 produces a different bit pattern internally than 0.  Of
course you can _want_ them to be different, but you'll hope in vain
;-)

> In [5]: -0 == -0.0
> Out[5]: True

You'll also find that -0.0 == +0.0 returns True, despite that atan2
may treat them differently.  The IEEE 754 standard for floating-point
arithmetic requires that they compare equal, BTW.

> This is python 2.4.4c0 on Debian GNU/Linux.

Doesn't much matter.  The hardware may matter, though, and so may the
C library implementing atan2, and so may the set of option flags
passed to the platform C compiler when Python is compiled.  For
example, DEC Alpha hardware has little native support for signed
zeroes, and compilers for that box accept the HW limitations by
default.

Bottom line:  if you want atan2 to treat signed zeroes differently,
never use integer arguments (or results computed from integers), and
even then don't expect it to be portable.


More information about the Python-list mailing list