Skip to content

Commit bcdaa07

Browse files
authored
10826 use cmp and abs to find larger NaN payload (#10895)
`getNaNPayload(x)` returns a value even if `x` is not NaN, so it is up to the caller to verify that `x` is a NaN before calling `getNaNPayload(x)`. This means it is not a good option for determining for determining which of a set of values has the largest NaN payload when it is only known that at least one value is NaN. `cmp(x, y)` can be used. since to orders -NaN as before -infinity and NaN as after infinity. When two NaN with the same sign are compared, the one with the larger payload is considered larger. This means that `cmp(abs(x), abx(y))` will find the NaN with the larger payload when it is only known that at least one of `x` or `y` is NaN.
1 parent b0bb590 commit bcdaa07

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

std/internal/math/gammafunction.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ real beta(in real x, in real y)
656656
{
657657
// When one of the input parameters is NaN, return the NaN with the larger
658658
// payload. This mimics the behavior of the + operator.
659-
if (isNaN(x) || isNaN(y)) return getNaNPayload(x) >= getNaNPayload(y) ? x : y;
659+
if (isNaN(x) || isNaN(y)) return cmp(abs(x), abs(y)) >= 0 ? x : y;
660660

661661
real res;
662662

@@ -764,7 +764,7 @@ real beta(in real x, in real y)
764764
@safe unittest
765765
{
766766
// Test NaN payload propagation
767-
assert(isIdentical(beta(NaN(0xABC), 2), NaN(0xABC)));
767+
assert(isIdentical(beta(NaN(0x1), 7), NaN(0x1)));
768768
assert(isIdentical(beta(2, NaN(0xABC)), NaN(0xABC)));
769769
assert(isIdentical(beta(NaN(0x1), NaN(0x2)), NaN(0x2)));
770770

0 commit comments

Comments
 (0)