]> Cypherpunks repositories - gostls13.git/commitdiff
math: signed zero Sqrt special case
authorCharles L. Dorian <cldorian@gmail.com>
Wed, 19 May 2010 05:16:01 +0000 (22:16 -0700)
committerRuss Cox <rsc@golang.org>
Wed, 19 May 2010 05:16:01 +0000 (22:16 -0700)
IEEE 754 says: sqrt(-0) = -0

R=rsc
CC=golang-dev
https://golang.org/cl/1098041

src/pkg/math/all_test.go
src/pkg/math/sqrt.go
src/pkg/math/sqrt_port.go

index a653555a2be4e38d48a665efdf02c911e74a461e..af4d88635245132065aa970dae94534d4e6db515 100644 (file)
@@ -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])
                }
        }
index e6bc4680b8dd8b366731c9993465dd74a9b8d21e..ff5cc91e08a86f6e74c48f4ac11ecb040a3c98e7 100644 (file)
@@ -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) }
index c818834e7f4c2e74bc6801d39cd4ef48f4f35742..8d821b559b64f095e22501e2ca896fece48ae3b3 100644 (file)
@@ -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()
        }