Commit 6cd8b74
Fix expm1l returning NaN for large finite arguments (#306)
For arguments just below the overflow threshold, expm1l reconstructed the
result as 2^k * qx + (2^k - 1) with k == LDBL_MAX_EXP (16384). ldexpl(1, k)
then overflowed to +Inf, and the subsequent (+Inf) + (-Inf) (qx is negative
when the reduced remainder is negative) produced NaN, even though the true
result is finite and near LDBL_MAX.
When k is large enough that 2^k overflows, instead form exp(remainder ln 2)
= qx + 1 (which lies near 1) and scale its exponent directly via scalbnl, so
no intermediate value overflows. This is algebraically identical to the
existing formula; the normal path is unchanged.
Result now matches glibc bit-for-bit on x86-64 (80-bit long double), and
slightly larger arguments still overflow to +Inf as before. Adds
regression test test-306.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent aeef4da commit 6cd8b74
2 files changed
Lines changed: 56 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| 60 | + | |
| 61 | + | |
60 | 62 | | |
61 | 63 | | |
62 | 64 | | |
| |||
131 | 133 | | |
132 | 134 | | |
133 | 135 | | |
134 | | - | |
135 | | - | |
136 | | - | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
137 | 148 | | |
138 | 149 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
0 commit comments