The bug is visible as of this writing at https://godbolt.org/z/P1zGr85vs. The input program and result are attached below. When https://sourceware.org/bugzilla/show_bug.cgi?id=6799 was fixed by commit 85422c2acba , the execution path where x==0 a few lines above the change was forgotten. _____ #include <math.h> #include <stdio.h> #include <errno.h> #include <float.h> #include <fenv.h> int main() { double v; printf("ERANGE = %d\n", ERANGE); // nextafter(0., 1.); feclearexcept(FE_UNDERFLOW); errno = 0; v = nextafter(0., 1.); printf("\nnextafter(0., 1.) = %a\n", v); printf("is subnormal: %d\n", fpclassify(v) == FP_SUBNORMAL); printf("errno = %d\n", errno); if(fetestexcept(FE_UNDERFLOW)) printf("FE_UNDERFLOW\n"); // nextafter(DBL_MIN_SUBNORMAL, 1.); feclearexcept(FE_UNDERFLOW); errno = 0; v = nextafter(v, 1.); printf("\nnextafter(DBL_MIN_SUBNORMAL, 1.) = %a\n", v); printf("is subnormal: %d\n", fpclassify(v) == FP_SUBNORMAL); printf("errno = %d\n", errno); if(fetestexcept(FE_UNDERFLOW)) printf("FE_UNDERFLOW\n"); // nextafter(DBL_MIN_SUBNORMAL, 0.); feclearexcept(FE_UNDERFLOW); errno = 0; v = nextafter(0x0.0000000000001p-1022, 0.); printf("\nnextafter(DBL_MIN_SUBNORMAL, 0.) = %a\n", v); printf("is subnormal: %d\n", fpclassify(v) == FP_SUBNORMAL); printf("errno = %d\n", errno); if(fetestexcept(FE_UNDERFLOW)) printf("FE_UNDERFLOW\n"); // nextafter(DBL_MIN, 0.); feclearexcept(FE_UNDERFLOW); errno = 0; v = nextafter(DBL_MIN, 0.); printf("\nnextafter(DBL_MIN, 0.) = %a\n", v); printf("is subnormal: %d\n", fpclassify(v) == FP_SUBNORMAL); printf("errno = %d\n", errno); if(fetestexcept(FE_UNDERFLOW)) printf("FE_UNDERFLOW\n"); // nextafter(1., 2.); feclearexcept(FE_UNDERFLOW); errno = 0; v = nextafter(1., 2.); printf("\nnextafter(1., 2.) = %a\n", v); printf("is subnormal: %d\n", fpclassify(v) == FP_SUBNORMAL); printf("errno = %d\n", errno); if(fetestexcept(FE_UNDERFLOW)) printf("FE_UNDERFLOW\n"); } _________ ERANGE = 34 nextafter(0., 1.) = 0x0.0000000000001p-1022 is subnormal: 1 errno = 0 FE_UNDERFLOW nextafter(DBL_MIN_SUBNORMAL, 1.) = 0x0.0000000000002p-1022 is subnormal: 1 errno = 34 FE_UNDERFLOW nextafter(DBL_MIN_SUBNORMAL, 0.) = 0x0p+0 is subnormal: 0 errno = 34 FE_UNDERFLOW nextafter(DBL_MIN, 0.) = 0x0.fffffffffffffp-1022 is subnormal: 1 errno = 34 FE_UNDERFLOW nextafter(1., 2.) = 0x1.0000000000001p+0 is subnormal: 0 errno = 0
errno is only intented to be set on underflow when the result is zero, as explained in commit 85422c2acba83852396c9d9fd22ff0493e3606fe.
I see. In this case the man page that can currently be read at https://man7.org/linux/man-pages/man3/nextafter.3.html is wrong, since it describes the behavior as: If x is not equal to y, and the correct function result would be subnormal, zero, or underflow, a range error occurs, and either the correct value (if it can be represented), or 0.0, is returned. But this of course is not Glibc's fault (chuckle).