diff options
Diffstat (limited to 'libgo/go/math/all_test.go')
-rw-r--r-- | libgo/go/math/all_test.go | 145 |
1 files changed, 118 insertions, 27 deletions
diff --git a/libgo/go/math/all_test.go b/libgo/go/math/all_test.go index d9ea1fdb51a..3d8cd7223de 100644 --- a/libgo/go/math/all_test.go +++ b/libgo/go/math/all_test.go @@ -1165,21 +1165,88 @@ var frexpSC = []fi{ {NaN(), 0}, } -var vfgammaSC = []float64{ - Inf(-1), - -3, - Copysign(0, -1), - 0, - Inf(1), - NaN(), -} -var gammaSC = []float64{ - NaN(), - NaN(), - Inf(-1), - Inf(1), - Inf(1), - NaN(), +var vfgamma = [][2]float64{ + {Inf(1), Inf(1)}, + {Inf(-1), NaN()}, + {0, Inf(1)}, + {Copysign(0, -1), Inf(-1)}, + {NaN(), NaN()}, + {-1, NaN()}, + {-2, NaN()}, + {-3, NaN()}, + {-1e16, NaN()}, + {-1e300, NaN()}, + {1.7e308, Inf(1)}, + + // Test inputs inspired by Python test suite. + // Outputs computed at high precision by PARI/GP. + // If recomputing table entries, be careful to use + // high-precision (%.1000g) formatting of the float64 inputs. + // For example, -2.0000000000000004 is the float64 with exact value + // -2.00000000000000044408920985626161695, and + // gamma(-2.0000000000000004) = -1249999999999999.5386078562728167651513, while + // gamma(-2.00000000000000044408920985626161695) = -1125899906826907.2044875028130093136826. + // Thus the table lists -1.1258999068426235e+15 as the answer. + {0.5, 1.772453850905516}, + {1.5, 0.886226925452758}, + {2.5, 1.329340388179137}, + {3.5, 3.3233509704478426}, + {-0.5, -3.544907701811032}, + {-1.5, 2.363271801207355}, + {-2.5, -0.9453087204829419}, + {-3.5, 0.2700882058522691}, + {0.1, 9.51350769866873}, + {0.01, 99.4325851191506}, + {1e-08, 9.999999942278434e+07}, + {1e-16, 1e+16}, + {0.001, 999.4237724845955}, + {1e-16, 1e+16}, + {1e-308, 1e+308}, + {5.6e-309, 1.7857142857142864e+308}, + {5.5e-309, Inf(1)}, + {1e-309, Inf(1)}, + {1e-323, Inf(1)}, + {5e-324, Inf(1)}, + {-0.1, -10.686287021193193}, + {-0.01, -100.58719796441078}, + {-1e-08, -1.0000000057721567e+08}, + {-1e-16, -1e+16}, + {-0.001, -1000.5782056293586}, + {-1e-16, -1e+16}, + {-1e-308, -1e+308}, + {-5.6e-309, -1.7857142857142864e+308}, + {-5.5e-309, Inf(-1)}, + {-1e-309, Inf(-1)}, + {-1e-323, Inf(-1)}, + {-5e-324, Inf(-1)}, + {-0.9999999999999999, -9.007199254740992e+15}, + {-1.0000000000000002, 4.5035996273704955e+15}, + {-1.9999999999999998, 2.2517998136852485e+15}, + {-2.0000000000000004, -1.1258999068426235e+15}, + {-100.00000000000001, -7.540083334883109e-145}, + {-99.99999999999999, 7.540083334884096e-145}, + {17, 2.0922789888e+13}, + {171, 7.257415615307999e+306}, + {171.6, 1.5858969096672565e+308}, + {171.624, 1.7942117599248104e+308}, + {171.625, Inf(1)}, + {172, Inf(1)}, + {2000, Inf(1)}, + {-100.5, -3.3536908198076787e-159}, + {-160.5, -5.255546447007829e-286}, + {-170.5, -3.3127395215386074e-308}, + {-171.5, 1.9316265431712e-310}, + {-176.5, -1.196e-321}, + {-177.5, 5e-324}, + {-178.5, Copysign(0, -1)}, + {-179.5, 0}, + {-201.0001, 0}, + {-202.9999, Copysign(0, -1)}, + {-1000.5, Copysign(0, -1)}, + {-1.0000000003e+09, Copysign(0, -1)}, + {-4.5035996273704955e+15, 0}, + {-63.349078729022985, 4.177797167776188e-88}, + {-127.45117632943295, 1.183111089623681e-214}, } var vfhypotSC = [][2]float64{ @@ -1735,6 +1802,12 @@ var logbBC = []float64{ } func tolerance(a, b, e float64) bool { + // Multiplying by e here can underflow denormal values to zero. + // Check a==b so that at least if a and b are small and identical + // we say they match. + if a == b { + return true + } d := a - b if d < 0 { d = -d @@ -1974,7 +2047,7 @@ func TestExp(t *testing.T) { func testExp(t *testing.T, Exp func(float64) float64, name string) { for i := 0; i < len(vf); i++ { - if f := Exp(vf[i]); !close(exp[i], f) { + if f := Exp(vf[i]); !veryclose(exp[i], f) { t.Errorf("%s(%g) = %g, want %g", name, vf[i], f, exp[i]) } } @@ -2147,9 +2220,18 @@ func TestGamma(t *testing.T) { t.Errorf("Gamma(%g) = %g, want %g", vf[i], f, gamma[i]) } } - for i := 0; i < len(vfgammaSC); i++ { - if f := Gamma(vfgammaSC[i]); !alike(gammaSC[i], f) { - t.Errorf("Gamma(%g) = %g, want %g", vfgammaSC[i], f, gammaSC[i]) + for _, g := range vfgamma { + f := Gamma(g[0]) + var ok bool + if IsNaN(g[1]) || IsInf(g[1], 0) || g[1] == 0 || f == 0 { + ok = alike(g[1], f) + } else if g[0] > -50 && g[0] <= 171 { + ok = veryclose(g[1], f) + } else { + ok = close(g[1], f) + } + if !ok { + t.Errorf("Gamma(%g) = %g, want %g", g[0], f, g[1]) } } } @@ -3000,27 +3082,36 @@ func BenchmarkSinh(b *testing.B) { var Global float64 -func BenchmarkSqrt(b *testing.B) { +func BenchmarkSqrtIndirect(b *testing.B) { x, y := 0.0, 10.0 + f := Sqrt for i := 0; i < b.N; i++ { - x += Sqrt(y) + x += f(y) } Global = x } -func BenchmarkSqrtIndirect(b *testing.B) { - x, y := 0.0, 10.0 +func BenchmarkSqrtLatency(b *testing.B) { + x := 10.0 + for i := 0; i < b.N; i++ { + x = Sqrt(x) + } + Global = x +} + +func BenchmarkSqrtIndirectLatency(b *testing.B) { + x := 10.0 f := Sqrt for i := 0; i < b.N; i++ { - x += f(y) + x = f(x) } Global = x } -func BenchmarkSqrtGo(b *testing.B) { - x, y := 0.0, 10.0 +func BenchmarkSqrtGoLatency(b *testing.B) { + x := 10.0 for i := 0; i < b.N; i++ { - x += SqrtGo(y) + x = SqrtGo(x) } Global = x } |