Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-39288: Add math.nextafter(x, y) #17937

Open
wants to merge 4 commits into
base: master
from
Open

Conversation

@vstinner
Copy link
Member

vstinner commented Jan 10, 2020

Return the next floating-point value after x towards y.

https://bugs.python.org/issue39288

Return the next floating-point value after x towards y.
@vstinner

This comment has been minimized.

Copy link
Member Author

vstinner commented Jan 10, 2020

I'm not sure about the unit tests: not sure if they are portable on any IEEE 754 implementation, and if the test would work on non-IEEE 754 implementation. Maybe the test should be decorated ith @requires_IEEE_754?

@mdickinson

This comment has been minimized.

Copy link
Member

mdickinson commented Jan 10, 2020

Maybe the test should be decorated ith @requires_IEEE_754?

Yes, I think so. There's nothing to guarantee that the existing test would work on a non IEEE 754 system, should Python ever encounter one.

Please could we also have tests covering all the various corner cases: signed zeros, infinities, nans, subnormals, etc.? There's a lot that can go wrong, and I don't think it's safe to trust that the platform's libm does the right thing. (We've tried that before.)

@@ -2033,6 +2033,12 @@ def testComb(self):
self.assertIs(type(comb(IntSubclass(5), IntSubclass(k))), int)
self.assertIs(type(comb(MyIndexable(5), MyIndexable(k))), int)

def test_nextafter(self):
self.assertEqual(math.nextafter(9223372036854775807.0, 0.0),

This comment has been minimized.

Copy link
@mdickinson

mdickinson Jan 10, 2020

Member

It would be clearer to use a value that's exactly representable here: 9223372036854775808.0 rather than 9223372036854775807.0. Otherwise there are two effects going on: first, the literal is being rounded to a different value, and then that value is having nextafter applied to it.

@mdickinson

This comment has been minimized.

Copy link
Member

mdickinson commented Jan 10, 2020

Oh, and once we have more comprehensive tests, it might be worth trying out the new "test on the buildbots" functionality with this PR, to ferret out any platforms that have dodgy implementations of nextafter.

vstinner added 2 commits Jan 10, 2020
Decorate tests with @requires_IEEE_754.
@vstinner

This comment has been minimized.

Copy link
Member Author

vstinner commented Jan 11, 2020

Please could we also have tests covering all the various corner cases: signed zeros, infinities, nans, subnormals, etc.?

I added more tests. Which tests do you want to signed zeros? The sign doesn't seem to matter:

>>> math.nextafter(0.0, 1.0)
5e-324
>>> math.nextafter(-0.0, 1.0)
5e-324
>>> math.nextafter(-0.0, -1.0)
-5e-324
>>> math.nextafter(+0.0, -1.0)
-5e-324

For subnormals, I wrote the following tests, is it what you expect?

        # around 0.0
        self.assertEqual(math.nextafter(0.0, INF),
                         sys.float_info.min * sys.float_info.epsilon)
        self.assertEqual(math.nextafter(0.0, -INF),
                         (-sys.float_info.min) * sys.float_info.epsilon)

There's a lot that can go wrong, and I don't think it's safe to trust that the platform's libm does the right thing. (We've tried that before.)

Do you that if nextafter() behaves differently on a platform, Python should patch the function?

@bedevere-bot

This comment has been minimized.

Copy link

bedevere-bot commented Jan 11, 2020

🤖 New build scheduled with the buildbot fleet by @vstinner for commit c12293b 🤖

If you want to schedule another build, you need to add the "🔨 test-with-buildbots" label again.

@vstinner

This comment has been minimized.

Copy link
Member Author

vstinner commented Jan 11, 2020

Oh, and once we have more comprehensive tests, it might be worth trying out the new "test on the buildbots" functionality with this PR, to ferret out any platforms that have dodgy implementations of nextafter.

Let me try this new toy :-)

math.nextafter
x: float
y: float

This comment has been minimized.

Copy link
@vstinner

vstinner Jan 11, 2020

Author Member

Oh. I didn't notice, that's a C float, whereas I mean a "Python float". In fact, I should use "double" here.

I will fix it once the buildbot tests will complete.

@vstinner

This comment has been minimized.

Copy link
Member Author

vstinner commented Jan 11, 2020

I don't understand this result, but nextafter() gives me the expected result, whereas 1.0-epsilon gives me a surprising smaller value.

>>> math.nextafter(1.0, 2.0) == 1.0 + sys.float_info.epsilon
True
>>> math.nextafter(1.0, 0.0) == 1.0 - sys.float_info.epsilon
False
>>> math.nextafter(1.0, 0.0), 1.0 - sys.float_info.epsilon
(0.9999999999999999, 0.9999999999999998)
>>> math.nextafter(1.0, 0.0).hex(), (1.0 - sys.float_info.epsilon).hex()
('0x1.fffffffffffffp-1', '0x1.ffffffffffffep-1')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.