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 — Let math.nextafter() compute multiple steps at a time. · Issue #94906 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
Closed
Let math.nextafter() compute multiple steps at a time.#94906
Copy link
Labels
3.12only security fixesonly security fixestype-featureA feature request or enhancementA feature request or enhancement

Description

@rhettinger
rhettinger
opened on Jul 16, 2022
Issue body actions

Sometimes math.nextafter() needs to be applied multiple times in succession.

   x = nextafter(nextafter( nextafter(x, inf), inf), inf)    # Three steps up

It would be nice if the function supported this directly:

   x = nextafter(x, inf, n=3)

The implementation would just be a for-loop:

def newnextafter(x, y, /, *, n=1):
    'Return the floating-point value n steps after x towards y.'
    for i in range(n):
        x = nextafter(x, y)
    return x

The formal paramater can be just n or the longer but more descriptive steps.

Linked PRs

  • gh-94906: Support multiple steps in math.nextafter #103881

Activity

Next
  1. rhettinger
    added
    type-featureA feature request or enhancementA feature request or enhancement
    easy
    3.12only security fixesonly security fixes
    on Jul 16, 2022
  2. hauntsaninja
    added a commit that references this issue on Jul 16, 2022

    pythongh-94906: Support multiple steps in math.nextafter

    5ba2c27
  3. mdickinson commented on Jul 17, 2022

    @mdickinson
    mdickinson
    on Jul 17, 2022
    Member
    More actions

    This feels like a needless expansion of the API to me; I don't think I've ever encountered a use-case for this. On the rare occasions that such a use-case turns up, what's wrong with the for loop, or with adding an integer multiple of math.ulp(x) to x?

    -1 from me.

    Reacted by Serhiy Storchaka and SycamoreLeaf
  4. rhettinger commented on Jul 17, 2022

    @rhettinger
    rhettinger
    on Jul 17, 2022
    ContributorAuthor
    More actions

    I wouldn't posted the issue if I hadn't needed this, so it is not "needless".

    In 30 seconds of searching, I found another example:

    https://github.com/ericherman/libefloat/blob/ad606d566ac5310aabb4fff8890e35200a718e8d/tests/test-distance-64.c#L94

    Python is a high level language and it is suitable to incorporate options that are more expansive than used in low level languages like C that tend to focus on atomic steps.

    Reacted by Erdem Özgen
  5. mdickinson commented on Jul 17, 2022

    @mdickinson
    mdickinson
    on Jul 17, 2022
    Member
    More actions

    Sure, I don't doubt that you had a genuine need, but that need is trivially addressed with a for loop. My use of "needless" referred to the expansion of the API. You needing the functionality is not the same thing as the math module needing to support the functionality directly.

    Sorry, but I don't see this use-case as valuable enough or common enough to warrant expanding the math.nextafter API.

    I'm also concerned that this might be a performance trap: a user could easily write nextafter(x, y, steps=10**6) without realising that that means a million invocations of nextafter internally. With an explicit for loop, the effect on running time is much more obvious.

  6. serhiy-storchaka commented on Jul 17, 2022

    @serhiy-storchaka
    serhiy-storchaka
    on Jul 17, 2022
    Member
    More actions

    I concur with @mdickinson. It is too niche feature, and it is not difficult to implement a wrapper in Python. BTW, the case you found is in tests.

    Reacted by SycamoreLeaf
  7. rhettinger commented on Jul 17, 2022

    @rhettinger
    rhettinger
    on Jul 17, 2022
    ContributorAuthor
    More actions

    I wish this wasn't dismissed so casually. All uses of nextafter() are "niche". There are only a handful of people who will ever use it.

    I submitted the feature request because I needed to write the replacement function in pure Python (much like the example I posted above) and it was distracting and felt amiss, like something the should have already have been built in to a higher level language.

    I wrote the function while working on another problem and had casually created an incorrect result along the way:

    >>> sixth_largest_random = 1.0 - 6 * ulp(1.0)
    >>> sixth_largest_random
    0.9999999999999987
    

    Fortunately, I spotted the problem before going too far with it and wrote the above function giving the correct answer:

    >>> sixth_largest_random = newnextafter(1.0, -inf, n=6)
    >>> sixth_largest_random
    0.9999999999999993
    
    

    The entire side trip was distracting and annoying. It took the focus away for the error analysis I was working on at the time.

    The proposal is an easy thing to do. It isn't even slightly confusing. It would be handy when needed by the few who ever use this function. I don't see any downside.

    Note, in numpy and scipy, most interesting functions have many options. That seems to work well for them. But in Python, there seems to be an urge to fight against the simplest of options as "unnecessary API expansion".

    Reacted by Handell and Goncalo Gomes
  8. matthiasgoergens commented on Aug 15, 2022

    @matthiasgoergens
    matthiasgoergens
    on Aug 15, 2022
    Contributor
    More actions

    @rhettinger @hauntsaninja @mdickinson

    You don't need a loop for this. See this pure Python prototype inspired by an old StackOverflow question of mine.

    And here's a prototype of a fix of the PR. (Please keep in mind that the C code in the PR isn't tested nor even run. The pure Python version has Hypothesis tests, so it's much more reliable.)

    Reacted by Goncalo Gomes, amirsharif and Stefan Pochmann
  9. matthiasgoergens
    added 2 commits that reference this issue on Aug 15, 2022

    Merge branch 'main' into pythongh-94906

    71e9d20

    Merge remote-tracking branch 'origin/main' into pythongh-94906

    966291c
  10. ambv
    removed
    easy
    on Apr 24, 2023
  11. ambv commented on Apr 24, 2023

    @ambv
    ambv
    on Apr 24, 2023
    Contributor
    More actions

    Removing "easy" for now as this needs a resolution between the two maintainers of the math module.

  12. mblahay commented on Apr 24, 2023

    @mblahay
    mblahay
    on Apr 24, 2023
    Contributor
    More actions

    @rhettinger @mdickinson

    If the goal here is to produce a one line implementation capable of looping the execution of math.next after, it can be achieved using the reduce function from func tools.

    from math import nextafter,inf
    from functools import reduce
    
    reduce(nextafter,[-inf]*6,1.0)
    

    The starting point, 1.0, is the third argument, then you must provide an iterator that outputs the direction of nextafter for the desired number of times, in this case I use a list and duplicate -inf 6 times.

  13. 7 remaining items

  14. matthiasgoergens
    added a commit that references this issue on Apr 25, 2023

    Merge remote-tracking branch 'origin/main' into pythongh-94906

    37d2783
  15. rhettinger commented on Apr 25, 2023

    @rhettinger
    rhettinger
    on Apr 25, 2023
    ContributorAuthor
    More actions

    Removing "easy" for now as this needs a resolution between the two maintainers of the math module.

    FWIW, I only care about this a tiny bit. Though I think it would be nice to have, I won't lose any sleep if this gets closed.

  16. matthiasgoergens commented on Apr 25, 2023

    @matthiasgoergens
    matthiasgoergens
    on Apr 25, 2023
    Contributor
    More actions

    I overhauled the C PR https://github.com/matthiasgoergens/cpython/pull/5/files Tests pass now.

    I'll point it at the upstream CPython repository, after I polish the git history.

  17. matthiasgoergens
    added 4 commits that reference this issue on Apr 26, 2023

    Merge remote-tracking branch 'origin/main' into pythongh-94906

    f939f45

    pythongh-94906: Support multiple steps in math.nextafter

    a96ad31

    pythongh-94906: Support multiple steps in math.nextafter

    77fb4ae

    Merge branch 'main' into pythongh-94906

    5e97978
  18. matthiasgoergens
    added 3 commits that reference this issue on May 17, 2023

    Merge remote-tracking branch 'origin/main' into pythongh-94906

    0f2b30f

    Merge branch 'main' into pythongh-94906

    b7b5ac6

    Merge branch 'main' into pythongh-94906

    04af22b
  19. mdickinson
    added a commit that references this issue on May 19, 2023

    gh-94906: Support multiple steps in math.nextafter (#103881)

    6e39fa1
  20. carljm
    added a commit that references this issue on May 20, 2023

    Merge branch 'main' into gsallam/main

    ea5965a
  21. mdickinson commented on May 20, 2023

    @mdickinson
    mdickinson
    on May 20, 2023
    Member
    More actions

    Done in #103881. Thanks to @matthiasgoergens.

  22. mdickinson
    closed this as completedon May 20, 2023
  23. matthiasgoergens commented on May 20, 2023

    @matthiasgoergens
    matthiasgoergens
    on May 20, 2023
    Contributor
    More actions

    Done in #103881. Thanks to @matthiasgoergens.

    Thanks for fixing the PR up at the end, too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixesonly security fixestype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.