New submission from STINNER Victor <vstin...@python.org>:
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(). ---------- components: Library (Lib) messages: 359731 nosy: vstinner priority: normal severity: normal status: open title: Add math.nextafter(a, b) versions: Python 3.9 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue39288> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com