Skip to content

Commit bed6326

Browse files
committed
math: fix Log2 test failures on ppc64 (and s390)
- Make Log2 exact for powers of two. - Fix error tolerance function to make tolerance a function of the correct (expected) value. Fixes #9066. Change-Id: I0320a93ce4130deed1c7b7685627d51acb7bc56d Reviewed-on: https://go-review.googlesource.com/12230 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 671bddf commit bed6326

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/math/all_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,8 +1730,10 @@ func tolerance(a, b, e float64) bool {
17301730
d = -d
17311731
}
17321732

1733-
if a != 0 {
1734-
e = e * a
1733+
// note: b is correct (expected) value, a is actual value.
1734+
// make error tolerance a fraction of b, not a.
1735+
if b != 0 {
1736+
e = e * b
17351737
if e < 0 {
17361738
e = -e
17371739
}

src/math/log10.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,10 @@ func Log2(x float64) float64
1818

1919
func log2(x float64) float64 {
2020
frac, exp := Frexp(x)
21+
// Make sure exact powers of two give an exact answer.
22+
// Don't depend on Log(0.5)*(1/Ln2)+exp being exactly exp-1.
23+
if frac == 0.5 {
24+
return float64(exp - 1)
25+
}
2126
return Log(frac)*(1/Ln2) + float64(exp)
2227
}

0 commit comments

Comments
 (0)