[Python-ideas] math.nextafter

David Mertz mertz at gnosis.cx
Sat Feb 25 12:27:58 EST 2017

I do agree that a math.count_ulps() would be very nice to have. Or whatever
spelling NumPy users for that concept.

I see nextafter as more similar. Yes, it's not a uniform increment, which
is the whole point. If you want a convergence that is as good as it can be
in float, that's the relevant measure. Likewise for "almost as good" being
N ulps.

On Feb 24, 2017 9:43 PM, "Nathaniel Smith" <njs at pobox.com> wrote:

On Feb 24, 2017 5:29 PM, "David Mertz" <mertz at gnosis.cx> wrote:

Marc-André slightly misspelled the recent-ish addition of math.isclose(),
but I agree that it is absolutely where a "nextafter" belongs.

The function signature is already relatively complex to cover several
different but related use cases.  I.e.:

    is_close(a, b, *, rel_tol=1e-09, abs_tol=0.0) -> bool

I think an additional keyword-only argument for `nextafter_tol` (maybe a
different spelling) would fit very naturally there.  This would allow
someone to specify 1 for that degree of closeness, but it would also allow
them to specify some integer larger than 1 without needed to write a loop
calling `nextafter()` repeatedly.

My 2c: I disagree -- numerical tolerance based closeness is fundamentally
different than floating point representation based closeness (often
discussed with the term "ulp" = "unit in the last place". For example, the
number that's closest to 1.0 from above is twice as far away in numerical
terms as the number that's closest to it from below. Mixing these two
concepts in one function is just confusing, and we're not going to run out
of names.

It's also a little weird to jump from nextafter to isclose, since AFAIK
they have pretty much non-overlapping use cases...

FWIW, numpy provides all of the following as separate functions:

* an isclose equivalent
* nextafter
* a function for counting the number of ulps between two floats
* a function for checking that two floats differ by at most N ulps

I'm not enough of an expert on numerical analysis to have an opinion on how
useful these would be for Python itself. They certainly are part of a
complete IEEE754 implementation, and useful for exploring the details of
how floats work, if nothing else.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170225/1661f5ac/attachment.html>

More information about the Python-ideas mailing list