Skip to content

Commit d0be987

Browse files
committed
pythongh-123811: test that round() can return signed zero
1 parent 11fa119 commit d0be987

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

Lib/test/test_float.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,28 @@ def test_short_repr(self):
864864
@support.requires_IEEE_754
865865
class RoundTestCase(unittest.TestCase):
866866

867+
def assertFloatsAreIdentical(self, x, y):
868+
"""Fail unless floats x and y are identical, in the sense that:
869+
(1) both x and y are nans, or
870+
(2) both x and y are infinities, with the same sign, or
871+
(3) both x and y are zeros, with the same sign, or
872+
(4) x and y are both finite and nonzero, and x == y
873+
"""
874+
msg = 'floats {!r} and {!r} are not identical'
875+
876+
if isnan(x) or isnan(y):
877+
if isnan(x) and isnan(y):
878+
return
879+
elif x == y:
880+
if x != 0.0:
881+
return
882+
# both zero; check that signs match
883+
elif copysign(1.0, x) == copysign(1.0, y):
884+
return
885+
else:
886+
msg += ': zeros have different signs'
887+
self.fail(msg.format(x, y))
888+
867889
def test_inf_nan(self):
868890
self.assertRaises(OverflowError, round, INF)
869891
self.assertRaises(OverflowError, round, -INF)
@@ -892,10 +914,10 @@ def test_large_n(self):
892914

893915
def test_small_n(self):
894916
for n in [-308, -309, -400, 1-2**31, -2**31, -2**31-1, -2**100]:
895-
self.assertEqual(round(123.456, n), 0.0)
896-
self.assertEqual(round(-123.456, n), -0.0)
897-
self.assertEqual(round(1e300, n), 0.0)
898-
self.assertEqual(round(1e-320, n), 0.0)
917+
self.assertFloatsAreIdentical(round(123.456, n), 0.0)
918+
self.assertFloatsAreIdentical(round(-123.456, n), -0.0)
919+
self.assertFloatsAreIdentical(round(1e300, n), 0.0)
920+
self.assertFloatsAreIdentical(round(1e-320, n), 0.0)
899921

900922
def test_overflow(self):
901923
self.assertRaises(OverflowError, round, 1.6e308, -308)

0 commit comments

Comments
 (0)