Skip to content

Commit

Permalink
pythongh-123811: test that round() can return signed zero
Browse files Browse the repository at this point in the history
  • Loading branch information
skirpichev committed Sep 8, 2024
1 parent 11fa119 commit d0be987
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions Lib/test/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,28 @@ def test_short_repr(self):
@support.requires_IEEE_754
class RoundTestCase(unittest.TestCase):

def assertFloatsAreIdentical(self, x, y):
"""Fail unless floats x and y are identical, in the sense that:
(1) both x and y are nans, or
(2) both x and y are infinities, with the same sign, or
(3) both x and y are zeros, with the same sign, or
(4) x and y are both finite and nonzero, and x == y
"""
msg = 'floats {!r} and {!r} are not identical'

if isnan(x) or isnan(y):
if isnan(x) and isnan(y):
return
elif x == y:
if x != 0.0:
return
# both zero; check that signs match
elif copysign(1.0, x) == copysign(1.0, y):
return
else:
msg += ': zeros have different signs'
self.fail(msg.format(x, y))

def test_inf_nan(self):
self.assertRaises(OverflowError, round, INF)
self.assertRaises(OverflowError, round, -INF)
Expand Down Expand Up @@ -892,10 +914,10 @@ def test_large_n(self):

def test_small_n(self):
for n in [-308, -309, -400, 1-2**31, -2**31, -2**31-1, -2**100]:
self.assertEqual(round(123.456, n), 0.0)
self.assertEqual(round(-123.456, n), -0.0)
self.assertEqual(round(1e300, n), 0.0)
self.assertEqual(round(1e-320, n), 0.0)
self.assertFloatsAreIdentical(round(123.456, n), 0.0)
self.assertFloatsAreIdentical(round(-123.456, n), -0.0)
self.assertFloatsAreIdentical(round(1e300, n), 0.0)
self.assertFloatsAreIdentical(round(1e-320, n), 0.0)

def test_overflow(self):
self.assertRaises(OverflowError, round, 1.6e308, -308)
Expand Down

0 comments on commit d0be987

Please sign in to comment.