Commit 4b265fb7 authored by Wèi Cōngruì's avatar Wèi Cōngruì Committed by Robert Griesemer

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 <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
parent 9967582f
...@@ -1967,6 +1967,8 @@ var vfldexpBC = []fi{ ...@@ -1967,6 +1967,8 @@ var vfldexpBC = []fi{
{-1, -1075}, {-1, -1075},
{1, 1024}, {1, 1024},
{-1, 1024}, {-1, 1024},
{1.0000000000000002, -1075},
{1, -1075},
} }
var ldexpBC = []float64{ var ldexpBC = []float64{
SmallestNonzeroFloat64, SmallestNonzeroFloat64,
...@@ -1977,6 +1979,8 @@ var ldexpBC = []float64{ ...@@ -1977,6 +1979,8 @@ var ldexpBC = []float64{
Copysign(0, -1), Copysign(0, -1),
Inf(1), Inf(1),
Inf(-1), Inf(-1),
SmallestNonzeroFloat64,
0,
} }
var logbBC = []float64{ var logbBC = []float64{
......
...@@ -25,7 +25,7 @@ func ldexp(frac float64, exp int) float64 { ...@@ -25,7 +25,7 @@ func ldexp(frac float64, exp int) float64 {
exp += e exp += e
x := Float64bits(frac) x := Float64bits(frac)
exp += int(x>>shift)&mask - bias exp += int(x>>shift)&mask - bias
if exp < -1074 { if exp < -1075 {
return Copysign(0, frac) // underflow return Copysign(0, frac) // underflow
} }
if exp > 1023 { // overflow if exp > 1023 { // overflow
...@@ -36,8 +36,8 @@ func ldexp(frac float64, exp int) float64 { ...@@ -36,8 +36,8 @@ func ldexp(frac float64, exp int) float64 {
} }
var m float64 = 1 var m float64 = 1
if exp < -1022 { // denormal if exp < -1022 { // denormal
exp += 52 exp += 53
m = 1.0 / (1 << 52) // 2**-52 m = 1.0 / (1 << 53) // 2**-53
} }
x &^= mask << shift x &^= mask << shift
x |= uint64(exp+bias) << shift x |= uint64(exp+bias) << shift
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment