GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-94906: Support multiple steps in math.nextafter by matthiasgoergens · Pull Request #103881 · python/cpython

Conversation

@matthiasgoergens

@matthiasgoergens matthiasgoergens commented Apr 26, 2023 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

See issue for discussion.

  • Issue: Let math.nextafter() compute multiple steps at a time. #94906

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot mentioned this pull request Apr 26, 2023
Let math.nextafter() compute multiple steps at a time. #94906
Closed
@bedevere-bot bedevere-bot added the awaiting review label Apr 26, 2023
@matthiasgoergens
pythongh-94906: Support multiple steps in math.nextafter
77fb4ae
@matthiasgoergens
matthiasgoergens force-pushed the gh-94906 branch from a96ad31 to 77fb4ae Compare April 26, 2023 14:44
@rhettinger

rhettinger commented Apr 26, 2023

Copy link
Copy Markdown
Contributor

It would be nice if steps could be negative rather than having to switch the sign of the second argument:

# Current way
nearby = [nextafter(x, -inf, steps=2),
          nextafter(x, -inf, steps=1),
          x,
          nextafter(x, inf, steps=1),
          nextafter(x, inf, steps=2),
]

# Better way
nearby = [nextafter(x, inf, steps=n) for n in (-2, -1, 0, 1, 2)]
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rhettinger rhettinger assigned mdickinson Apr 26, 2023
@rhettinger

rhettinger commented Apr 26, 2023

Copy link
Copy Markdown
Contributor

@mdickinson Please make the final decision on this. I still like the idea and would use it, but if you want to reject it, you have my support.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@matthiasgoergens

matthiasgoergens commented Apr 27, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

@rhettinger I'd be happy to make negative steps work. I left it out for simplicity, because there are questions about semantics that would need to be resolved:

The most obvious semantics would be for negative steps to move away from the target.

Or are you suggesting that negative steps should be equivalent to negating the target argument? So for example:

nextafter(x, 0.0, -1) == nextafter(x, -0.0, 1)

Should infinity be absorbing, or should going beyond infinity give NaN? The former seems reasonable to me. But I can also come up with arguments for the latter.

