Bug 30040 - nextafter(0.0, 1.0) does not correctly set errno
Summary: nextafter(0.0, 1.0) does not correctly set errno
Status: UNCONFIRMED
Alias: None
Product: glibc
Classification: Unclassified
Component: math (show other bugs)
Version: 2.38
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-01-24 10:47 UTC by Pascal Cuoq
Modified: 2023-03-08 17:43 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Pascal Cuoq 2023-01-24 10:47:33 UTC
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
Comment 1 Andreas Schwab 2023-03-08 17:23:58 UTC
errno is only intented to be set on underflow when the result is zero, as explained in commit 85422c2acba83852396c9d9fd22ff0493e3606fe.
Comment 2 Pascal Cuoq 2023-03-08 17:43:02 UTC
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).