Skip to content

Commit fd39437

Browse files
committed
TST: Add some tests around (c)math.nan and nan parsing signs
1 parent 230882c commit fd39437

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

Lib/test/test_cmath.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ def test_infinity_and_nan_constants(self):
166166
self.assertEqual(cmath.nan.imag, 0.0)
167167
self.assertEqual(cmath.nanj.real, 0.0)
168168
self.assertTrue(math.isnan(cmath.nanj.imag))
169+
# Also check that the sign of all of these is positive:
170+
self.assertEqual(math.copysign(1., cmath.nan.real), 1.)
171+
self.assertEqual(math.copysign(1., cmath.nan.imag), 1.)
172+
self.assertEqual(math.copysign(1., cmath.nanj.real), 1.)
173+
self.assertEqual(math.copysign(1., cmath.nanj.imag), 1.)
169174

170175
# Check consistency with reprs.
171176
self.assertEqual(repr(cmath.inf), "inf")

Lib/test/test_complex.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,12 @@ class complex2(complex):
529529
self.assertFloatsAreIdentical(z.real, x)
530530
self.assertFloatsAreIdentical(z.imag, y)
531531

532+
def test_constructor_negative_nans_from_string(self):
533+
self.assertEqual(copysign(1., complex("-nan").real), -1.)
534+
self.assertEqual(copysign(1., complex("-nanj").imag), -1.)
535+
self.assertEqual(copysign(1., complex("-nan-nanj").real), -1.)
536+
self.assertEqual(copysign(1., complex("-nan-nanj").imag), -1.)
537+
532538
def test_underscores(self):
533539
# check underscores
534540
for lit in VALID_UNDERSCORE_LITERALS:
@@ -569,6 +575,7 @@ def test(v, expected, test_fn=self.assertEqual):
569575
test(complex(NAN, 1), "(nan+1j)")
570576
test(complex(1, NAN), "(1+nanj)")
571577
test(complex(NAN, NAN), "(nan+nanj)")
578+
test(complex(-NAN, -NAN), "(nan+nanj)")
572579

573580
test(complex(0, INF), "infj")
574581
test(complex(0, -INF), "-infj")

Lib/test/test_float.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,11 +1040,8 @@ def test_inf_signs(self):
10401040
self.assertEqual(copysign(1.0, float('inf')), 1.0)
10411041
self.assertEqual(copysign(1.0, float('-inf')), -1.0)
10421042

1043-
@unittest.skipUnless(getattr(sys, 'float_repr_style', '') == 'short',
1044-
"applies only when using short float repr style")
10451043
def test_nan_signs(self):
1046-
# When using the dtoa.c code, the sign of float('nan') should
1047-
# be predictable.
1044+
# The sign of float('nan') should be predictable.
10481045
self.assertEqual(copysign(1.0, float('nan')), 1.0)
10491046
self.assertEqual(copysign(1.0, float('-nan')), -1.0)
10501047

Lib/test/test_math.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,11 +1881,11 @@ def testIsinf(self):
18811881
self.assertFalse(math.isinf(0.))
18821882
self.assertFalse(math.isinf(1.))
18831883

1884-
@requires_IEEE_754
18851884
def test_nan_constant(self):
1885+
# `math.nan` must be a quiet NaN with positive sign bit
18861886
self.assertTrue(math.isnan(math.nan))
1887+
self.assertEqual(math.copysign(1., math.nan), 1.)
18871888

1888-
@requires_IEEE_754
18891889
def test_inf_constant(self):
18901890
self.assertTrue(math.isinf(math.inf))
18911891
self.assertGreater(math.inf, 0.0)

0 commit comments

Comments
 (0)