mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
bpo-39288: Add math.nextafter(x, y) (GH-17937)
Return the next floating-point value after x towards y.
This commit is contained in:
parent
1b335ae281
commit
100fafcf20
6 changed files with 140 additions and 1 deletions
|
@ -2033,6 +2033,60 @@ class IsCloseTests(unittest.TestCase):
|
|||
self.assertIs(type(comb(IntSubclass(5), IntSubclass(k))), int)
|
||||
self.assertIs(type(comb(MyIndexable(5), MyIndexable(k))), int)
|
||||
|
||||
def assertEqualSign(self, x, y):
|
||||
"""Similar to assertEqual(), but compare also the sign.
|
||||
|
||||
Function useful to check to signed zero.
|
||||
"""
|
||||
self.assertEqual(x, y)
|
||||
self.assertEqual(math.copysign(1.0, x), math.copysign(1.0, y))
|
||||
|
||||
@requires_IEEE_754
|
||||
def test_nextafter(self):
|
||||
# around 2^52 and 2^63
|
||||
self.assertEqual(math.nextafter(4503599627370496.0, -INF),
|
||||
4503599627370495.5)
|
||||
self.assertEqual(math.nextafter(4503599627370496.0, INF),
|
||||
4503599627370497.0)
|
||||
self.assertEqual(math.nextafter(9223372036854775808.0, 0.0),
|
||||
9223372036854774784.0)
|
||||
self.assertEqual(math.nextafter(-9223372036854775808.0, 0.0),
|
||||
-9223372036854774784.0)
|
||||
|
||||
# around 1.0
|
||||
self.assertEqual(math.nextafter(1.0, -INF),
|
||||
float.fromhex('0x1.fffffffffffffp-1'))
|
||||
self.assertEqual(math.nextafter(1.0, INF),
|
||||
float.fromhex('0x1.0000000000001p+0'))
|
||||
|
||||
# x == y: y is returned
|
||||
self.assertEqual(math.nextafter(2.0, 2.0), 2.0)
|
||||
self.assertEqualSign(math.nextafter(-0.0, +0.0), +0.0)
|
||||
self.assertEqualSign(math.nextafter(+0.0, -0.0), -0.0)
|
||||
|
||||
# around 0.0
|
||||
smallest_subnormal = sys.float_info.min * sys.float_info.epsilon
|
||||
self.assertEqual(math.nextafter(+0.0, INF), smallest_subnormal)
|
||||
self.assertEqual(math.nextafter(-0.0, INF), smallest_subnormal)
|
||||
self.assertEqual(math.nextafter(+0.0, -INF), -smallest_subnormal)
|
||||
self.assertEqual(math.nextafter(-0.0, -INF), -smallest_subnormal)
|
||||
self.assertEqualSign(math.nextafter(smallest_subnormal, +0.0), +0.0)
|
||||
self.assertEqualSign(math.nextafter(-smallest_subnormal, +0.0), -0.0)
|
||||
self.assertEqualSign(math.nextafter(smallest_subnormal, -0.0), +0.0)
|
||||
self.assertEqualSign(math.nextafter(-smallest_subnormal, -0.0), -0.0)
|
||||
|
||||
# around infinity
|
||||
largest_normal = sys.float_info.max
|
||||
self.assertEqual(math.nextafter(INF, 0.0), largest_normal)
|
||||
self.assertEqual(math.nextafter(-INF, 0.0), -largest_normal)
|
||||
self.assertEqual(math.nextafter(largest_normal, INF), INF)
|
||||
self.assertEqual(math.nextafter(-largest_normal, -INF), -INF)
|
||||
|
||||
# NaN
|
||||
self.assertTrue(math.isnan(math.nextafter(NAN, 1.0)))
|
||||
self.assertTrue(math.isnan(math.nextafter(1.0, NAN)))
|
||||
self.assertTrue(math.isnan(math.nextafter(NAN, NAN)))
|
||||
|
||||
|
||||
def test_main():
|
||||
from doctest import DocFileSuite
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue