self.assertAlmostEqual(complex(complex1(1j)), 2j)
self.assertRaises(TypeError, complex, complex2(1j))
+ @support.requires_IEEE_754
+ def test_constructor_special_numbers(self):
+ class complex2(complex):
+ pass
+ for x in 0.0, -0.0, INF, -INF, NAN:
+ for y in 0.0, -0.0, INF, -INF, NAN:
+ with self.subTest(x=x, y=y):
+ z = complex(x, y)
+ self.assertFloatsAreIdentical(z.real, x)
+ self.assertFloatsAreIdentical(z.imag, y)
+ z = complex2(x, y)
+ self.assertIs(type(z), complex2)
+ self.assertFloatsAreIdentical(z.real, x)
+ self.assertFloatsAreIdentical(z.imag, y)
+ z = complex(complex2(x, y))
+ self.assertIs(type(z), complex)
+ self.assertFloatsAreIdentical(z.real, x)
+ self.assertFloatsAreIdentical(z.imag, y)
+ z = complex2(complex(x, y))
+ self.assertIs(type(z), complex2)
+ self.assertFloatsAreIdentical(z.real, x)
+ self.assertFloatsAreIdentical(z.imag, y)
+
def test_underscores(self):
# check underscores
for lit in VALID_UNDERSCORE_LITERALS:
return NULL;
}
cr.real = PyFloat_AsDouble(tmp);
- cr.imag = 0.0; /* Shut up compiler warning */
+ cr.imag = 0.0;
Py_DECREF(tmp);
}
if (i == NULL) {
- ci.real = 0.0;
+ ci.real = cr.imag;
}
else if (PyComplex_Check(i)) {
ci = ((PyComplexObject*)i)->cval;
if (ci_is_complex) {
cr.real -= ci.imag;
}
- if (cr_is_complex) {
+ if (cr_is_complex && i != NULL) {
ci.real += cr.imag;
}
return complex_subtype_from_doubles(type, cr.real, ci.real);