[Python-ideas] math.nextafter

Stephan Houben stephanh42 at gmail.com
Sat Feb 4 06:59:51 EST 2017


Hi all,

Visual C++ 2015 supports this one:

https://msdn.microsoft.com/en-us/library/h0dff77w.aspx

In any case, this is easy to implement an efficient fallback in C, unlike
the fma() function we discussed some time ago.

To put this in a bit wider perspective: would it be useful to investigate
how much of the C99 math library could
be supported in Python in general?

Stephan


2017-02-04 12:52 GMT+01:00 <tritium-list at sdamon.com>:

> The presence of the function in C99’s math.h isn’t strictly useful unless
> it is also in the MSVC math.h.  MSVC only supports a subset of C99
>
>
>
> *From:* Python-ideas [mailto:python-ideas-bounces+tritium-list=sdamon.com@
> python.org] *On Behalf Of *Juraj Sukop
> *Sent:* Saturday, February 4, 2017 6:31 AM
> *To:* python-ideas at python.org
> *Subject:* [Python-ideas] math.nextafter
>
>
>
> Hello!
>
>
>
> Function `nextafter(x, y)` returns the next representable value of `x` in
> the direction of `y`, and if `x` equals to `y`, `y` is returned. [1]
>
>
>
> It is useful for incrementing/decrementing floating-point number by the
> smallest amount possible or for testing if two numbers are closest to each
> other without being the same.
>
>
>
> The following snippet written by Mark Dickinson emulates the functionality
> in pure Python [2]:
>
>
>
>     import math
>
>     import struct
>
>
>
>     def next_up(x):
>
>         # NaNs and positive infinity map to themselves.
>
>         if math.isnan(x) or (math.isinf(x) and x > 0):
>
>             return x
>
>
>
>         # 0.0 and -0.0 both map to the smallest +ve float.
>
>         if x == 0.0:
>
>             x = 0.0
>
>
>
>         n = struct.unpack('<q', struct.pack('<d', x))[0]
>
>         if n >= 0:
>
>             n += 1
>
>         else:
>
>             n -= 1
>
>         return struct.unpack('<d', struct.pack('<q', n))[0]
>
>
>
>     def next_down(x):
>
>         return -next_up(-x)
>
>
>
>     def next_after(x, y):
>
>         # If either argument is a NaN, return that argument.
>
>         # This matches the implementation in decimal.Decimal
>
>         if math.isnan(x):
>
>             return x
>
>         if math.isnan(y):
>
>             return y
>
>
>
>         if y == x:
>
>             return y
>
>         elif y > x:
>
>             return next_up(x)
>
>         else:
>
>             return next_down(x)
>
>
>
> Other implementations can be found at [3], [4] or [5], for example.
>
>
>
> It would be useful to have `math.nextafter` function available in standard
> library, it also is provided by C99 <math.h>, and is similar in spirit to
> `math.copysign` or `math.isclose`.
>
>
>
> As to why to include it by default when the above snippet works just fine,
> a C implementation is likely to be much faster than using `struct.pack` and
> `struct.unpack`.
>
>
>
> Thank you for considering this proposal and any feedback is greatly
> welcomed!
>
>
>
> Juraj Sukop
>
>
>
> [1] http://en.cppreference.com/w/c/numeric/math/nextafter
>
> [2] http://stackoverflow.com/a/10426033
>
> [3] http://git.musl-libc.org/cgit/musl/tree/src/math/nextafter.c
>
> [4] https://github.com/android/platform_bionic/blob/master/
> libm/upstream-freebsd/lib/msun/src/s_nextafter.c
>
> [5] https://github.com/golang/go/blob/master/src/math/nextafter.go
>
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170204/c9163922/attachment.html>


More information about the Python-ideas mailing list