diff --git a/src/pkg/math/all_test.go b/src/pkg/math/all_test.go index d2a7d411ec0..1fe4513c183 100644 --- a/src/pkg/math/all_test.go +++ b/src/pkg/math/all_test.go @@ -1359,6 +1359,20 @@ var powSC = []float64{ NaN(), // pow(NaN, NaN) } +var vfpow10SC = []int{ + MinInt32, + MaxInt32, + -325, + 309, +} + +var pow10SC = []float64{ + 0, // pow10(MinInt32) + Inf(1), // pow10(MaxInt32) + 0, // pow10(-325) + Inf(1), // pow10(309) +} + var vfsignbitSC = []float64{ Inf(-1), Copysign(0, -1), @@ -2143,6 +2157,14 @@ func TestPow(t *testing.T) { } } +func TestPow10(t *testing.T) { + for i := 0; i < len(vfpow10SC); i++ { + if f := Pow10(vfpow10SC[i]); !alike(pow10SC[i], f) { + t.Errorf("Pow10(%d) = %g, want %g", vfpow10SC[i], f, pow10SC[i]) + } + } +} + func TestRemainder(t *testing.T) { for i := 0; i < len(vf); i++ { if f := Remainder(10, vf[i]); remainder[i] != f { @@ -2659,6 +2681,18 @@ func BenchmarkPowFrac(b *testing.B) { } } +func BenchmarkPow10Pos(b *testing.B) { + for i := 0; i < b.N; i++ { + Pow10(300) + } +} + +func BenchmarkPow10Neg(b *testing.B) { + for i := 0; i < b.N; i++ { + Pow10(-300) + } +} + func BenchmarkRemainder(b *testing.B) { for i := 0; i < b.N; i++ { Remainder(10, 3) diff --git a/src/pkg/math/pow10.go b/src/pkg/math/pow10.go index bda2e824ef1..20f91bcb70c 100644 --- a/src/pkg/math/pow10.go +++ b/src/pkg/math/pow10.go @@ -10,6 +10,12 @@ var pow10tab [70]float64 // Pow10 returns 10**e, the base-10 exponential of e. func Pow10(e int) float64 { + if e <= -325 { + return 0 + } else if e > 309 { + return Inf(1) + } + if e < 0 { return 1 / Pow10(-e) }