www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20542] New: std.math.nextafter(NaN, y) and nextafter(x, NaN)

https://issues.dlang.org/show_bug.cgi?id=20542

          Issue ID: 20542
           Summary: std.math.nextafter(NaN, y) and nextafter(x, NaN)
                    should return NaN
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: electrolysis.jp gmail.com

Currently the result of nextafter(x, y) against NaN is undefined at least in
the document. Actually, although nextafter(NaN, y) returns NaN as a result of
nextDown(NaN), nextafter(x, NaN) returns nextDown(x), as below:

void main()
{
    import std;

    auto a = 4.0;
    writefln("%a", a); // 0x1p+2
    writefln("%a", nextafter(a.nan, a)); // nan, it's fine.
    writefln("%a", nextafter(a, a.nan)); // 0x1.fffffffffffffp+1, but should be
nan.
}

On POSIX, the man page of nextafter(3) describes ambiguously:
 If x or y is NaN, a NaN shall be returned.
http://man7.org/linux/man-pages/man3/nextafter.3p.html On the other hand, the Linux version says:
 If x or y is a NaN, a NaN is returned.
http://man7.org/linux/man-pages/man3/nextafter.3.html Similarly, Microsoft says:
 If either x or y is a NAN, then the return value is one of the input NANs.
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/nextafter-functions?view=vs-2019 --
Jan 28