Similarly, we have nextafter(x, x, n) == x for all x and all n. (Ignoring that NaNs don't compare with == equality.) Including x=inf.

That's rather different from nextafter(x, -x, -n) for most x and most n.

We could throw a value error for all the weird cases, and allow negative n for the obvious cases only. But that seems like it might be hard to understand for users?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mdickinson

mdickinson commented Apr 27, 2023

Copy link
Copy Markdown
Member

Thanks @rhettinger. I'm short of time right now, but I'll take a fresh look at this this weekend.

rhettinger reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mdickinson

mdickinson commented May 1, 2023

Copy link
Copy Markdown
Member

Okay, let's go ahead with this.

Comments: I'm not totally convinced that nextafter is the right place for this functionality - I suspect the real need is to either add or subtract "n" ulps from a given value, while nextafter is doing something more complicated than that (e.g., saturating when it hits the target value). I'd be open to suggestions for alternative spellings for the functionality. (Aside: an obvious extension of this functionality would be the ability to subtract one float from another and get a result expressed in an integer number of ulps.)

But nextafter itself is already a slightly odd beast, in that I don't think the generality of the second argument is valuable - I suspect there are few use-cases that aren't one of next-towards-plus-infinity / next-towards-negative-infinity / next-towards-zero / next-away-from-zero (and that last one isn't even expressible cleanly with nextafter). Maybe that's why IEEE 754 went with nextUp and nextDown instead of following C's nextafter.

It's only recently that we've started assuming that the floating-point format in use by CPython is IEEE 754 binary64, which is what makes the type-punning via a union work. (Note that we're also assuming in this PR that uint64_t and double have matching endianness; in theory that's not guaranteed, but in practice it seems to be a fairly safe assumption. It may be worth a comment in the implementation, though.)

@matthiasgoergens

On negative steps:

The most obvious semantics would be for negative steps to move away from the target.

Agreed, and that's what I'd favour, but I also agree that the semantics aren't obvious. The more I think about it, the more this feels like an argument for having nextdown and nextup instead of nextafter (or even just nextup), where it is obvious what negative steps would mean.

Should infinity be absorbing, or should going beyond infinity give NaN?

Definitely absorbing.

How about we get this in before the beta-1 cutoff and punt on allowing negative steps for now - we can always add those later.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@matthiasgoergens

matthiasgoergens commented May 2, 2023

Copy link
Copy Markdown
Contributor Author

@mdickinson Seems all good to me.

(Aside: an obvious extension of this functionality would be the ability to subtract one float from another and get a result expressed in an integer number of ulps.)

We can do that in pure Python and stick it into a (non-standard) library, if there's demand for it?

Even the multiple-steps-nextafter could live in a place that's not the standard library. I only took this PR to show off that you don't need a loop but can use some clever manipulation of the bits instead, not actually because I think this functionality needs to live in the standard library.

How about we get this in before the beta-1 cutoff and punt on allowing negative steps for now - we can always add those later.

Works for me! Is there anything we need to change in this PR for that?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@matthiasgoergens
Merge branch 'main' into pythongh-94906
5e97978
@mdickinson
mdickinson self-requested a review May 7, 2023 09:56
mdickinson
mdickinson reviewed May 8, 2023
View reviewed changes

@mdickinson mdickinson left a comment

Copy link
Copy Markdown
Member

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.

This generally looks good to me; a few comments.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Modules/mathmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/mathmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/mathmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/math.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@rhettinger

rhettinger commented May 16, 2023

Copy link
Copy Markdown
Contributor

The feature freeze is fast approaching. Is the ready to be applied? Once the core API is in, it is okay for the docs and tests continue to be tweaked.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

matthiasgoergens added 11 commits May 17, 2023 14:27
@matthiasgoergens
Merge remote-tracking branch 'origin/main' into pythongh-94906
0f2b30f
@matthiasgoergens
Pass through NaN unchanged
b90e86c
@matthiasgoergens
Explain that we need same endianness
accc22f
@matthiasgoergens
Allow arbitrarily many steps
8e1db39
@matthiasgoergens
Fix problems
6873347
@matthiasgoergens
Update regen-all
1fff8b4
@matthiasgoergens
Make steps also available as a non-kw arg
311f266
@matthiasgoergens
Fix docs
cd70fea
@matthiasgoergens
Update docs
e0421b6
@matthiasgoergens
Py_None instead of NULL
a8c83a7
@matthiasgoergens
Fix typo
88c46c4
matthiasgoergens
matthiasgoergens commented May 17, 2023
View reviewed changes
Comment thread Doc/library/math.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@matthiasgoergens

matthiasgoergens commented May 17, 2023

Copy link
Copy Markdown
Contributor Author

@rhettinger I implemented the review suggestions.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@matthiasgoergens

matthiasgoergens commented May 17, 2023

Copy link
Copy Markdown
Contributor Author

There's a failed check "Tests / Ubunt SSL tests with OpenSSL (1.1.1t)" but it seems to be flaky and nothing to do with this PR. https://github.com/python/cpython/actions/runs/5001023587/jobs/8959184113?pr=103881

mdickinson reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@matthiasgoergens
Tickle the CI/CD
2232bb4
matthiasgoergens added 4 commits May 17, 2023 16:38
@matthiasgoergens
Revert "Make steps also available as a non-kw arg"
a2724f6
This reverts commit 311f266.
@matthiasgoergens
Merge branch 'main' into pythongh-94906
b7b5ac6
@matthiasgoergens
property based tests (should fail)
b45f9c2
@matthiasgoergens
Property based tests
1f9d890
@matthiasgoergens

matthiasgoergens commented May 17, 2023

Copy link
Copy Markdown
Contributor Author

I noticed that we are supporting hypothesis property based testing now. I added some property tests.

mdickinson reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

matthiasgoergens added 2 commits May 17, 2023 17:34
@matthiasgoergens
simplify
2153302
@matthiasgoergens
Tickle CI/CD
61c7c6e
@matthiasgoergens

matthiasgoergens commented May 17, 2023

Copy link
Copy Markdown
Contributor Author

Hmm, I don't understand the test failures. They seem unrelated to what I did, but I am not sure. My tests work locally for me.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@matthiasgoergens
Merge branch 'main' into pythongh-94906
04af22b
@mdickinson

mdickinson commented May 19, 2023

Copy link
Copy Markdown
Member

Thanks for the updates; I'll re-review (and look into the bot failure) shortly.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mdickinson
Fix whitespace issue
6527b77
mdickinson
mdickinson reviewed May 19, 2023
View reviewed changes
Comment thread Modules/mathmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@mdickinson

mdickinson commented May 19, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

@matthiasgoergens

Hmm, I don't understand the test failures. [...]

make patchcheck was failing on a whitespace error (a stray TAB character); I've fixed it in 6527b77.

matthiasgoergens reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mdickinson
Portability fix and a minor style fix
66ef3f9
@mdickinson mdickinson added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label May 19, 2023
@bedevere-bot

bedevere-bot commented May 19, 2023

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @mdickinson for commit 66ef3f9 🤖

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label May 19, 2023
mdickinson
mdickinson approved these changes May 19, 2023
View reviewed changes

@mdickinson mdickinson left a comment

Copy link
Copy Markdown
Member

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.

Code LGTM; functionality works as expected in manual testing, and the bots seem happy. I'm not going to wait for the buildbots to finish, since that will take all night (literally).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting review labels May 19, 2023
@mdickinson
mdickinson merged commit 6e39fa1 into python:main May 19, 2023
@bedevere-bot bedevere-bot removed the awaiting merge label May 19, 2023
carljm added a commit to gsallam/cpython_with_perfmap_apii that referenced this pull request May 20, 2023
@carljm
Merge branch 'main' into gsallam/main
ea5965a
* main: (30 commits)
  pythongh-103987: fix several crashes in mmap module (python#103990)
  docs: fix wrong indentation causing rendering error in dis page (python#104661)
  pythongh-94906: Support multiple steps in math.nextafter (python#103881)
  pythongh-104472: Skip `test_subprocess.ProcessTestCase.test_empty_env` if ASAN is enabled (python#104667)
  pythongh-103839: Allow building Tkinter against Tcl 8.7 without external libtommath (pythonGH-103842)
  pythongh-85984: New additions and improvements to the tty library. (python#101832)
  pythongh-104659: Consolidate python examples in enum documentation (python#104665)
  pythongh-92248: Deprecate `type`, `choices`, `metavar` parameters of `argparse.BooleanOptionalAction` (python#103678)
  pythongh-104645: fix error handling in marshal tests (python#104646)
  pythongh-104600: Make type.__type_params__ writable (python#104634)
  pythongh-104602: Add additional test for listcomp with lambda (python#104639)
  pythongh-104640: Disallow walrus in comprehension within type scopes (python#104641)
  pythongh-103921: Rename "type" header in argparse docs (python#104654)
  Improve readability of `typing._ProtocolMeta.__instancecheck__` (python#104649)
  pythongh-96522: Fix deadlock in pty.spawn (python#96639)
  pythonGH-102818: Do not call `PyTraceBack_Here` in sys.settrace trampoline.  (pythonGH-104579)
  pythonGH-103545: Add macOS specific constants for ``os.setpriority`` to ``os`` (python#104606)
  pythongh-104623: Update macOS installer to SQLite 3.42.0 (pythonGH-104624)
  pythongh-104619: never leak comprehension locals to outer locals() (python#104637)
  pythongh-104602: ensure all cellvars are known up front (python#104603)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer
@mdickinson mdickinson mdickinson approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@mdickinson mdickinson

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@matthiasgoergens @rhettinger @mdickinson @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.