Skip to content

Commit 4b265fb

Browse files
crvvgriesemer
authored andcommitted
math: fix Ldexp when result is below ldexp(2, -1075)
Before this change, the smallest result Ldexp can handle was ldexp(2, -1075), which is SmallestNonzeroFloat64. There are some numbers below it should also be rounded to SmallestNonzeroFloat64. The change fixes this. Fixes #23407 Change-Id: I76f4cb005a6e9ccdd95b5e5c734079fd5d29e4aa Reviewed-on: https://go-review.googlesource.com/87338 Run-TryBot: Robert Griesemer <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 9967582 commit 4b265fb

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/math/all_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,6 +1967,8 @@ var vfldexpBC = []fi{
19671967
{-1, -1075},
19681968
{1, 1024},
19691969
{-1, 1024},
1970+
{1.0000000000000002, -1075},
1971+
{1, -1075},
19701972
}
19711973
var ldexpBC = []float64{
19721974
SmallestNonzeroFloat64,
@@ -1977,6 +1979,8 @@ var ldexpBC = []float64{
19771979
Copysign(0, -1),
19781980
Inf(1),
19791981
Inf(-1),
1982+
SmallestNonzeroFloat64,
1983+
0,
19801984
}
19811985

19821986
var logbBC = []float64{

src/math/ldexp.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func ldexp(frac float64, exp int) float64 {
2525
exp += e
2626
x := Float64bits(frac)
2727
exp += int(x>>shift)&mask - bias
28-
if exp < -1074 {
28+
if exp < -1075 {
2929
return Copysign(0, frac) // underflow
3030
}
3131
if exp > 1023 { // overflow
@@ -36,8 +36,8 @@ func ldexp(frac float64, exp int) float64 {
3636
}
3737
var m float64 = 1
3838
if exp < -1022 { // denormal
39-
exp += 52
40-
m = 1.0 / (1 << 52) // 2**-52
39+
exp += 53
40+
m = 1.0 / (1 << 53) // 2**-53
4141
}
4242
x &^= mask << shift
4343
x |= uint64(exp+bias) << shift

0 commit comments

Comments
 (0)