question about nextafter

Paul Zimmermann Paul.Zimmermann@inria.fr
Tue Mar 4 08:31:20 GMT 2025


       Hi,

the man page of nextafter says:

       double nextafter(double x, 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.

However it does not seem a range error occurs (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;
  errno = 0;
  feclearexcept (FE_UNDERFLOW);
  double z = nextafter (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: 16
z=0x0.0000000000001p-1022

Did I miss something?

Paul

PS: I do not see how it can happen that the correct value cannot be
represented when the correct function result would be subnormal.
If x = k*2^-1074 for k integer and y > x, then the correct value is
(k+1)*2^-1074, which is always representable if subnormal, no?







More information about the Libc-alpha mailing list