aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles L. Dorian <cldorian@gmail.com>2010-09-28 10:15:21 -0400
committerRuss Cox <rsc@golang.org>2010-09-28 10:15:21 -0400
commitb233ac8f18a668a2d91299ebc2996e7e2b623b29 (patch)
tree65a705e3a3d25556b8053bc1a49fe2c900a8bb4d
parent0e66a13d10856b2d7c8df1c82112d75746f10725 (diff)
downloadgo-b233ac8f18a668a2d91299ebc2996e7e2b623b29.tar.gz
go-b233ac8f18a668a2d91299ebc2996e7e2b623b29.zip
math: Fix off-by-one error in Ilogb and Logb.
Fixes #1141. R=rsc CC=adg, golang-dev https://golang.org/cl/2194047
-rw-r--r--src/pkg/math/all_test.go17
-rw-r--r--src/pkg/math/logb.go8
2 files changed, 13 insertions, 12 deletions
diff --git a/src/pkg/math/all_test.go b/src/pkg/math/all_test.go
index 485f6e54c8..8a60bca01d 100644
--- a/src/pkg/math/all_test.go
+++ b/src/pkg/math/all_test.go
@@ -383,16 +383,16 @@ var log = []float64{
2.161703872847352815363655e+00,
}
var logb = []float64{
- 3.0000000000000000e+00,
- 3.0000000000000000e+00,
- -1.0000000000000000e+00,
- 3.0000000000000000e+00,
- 4.0000000000000000e+00,
+ 2.0000000000000000e+00,
+ 2.0000000000000000e+00,
+ -2.0000000000000000e+00,
2.0000000000000000e+00,
3.0000000000000000e+00,
+ 1.0000000000000000e+00,
2.0000000000000000e+00,
1.0000000000000000e+00,
- 4.0000000000000000e+00,
+ 0.0000000000000000e+00,
+ 3.0000000000000000e+00,
}
var log10 = []float64{
6.9714316642508290997617083e-01,
@@ -1806,8 +1806,9 @@ func TestHypot(t *testing.T) {
func TestIlogb(t *testing.T) {
for i := 0; i < len(vf); i++ {
- if e := Ilogb(vf[i]); frexp[i].i != e {
- t.Errorf("Ilogb(%g) = %d, want %d", vf[i], e, frexp[i].i)
+ a := frexp[i].i - 1 // adjust because fr in the interval [½, 1)
+ if e := Ilogb(vf[i]); a != e {
+ t.Errorf("Ilogb(%g) = %d, want %d", vf[i], e, a)
}
}
for i := 0; i < len(vflogbSC); i++ {
diff --git a/src/pkg/math/logb.go b/src/pkg/math/logb.go
index acda15d226..22ec06325d 100644
--- a/src/pkg/math/logb.go
+++ b/src/pkg/math/logb.go
@@ -4,7 +4,7 @@
package math
-// Logb(x) returns the binary logarithm of non-zero x.
+// Logb(x) returns the binary exponent of non-zero x.
//
// Special cases are:
// Logb(±Inf) = +Inf
@@ -22,10 +22,10 @@ func Logb(x float64) float64 {
case x != x: // IsNaN(x):
return x
}
- return float64(int((Float64bits(x)>>shift)&mask) - bias)
+ return float64(int((Float64bits(x)>>shift)&mask) - (bias + 1))
}
-// Ilogb(x) returns the binary logarithm of non-zero x as an integer.
+// Ilogb(x) returns the binary exponent of non-zero x as an integer.
//
// Special cases are:
// Ilogb(±Inf) = MaxInt32
@@ -43,5 +43,5 @@ func Ilogb(x float64) int {
case x < -MaxFloat64 || x > MaxFloat64: // IsInf(x, 0):
return MaxInt32
}
- return int((Float64bits(x)>>shift)&mask) - bias
+ return int((Float64bits(x)>>shift)&mask) - (bias + 1)
}