From 879241d819fd69cb6a75f3b615a6787c8006cd20 Mon Sep 17 00:00:00 2001 From: "Charles L. Dorian" Date: Tue, 18 May 2010 22:16:01 -0700 Subject: [PATCH] math: signed zero Sqrt special case IEEE 754 says: sqrt(-0) = -0 R=rsc CC=golang-dev https://golang.org/cl/1098041 --- src/pkg/math/all_test.go | 11 +++++++++-- src/pkg/math/sqrt.go | 1 + src/pkg/math/sqrt_port.go | 6 ++---- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/pkg/math/all_test.go b/src/pkg/math/all_test.go index a653555a2b..af4d886352 100644 --- a/src/pkg/math/all_test.go +++ b/src/pkg/math/all_test.go @@ -1335,12 +1335,16 @@ var sinhSC = []float64{ var vfsqrtSC = []float64{ Inf(-1), -Pi, + -1 / Inf(1), // -0 + 0, Inf(1), NaN(), } var sqrtSC = []float64{ NaN(), NaN(), + -1 / Inf(1), // -0 + 0, Inf(1), NaN(), } @@ -2018,7 +2022,7 @@ func TestSqrt(t *testing.T) { for i := 0; i < len(vf); i++ { a := Fabs(vf[i]) if f := SqrtGo(a); sqrt[i] != f { - t.Errorf("sqrtGo(%g) = %g, want %g\n", a, f, sqrt[i]) + t.Errorf("SqrtGo(%g) = %g, want %g\n", a, f, sqrt[i]) } a = Fabs(vf[i]) if f := Sqrt(a); sqrt[i] != f { @@ -2026,7 +2030,10 @@ func TestSqrt(t *testing.T) { } } for i := 0; i < len(vfsqrtSC); i++ { - if f := Log10(vfsqrtSC[i]); !alike(sqrtSC[i], f) { + if f := SqrtGo(vfsqrtSC[i]); !alike(sqrtSC[i], f) { + t.Errorf("SqrtGo(%g) = %g, want %g\n", vfsqrtSC[i], f, sqrtSC[i]) + } + if f := Sqrt(vfsqrtSC[i]); !alike(sqrtSC[i], f) { t.Errorf("Sqrt(%g) = %g, want %g\n", vfsqrtSC[i], f, sqrtSC[i]) } } diff --git a/src/pkg/math/sqrt.go b/src/pkg/math/sqrt.go index e6bc4680b8..ff5cc91e08 100644 --- a/src/pkg/math/sqrt.go +++ b/src/pkg/math/sqrt.go @@ -8,6 +8,7 @@ package math // // Special cases are: // Sqrt(+Inf) = +Inf +// Sqrt(±0) = ±0 // Sqrt(x < 0) = NaN // Sqrt(NaN) = NaN func Sqrt(x float64) float64 { return sqrtGo(x) } diff --git a/src/pkg/math/sqrt_port.go b/src/pkg/math/sqrt_port.go index c818834e7f..8d821b559b 100644 --- a/src/pkg/math/sqrt_port.go +++ b/src/pkg/math/sqrt_port.go @@ -90,7 +90,7 @@ package math // // Special cases are: // Sqrt(+Inf) = +Inf -// Sqrt(0) = 0 +// Sqrt(±0) = ±0 // Sqrt(x < 0) = NaN // Sqrt(NaN) = NaN func sqrtGo(x float64) float64 { @@ -98,10 +98,8 @@ func sqrtGo(x float64) float64 { // TODO(rsc): Remove manual inlining of IsNaN, IsInf // when compiler does it for us switch { - case x != x || x > MaxFloat64: // IsNaN(x) || IsInf(x, 1): + case x == 0 || x != x || x > MaxFloat64: // x == 0 || IsNaN(x) || IsInf(x, 1): return x - case x == 0: - return 0 case x < 0: return NaN() } -- 2.48.1