Author vstinner
Recipients vstinner
Date 2020-01-10.15:51:21
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1578671482.19.0.587553829591.issue39288@roundup.psfhosted.org>
In-reply-to
Content
Linux manual page of nextafter():
"""
The nextafter() function return the next representable floating-point  value following x in the direction of y.  If y is less than x, these functions will return the largest representable number less than x.

If x equals y, the functions return y.
"""

I used this function to round an integer towards zero when casting a float to an integer in bpo-39277. Example in C:

#include <math.h>
#include <stdio.h>
#include <stdint.h>
int main()
{
    int64_t int64_max = 9223372036854775807LL;
    double d = (double)int64_max;  /* ROUND_HALF_EVEN */
    double d2 = nextafter(d, 0.0);
    printf("i = %ld\n", int64_max);
    printf("d = %.0f\n", d);
    printf("d2 = %.0f\n", d2);
    printf("d - d2 = %.0f\n", d - d2);
    return 0;
}

Output:

i = 9223372036854775807
d = 9223372036854775808
d2 = 9223372036854774784
d - d2 = 1024

The function exists in numpy:

numpy.nextafter(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'nextafter'>

    Return the next floating-point value after x1 towards x2, element-wise.

https://docs.scipy.org/doc/numpy/reference/generated/numpy.nextafter.html


Attached PR adds math.nextafter().
History
Date User Action Args
2020-01-10 15:51:22vstinnersetrecipients: + vstinner
2020-01-10 15:51:22vstinnersetmessageid: <1578671482.19.0.587553829591.issue39288@roundup.psfhosted.org>
2020-01-10 15:51:22vstinnerlinkissue39288 messages
2020-01-10 15:51:21vstinnercreate