Negative integers
Christian Heimes
lists at cheimes.de
Wed Aug 20 18:19:09 EDT 2008
johnewing wrote:
> but that fails when one of the numbers is 0. I'm sure that there is
> an easy way to do this. Any suggestions?
For the not-so-distant future:
Python 2.6 and 3.0 have a new function "copysign" in the math module. I
added it during the revamp of the math module. copysign(x, y) returns x
as float with the sign of y. It works also works for ints, longs and
floats as y. copysign() is the most reliable and fastest way to
distinguish -0.0 from +0.0.
>>> from math import copysign
>>> copysign(1.0, 23)
1.0
>>> copysign(1.0, -42)
-1.0
>>> copysign(1.0, -0.0)
-1.0
>>> copysign(1.0, +0.0)
1.0
>>> -0.0 == +0.0
True
Christian
More information about the Python-list
mailing list