Commit 879241d8 authored by Charles L. Dorian's avatar Charles L. Dorian Committed by Russ Cox

math: signed zero Sqrt special case

IEEE 754 says: sqrt(-0) = -0

R=rsc
CC=golang-dev
https://golang.org/cl/1098041
parent 90d0c331
...@@ -1335,12 +1335,16 @@ var sinhSC = []float64{ ...@@ -1335,12 +1335,16 @@ var sinhSC = []float64{
var vfsqrtSC = []float64{ var vfsqrtSC = []float64{
Inf(-1), Inf(-1),
-Pi, -Pi,
-1 / Inf(1), // -0
0,
Inf(1), Inf(1),
NaN(), NaN(),
} }
var sqrtSC = []float64{ var sqrtSC = []float64{
NaN(), NaN(),
NaN(), NaN(),
-1 / Inf(1), // -0
0,
Inf(1), Inf(1),
NaN(), NaN(),
} }
...@@ -2018,7 +2022,7 @@ func TestSqrt(t *testing.T) { ...@@ -2018,7 +2022,7 @@ func TestSqrt(t *testing.T) {
for i := 0; i < len(vf); i++ { for i := 0; i < len(vf); i++ {
a := Fabs(vf[i]) a := Fabs(vf[i])
if f := SqrtGo(a); sqrt[i] != f { 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]) a = Fabs(vf[i])
if f := Sqrt(a); sqrt[i] != f { if f := Sqrt(a); sqrt[i] != f {
...@@ -2026,7 +2030,10 @@ func TestSqrt(t *testing.T) { ...@@ -2026,7 +2030,10 @@ func TestSqrt(t *testing.T) {
} }
} }
for i := 0; i < len(vfsqrtSC); i++ { 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]) t.Errorf("Sqrt(%g) = %g, want %g\n", vfsqrtSC[i], f, sqrtSC[i])
} }
} }
......
...@@ -8,6 +8,7 @@ package math ...@@ -8,6 +8,7 @@ package math
// //
// Special cases are: // Special cases are:
// Sqrt(+Inf) = +Inf // Sqrt(+Inf) = +Inf
// Sqrt(±0) = ±0
// Sqrt(x < 0) = NaN // Sqrt(x < 0) = NaN
// Sqrt(NaN) = NaN // Sqrt(NaN) = NaN
func Sqrt(x float64) float64 { return sqrtGo(x) } func Sqrt(x float64) float64 { return sqrtGo(x) }
...@@ -90,7 +90,7 @@ package math ...@@ -90,7 +90,7 @@ package math
// //
// Special cases are: // Special cases are:
// Sqrt(+Inf) = +Inf // Sqrt(+Inf) = +Inf
// Sqrt(0) = 0 // Sqrt(±0) = ±0
// Sqrt(x < 0) = NaN // Sqrt(x < 0) = NaN
// Sqrt(NaN) = NaN // Sqrt(NaN) = NaN
func sqrtGo(x float64) float64 { func sqrtGo(x float64) float64 {
...@@ -98,10 +98,8 @@ func sqrtGo(x float64) float64 { ...@@ -98,10 +98,8 @@ func sqrtGo(x float64) float64 {
// TODO(rsc): Remove manual inlining of IsNaN, IsInf // TODO(rsc): Remove manual inlining of IsNaN, IsInf
// when compiler does it for us // when compiler does it for us
switch { 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 return x
case x == 0:
return 0
case x < 0: case x < 0:
return NaN() return NaN()
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment