-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-39288: Add math.nextafter(x, y) #17937
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
Conversation
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 |
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.) |
Lib/test/test_math.py
Outdated
@@ -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), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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. |
I added more tests. Which tests do you want to signed zeros? The sign doesn't seem to matter:
For subnormals, I wrote the following tests, is it what you expect?
Do you that if nextafter() behaves differently on a platform, Python should patch the function? |
🤖 New build scheduled with the buildbot fleet by @vstinner for commit c12293b0d5bcef86db12b5075bc3500b17ff11be 🤖 If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again. |
Let me try this new toy :-) |
Modules/mathmodule.c
Outdated
math.nextafter | ||
|
||
x: float | ||
y: float |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
I don't understand this result, but nextafter() gives me the expected result, whereas 1.0-epsilon gives me a surprising smaller value.
|
Good: the test passed on all buildbots! buildbot/PPC64 Fedora PR failed on test_distutils, but it's unrelated to this PR: |
I fixed the implementation to use C double rather than C float, rebased my PR and squashed commits. @mdickinson: Would you mind to review the PR again? |
I'm not sure about "If x equals to y, return y": an implementation might return x. But I wrote an unit test to ensure that y is returned: the test using signed zeros. |
Makes sense: assuming IEEE 754, the next Python |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM; one documentation nitpick.
There's a suggestion on the issue to make the second argument optional. I'd suggest merging this PR as-is, and doing that in a separate PR if we decide that's the way we want to go.
Doc/library/math.rst
Outdated
|
||
Return the next floating-point value after *x* towards *y*. | ||
|
||
If *x* equals to *y*, return *y*. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest "is equal to" in place of "equals to"; it reads a bit better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. You may suggest this change to the Linux manual page of nextafter() as well where I stole this sentence :-)
Return the next floating-point value after x towards y.
My hope is that all platforms we care about follow the specification, but I'd at least like to know about it if there are platforms that don't. If it does turn out that some platforms deviate from the specification, we can make an informed decision about how to deal with them. But let's save that for when it happens. |
PR rebased to fix a conflict in Doc/whatsnew/3.9.rst and commits squashed. Once regular CI tests will pass, I will give a second try to buildbots since I added many tests since the previous run. |
@mdickinson: "If it does turn out that some platforms deviate from the specification, we can make an informed decision about how to deal with them. But let's save that for when it happens." Sure, I totally agree. I added tons of unit tests, so different behaviors will be easily catched. |
Yep, the tests look very good. :-) |
Thanks for the review @mdickinson and for helping to write unit tests. |
Return the next floating-point value after x towards y.
Return the next floating-point value after x towards y.
https://bugs.python.org/issue39288