[issue1507] complex constructor loses signs of zeros

Mark Dickinson report at bugs.python.org
Tue Nov 27 23:25:12 CET 2007


New submission from Mark Dickinson:

In Python2.5 and the current trunk, construction of a complex number from two floats 
loses the negative sign of a negative zero in the imaginary part.

>>> complex(-0., 0.).real  # behaves as expected
-0.0
>>> complex(0., -0.).imag  # loses sign of zero
0.0

There are situations where it's important to preserve the sign of zero (typically 
when doing calculations involving functions with branch cuts);  there are probably 
platforms where this is difficult for one reason or another, but on a platform that 
complies with all the usual standards I can't see any reason for Python not to 
preserve the signs of zeros, provided that it's straightforward to do so.

The attached patch changes the complex constructor so that if x and y are numeric 
and neither x nor y is complex (or a subclass of complex) then complex(x, y) simply 
places x into the real part of the complex number and y into the imaginary part.  
For someone who doesn't care about the sign of zeros, this patch will have no 
noticeable effect.  Similarly, on a system that does not have full hardware or 
system support for signed zeros, this patch should be harmless.  Note that the patch 
does *not* change the feature that complex(z1, z2) returns z1 + 1j*z2 when z1 and/or 
z2 is itself complex.

There was some previous discussion of this on python-dev earlier this year.  See

http://mail.python.org/pipermail/python-dev/2007-January/070770.html

----------
files: complex_new.patch
messages: 57891
nosy: marketdickinson
severity: normal
status: open
title: complex constructor loses signs of zeros
versions: Python 2.5, Python 2.6
Added file: http://bugs.python.org/file8815/complex_new.patch

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue1507>
__________________________________
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: complex_new.patch
Url: http://mail.python.org/pipermail/python-bugs-list/attachments/20071127/e3e69a97/attachment-0001.txt 


More information about the Python-bugs-list mailing list