}
}
-func TestHypotSqrtGo(t *testing.T) {
+func TestHypotGo(t *testing.T) {
for i := 0; i < len(vf); i++ {
a := Abs(1e200 * tanh[i] * Sqrt(2))
- if f := HypotSqrtGo(1e200*tanh[i], 1e200*tanh[i]); !veryclose(a, f) {
- t.Errorf("HypotSqrtGo(%g, %g) = %g, want %g", 1e200*tanh[i], 1e200*tanh[i], f, a)
+ if f := HypotGo(1e200*tanh[i], 1e200*tanh[i]); !veryclose(a, f) {
+ t.Errorf("HypotGo(%g, %g) = %g, want %g", 1e200*tanh[i], 1e200*tanh[i], f, a)
}
}
for i := 0; i < len(vfhypotSC); i++ {
- if f := HypotSqrtGo(vfhypotSC[i][0], vfhypotSC[i][1]); !alike(hypotSC[i], f) {
- t.Errorf("HypotSqrtGo(%g, %g) = %g, want %g", vfhypotSC[i][0], vfhypotSC[i][1], f, hypotSC[i])
- }
- }
-}
-
-func TestHypotNoSqrtGo(t *testing.T) {
- for i := 0; i < len(vf); i++ {
- a := Abs(1e200 * tanh[i] * Sqrt(2))
- if f := HypotNoSqrtGo(1e200*tanh[i], 1e200*tanh[i]); !veryclose(a, f) {
- t.Errorf("HypotNoSqrtGo(%g, %g) = %g, want %g", 1e200*tanh[i], 1e200*tanh[i], f, a)
- }
- }
- for i := 0; i < len(vfhypotSC); i++ {
- if f := HypotNoSqrtGo(vfhypotSC[i][0], vfhypotSC[i][1]); !alike(hypotSC[i], f) {
- t.Errorf("HypotNoSqrtGo(%g, %g) = %g, want %g", vfhypotSC[i][0], vfhypotSC[i][1], f, hypotSC[i])
+ if f := HypotGo(vfhypotSC[i][0], vfhypotSC[i][1]); !alike(hypotSC[i], f) {
+ t.Errorf("HypotGo(%g, %g) = %g, want %g", vfhypotSC[i][0], vfhypotSC[i][1], f, hypotSC[i])
}
}
}
}
}
-func BenchmarkHypotNoSqrtGo(b *testing.B) {
- for i := 0; i < b.N; i++ {
- HypotNoSqrtGo(3, 4)
- }
-}
-
-func BenchmarkHypotSqrtGo(b *testing.B) {
+func BenchmarkHypotGo(b *testing.B) {
for i := 0; i < b.N; i++ {
- HypotSqrtGo(3, 4)
+ HypotGo(3, 4)
}
}
// Hypot(p, q) = NaN if p or q is NaN
func Hypot(p, q float64) float64
-func hypotSqrt(p, q float64) float64 {
+func hypot(p, q float64) float64 {
// TODO(rsc): Remove manual inlining of IsNaN, IsInf
// when compiler does it for us
// special cases
q = q / p
return p * Sqrt(1+q*q)
}
-
-func hypotNoSqrt(p, q float64) float64 {
- // TODO(rsc): Remove manual inlining of IsNaN, IsInf
- // when compiler does it for us
- // special cases
- switch {
- case p < -MaxFloat64 || p > MaxFloat64 || q < -MaxFloat64 || q > MaxFloat64: // IsInf(p, 0) || IsInf(q, 0):
- return Inf(1)
- case p != p || q != q: // IsNaN(p) || IsNaN(q):
- return NaN()
- }
- if p < 0 {
- p = -p
- }
- if q < 0 {
- q = -q
- }
-
- if p < q {
- p, q = q, p
- }
-
- if p == 0 {
- return 0
- }
-
- pfac := p
- q = q / p
- r := q
- p = 1
- for {
- r = r * r
- s := r + 4
- if s == 4 {
- return p * pfac
- }
- r = r / s
- p = p + 2*r*p
- q = q * r
- r = q / p
- }
- panic("unreachable")
-}