math: special cases for Hypot
Added special case tests to all_test.go. Added tests to hypot.go,
otherwise hangs.
R=rsc
CC=golang-dev
https://golang.org/cl/186118
diff --git a/src/pkg/math/all_test.go b/src/pkg/math/all_test.go
index 0c65dd7..15d289b 100644
--- a/src/pkg/math/all_test.go
+++ b/src/pkg/math/all_test.go
@@ -309,6 +309,41 @@
NaN(),
}
+var vfhypotSC = [][2]float64{
+ [2]float64{Inf(-1), Inf(-1)},
+ [2]float64{Inf(-1), 0},
+ [2]float64{Inf(-1), Inf(1)},
+ [2]float64{Inf(-1), NaN()},
+ [2]float64{0, Inf(-1)},
+ [2]float64{0, Inf(1)},
+ [2]float64{0, NaN()},
+ [2]float64{Inf(1), Inf(-1)},
+ [2]float64{Inf(1), 0},
+ [2]float64{Inf(1), Inf(1)},
+ [2]float64{Inf(1), NaN()},
+ [2]float64{NaN(), Inf(-1)},
+ [2]float64{NaN(), 0},
+ [2]float64{NaN(), Inf(1)},
+ [2]float64{NaN(), NaN()},
+}
+var hypotSC = []float64{
+ Inf(1),
+ Inf(1),
+ Inf(1),
+ Inf(1),
+ Inf(1),
+ Inf(1),
+ NaN(),
+ Inf(1),
+ Inf(1),
+ Inf(1),
+ Inf(1),
+ Inf(1),
+ NaN(),
+ Inf(1),
+ NaN(),
+}
+
var vflogSC = []float64{
Inf(-1),
-Pi,
@@ -413,6 +448,19 @@
1,
}
+var vfsqrtSC = []float64{
+ Inf(-1),
+ -Pi,
+ Inf(1),
+ NaN(),
+}
+var sqrtSC = []float64{
+ NaN(),
+ NaN(),
+ Inf(1),
+ NaN(),
+}
+
func tolerance(a, b, e float64) bool {
d := a - b
if d < 0 {
@@ -525,12 +573,26 @@
func TestFmod(t *testing.T) {
for i := 0; i < len(vf); i++ {
if f := Fmod(10, vf[i]); !close(fmod[i], f) {
- t.Errorf("Fmod(10, %.17g) = %.17g, want %.17g\n", vf[i], f, fmod[i])
+ t.Errorf("Fmod(10, %g) = %g, want %g\n", vf[i], f, fmod[i])
}
}
for i := 0; i < len(vffmodSC); i++ {
if f := Fmod(vffmodSC[i][0], vffmodSC[i][1]); !alike(fmodSC[i], f) {
- t.Errorf("Fmod(%.17g, %.17g) = %.17g, want %.17g\n", vffmodSC[i][0], vffmodSC[i][1], f, fmodSC[i])
+ t.Errorf("Fmod(%g, %g) = %g, want %g\n", vffmodSC[i][0], vffmodSC[i][1], f, fmodSC[i])
+ }
+ }
+}
+
+func TestHypot(t *testing.T) {
+ for i := 0; i < len(vf); i++ {
+ a := Fabs(tanh[i] * Sqrt(2))
+ if f := Hypot(tanh[i], tanh[i]); a != f {
+ t.Errorf("Hypot(%g, %g) = %g, want %g\n", tanh[i], tanh[i], f, a)
+ }
+ }
+ for i := 0; i < len(vfhypotSC); i++ {
+ if f := Hypot(vfhypotSC[i][0], vfhypotSC[i][1]); !alike(hypotSC[i], f) {
+ t.Errorf("Hypot(%g, %g) = %g, want %g\n", vfhypotSC[i][0], vfhypotSC[i][1], f, hypotSC[i])
}
}
}
@@ -572,12 +634,12 @@
func TestPow(t *testing.T) {
for i := 0; i < len(vf); i++ {
if f := Pow(10, vf[i]); !close(pow[i], f) {
- t.Errorf("Pow(10, %.17g) = %.17g, want %.17g\n", vf[i], f, pow[i])
+ t.Errorf("Pow(10, %g) = %g, want %g\n", vf[i], f, pow[i])
}
}
for i := 0; i < len(vfpowSC); i++ {
if f := Pow(vfpowSC[i][0], vfpowSC[i][1]); !alike(powSC[i], f) {
- t.Errorf("Pow(%.17g, %.17g) = %.17g, want %.17g\n", vfpowSC[i][0], vfpowSC[i][1], f, powSC[i])
+ t.Errorf("Pow(%g, %g) = %g, want %g\n", vfpowSC[i][0], vfpowSC[i][1], f, powSC[i])
}
}
}
@@ -609,6 +671,11 @@
t.Errorf("Sqrt(%g) = %g, want %g\n", a, f, sqrt[i])
}
}
+ for i := 0; i < len(vfsqrtSC); i++ {
+ if f := Log10(vfsqrtSC[i]); !alike(sqrtSC[i], f) {
+ t.Errorf("Sqrt(%g) = %g, want %g\n", vfsqrtSC[i], f, sqrtSC[i])
+ }
+ }
}
func TestTan(t *testing.T) {
@@ -627,15 +694,6 @@
}
}
-func TestHypot(t *testing.T) {
- for i := 0; i < len(vf); i++ {
- a := Fabs(tanh[i] * Sqrt(2))
- if f := Hypot(tanh[i], tanh[i]); a != f {
- t.Errorf("Hypot(%g, %g) = %g, want %g\n", tanh[i], tanh[i], f, a)
- }
- }
-}
-
// Check that math functions of high angle values
// return similar results to low angle values
func TestLargeSin(t *testing.T) {
@@ -700,18 +758,6 @@
// Benchmarks
-func BenchmarkPowInt(b *testing.B) {
- for i := 0; i < b.N; i++ {
- Pow(2, 2)
- }
-}
-
-func BenchmarkPowFrac(b *testing.B) {
- for i := 0; i < b.N; i++ {
- Pow(2.5, 1.5)
- }
-}
-
func BenchmarkAtan(b *testing.B) {
for i := 0; i < b.N; i++ {
Atan(.5)
@@ -730,6 +776,18 @@
}
}
+func BenchmarkPowInt(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ Pow(2, 2)
+ }
+}
+
+func BenchmarkPowFrac(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ Pow(2.5, 1.5)
+ }
+}
+
func BenchmarkSqrt(b *testing.B) {
for i := 0; i < b.N; i++ {
Sqrt(10)