# [Python-ideas] math.nextafter

Nathaniel Smith njs at pobox.com
Fri Feb 24 21:43:01 EST 2017

```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.

-n
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170224/aea17b8e/attachment.html>
```