D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20542 - std.math.nextafter(NaN, y) and nextafter(x, NaN) should return NaN
Summary: std.math.nextafter(NaN, y) and nextafter(x, NaN) should return NaN
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2020-01-28 15:57 UTC by e10s
Modified: 2020-01-31 03:25 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description e10s 2020-01-28 15:57:32 UTC
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
Comment 1 Dlang Bot 2020-01-28 16:20:53 UTC
@e10s created dlang/phobos pull request #7380 "Fix Issue 20542 - std.math.nextafter(NaN, y) and nextafter(x, NaN) sh…" fixing this issue:

- Fix Issue 20542 - std.math.nextafter(NaN, y) and nextafter(x, NaN) should return NaN

https://github.com/dlang/phobos/pull/7380
Comment 2 Dlang Bot 2020-01-31 03:25:08 UTC
dlang/phobos pull request #7380 "Fix Issue 20542 - std.math.nextafter(NaN, y) and nextafter(x, NaN) sh…" was merged into master:

- 2d726d2dc3217c98f0cec7088a04ccb19e6f6346 by Kazuya Takahashi:
  Fix Issue 20542 - std.math.nextafter(NaN, y) and nextafter(x, NaN) should return NaN

https://github.com/dlang/phobos/pull/7380