[cfp-interest 3399] nexttoward
Paul Zimmermann
Paul.Zimmermann at inria.fr
Mon Mar 3 09:57:29 PST 2025
Hi,
I have a question about nexttoward(). The Linux man page says:
double nexttoward(double x, long double y);
...
If x is not equal to y, and the correct function result would be subnor‐
mal, zero, or underflow, a range error occurs, and either the correct
value (if it can be represented), or 0.0, is returned.
For example nexttoward (0.0, 1.0) will set errno = ERANGE.
I wonder why this is so, since for mathematical functions, underflow is not
raised (and thus errno is not set to ERANGE) when the result is exact.
And nexttoward always delivers an exact result.
Paul
PS: moreover the GNU libc does not seem to follow the man page, it seems
errno is not set, but underflow is raised:
$ cat e.c
#include <stdio.h>
#include <errno.h>
#include <math.h>
#include <fenv.h>
int
main()
{
double x = 0.0, y = 1.0;
printf ("errno=%d\n", errno);
printf ("underflow: %d\n", fetestexcept (FE_UNDERFLOW));
double z = nexttoward (x, y);
printf ("errno=%d\n", errno);
printf ("underflow: %d\n", fetestexcept (FE_UNDERFLOW));
printf ("z=%la\n", z);
}
$ gcc -fno-builtin e.c -lm && ./a.out
errno=0
underflow: 0
errno=0
underflow: 16
z=0x0.0000000000001p-1022
More information about the cfp-interest
mailing list