Skip to content

Commit d57fcbc

Browse files
author
christian.heimes
committed
Merged revisions 59202-59211 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r59203 | guido.van.rossum | 2007-11-27 23:38:36 +0100 (Tue, 27 Nov 2007) | 4 lines Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of the imaginary part (as long as it's not complex). Backport candidate? ........ r59204 | christian.heimes | 2007-11-28 00:16:44 +0100 (Wed, 28 Nov 2007) | 2 lines Expose Py_Py3kWarningFlag as sys.py3kwarning as discussed in #1504 Also added a warning.warnpy3k() as convenient method for Python 3.x related deprecation warnings. ........ r59206 | christian.heimes | 2007-11-28 00:53:14 +0100 (Wed, 28 Nov 2007) | 1 line I forgot to fix one occurence of new in test_descr ........ r59208 | christian.heimes | 2007-11-28 09:02:36 +0100 (Wed, 28 Nov 2007) | 1 line Added py3kwarning to the documentation of the sys module. ........ git-svn-id: http://svn.python.org/projects/python/branches/py3k@59213 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 7a4145f commit d57fcbc

2 files changed

Lines changed: 28 additions & 9 deletions

File tree

Lib/test/test_complex.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from test import test_support
33

44
from random import random
5+
from math import atan2
56

67
# These tests ensure that complex math does the right thing
78

@@ -207,6 +208,18 @@ class complex2(complex): pass
207208
self.assertAlmostEqual(complex(real=17+23j, imag=23), 17+46j)
208209
self.assertAlmostEqual(complex(real=1+2j, imag=3+4j), -3+5j)
209210

211+
# check that the sign of a zero in the real or imaginary part
212+
# is preserved when constructing from two floats. (These checks
213+
# are harmless on systems without support for signed zeros.)
214+
def split_zeros(x):
215+
"""Function that produces different results for 0. and -0."""
216+
return atan2(x, -1.)
217+
218+
self.assertEqual(split_zeros(complex(1., 0.).imag), split_zeros(0.))
219+
self.assertEqual(split_zeros(complex(1., -0.).imag), split_zeros(-0.))
220+
self.assertEqual(split_zeros(complex(0., 1.).real), split_zeros(0.))
221+
self.assertEqual(split_zeros(complex(-0., 1.).real), split_zeros(-0.))
222+
210223
c = 3.14 + 1j
211224
self.assert_(complex(c) is c)
212225
del c

Objects/complexobject.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,8 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
809809
PyNumberMethods *nbr, *nbi = NULL;
810810
Py_complex cr, ci;
811811
int own_r = 0;
812+
int cr_is_complex = 0;
813+
int ci_is_complex = 0;
812814
static PyObject *complexstr;
813815
static char *kwlist[] = {"real", "imag", 0};
814816

@@ -889,6 +891,7 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
889891
retaining its real & imag parts here, and the return
890892
value is (properly) of the builtin complex type. */
891893
cr = ((PyComplexObject*)r)->cval;
894+
cr_is_complex = 1;
892895
if (own_r) {
893896
Py_DECREF(r);
894897
}
@@ -897,7 +900,6 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
897900
/* The "real" part really is entirely real, and contributes
898901
nothing in the imaginary direction.
899902
Just treat it as a double. */
900-
cr.imag = 0.0;
901903
tmp = PyNumber_Float(r);
902904
if (own_r) {
903905
/* r was a newly created complex number, rather
@@ -917,27 +919,31 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
917919
}
918920
if (i == NULL) {
919921
ci.real = 0.0;
920-
ci.imag = 0.0;
921922
}
922-
else if (PyComplex_Check(i))
923+
else if (PyComplex_Check(i)) {
923924
ci = ((PyComplexObject*)i)->cval;
924-
else {
925+
ci_is_complex = 1;
926+
} else {
925927
/* The "imag" part really is entirely imaginary, and
926928
contributes nothing in the real direction.
927929
Just treat it as a double. */
928-
ci.imag = 0.0;
929930
tmp = (*nbi->nb_float)(i);
930931
if (tmp == NULL)
931932
return NULL;
932933
ci.real = PyFloat_AsDouble(tmp);
933934
Py_DECREF(tmp);
934935
}
935936
/* If the input was in canonical form, then the "real" and "imag"
936-
parts are real numbers, so that ci.real and cr.imag are zero.
937+
parts are real numbers, so that ci.imag and cr.imag are zero.
937938
We need this correction in case they were not real numbers. */
938-
cr.real -= ci.imag;
939-
cr.imag += ci.real;
940-
return complex_subtype_from_c_complex(type, cr);
939+
940+
if (ci_is_complex) {
941+
cr.real -= ci.imag;
942+
}
943+
if (cr_is_complex) {
944+
ci.real += cr.imag;
945+
}
946+
return complex_subtype_from_doubles(type, cr.real, ci.real);
941947
}
942948

943949
PyDoc_STRVAR(complex_doc,

0 commit comments

Comments
 (0)