steve.dower at python.org
Sat Feb 4 10:15:11 EST 2017
These days, the subset of C99 supported by MSVC is "most" of it, so feel free to start off by assuming the best, at least for new features (the version we use for 2.7 obviously is not improving).
Top-posted from my Windows Phone
From: "tritium-list at sdamon.com" <tritium-list at sdamon.com>
Sent: 2/4/2017 3:53
To: "python-ideas at python.org" <python-ideas at python.org>
Subject: Re: [Python-ideas] math.nextafter
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 at 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
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. 
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 :
# NaNs and positive infinity map to themselves.
if math.isnan(x) or (math.isinf(x) and x > 0):
# 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))
if n >= 0:
n += 1
n -= 1
return struct.unpack('<d', struct.pack('<q', n))
def next_after(x, y):
# If either argument is a NaN, return that argument.
# This matches the implementation in decimal.Decimal
if y == x:
elif y > x:
Other implementations can be found at ,  or , 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!
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas