Negative integers

Christian Heimes lists at
Thu Aug 21 00:19:09 CEST 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)
 >>> copysign(1.0, -42)
 >>> copysign(1.0, -0.0)
 >>> copysign(1.0, +0.0)
 >>> -0.0 == +0.0


More information about the Python-list mailing list