diff options
author | Szabolcs Nagy <nsz@port70.net> | 2017-04-03 02:38:13 +0200 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2017-04-21 17:31:00 -0400 |
commit | 8c44a060243f04283ca68dad199aab90336141db (patch) | |
tree | 5fd8fd700ac059c1ef2f2999c5783207c91bcf59 /src/math/scalbn.c | |
parent | 2577b1bc16124d0690b9dd268a9f582f80bdcd67 (diff) | |
download | musl-8c44a060243f04283ca68dad199aab90336141db.tar.gz musl-8c44a060243f04283ca68dad199aab90336141db.tar.bz2 musl-8c44a060243f04283ca68dad199aab90336141db.tar.xz musl-8c44a060243f04283ca68dad199aab90336141db.zip |
fix scalbn when result is in the subnormal range
in nearest rounding mode scalbn could introduce double rounding error
when an intermediate value and the final result were both in the
subnormal range e.g.
scalbn(0x1.7ffffffffffffp-1, -1073)
returned 0x1p-1073 instead of 0x1p-1074, because the intermediate
computation got rounded to 0x1.8p-1023.
with the fix an intermediate value can only be in the subnormal range
if the final result is 0 which is correct even after double rounding.
(there still can be two roundings so signals may be raised twice, but
that's only observable with trapping exceptions which is not supported.)
Diffstat (limited to 'src/math/scalbn.c')
-rw-r--r-- | src/math/scalbn.c | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/src/math/scalbn.c b/src/math/scalbn.c index 530e07c7..182f5610 100644 --- a/src/math/scalbn.c +++ b/src/math/scalbn.c @@ -16,11 +16,13 @@ double scalbn(double x, int n) n = 1023; } } else if (n < -1022) { - y *= 0x1p-1022; - n += 1022; + /* make sure final n < -53 to avoid double + rounding in the subnormal range */ + y *= 0x1p-1022 * 0x1p53; + n += 1022 - 53; if (n < -1022) { - y *= 0x1p-1022; - n += 1022; + y *= 0x1p-1022 * 0x1p53; + n += 1022 - 53; if (n < -1022) n = -1022; } |