1
0
mirror of https://github.com/golang/go synced 2024-11-20 03:54:40 -07:00

math: signed zero Sqrt special case

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

R=rsc
CC=golang-dev
https://golang.org/cl/1098041
This commit is contained in:
Charles L. Dorian 2010-05-18 22:16:01 -07:00 committed by Russ Cox
parent 90d0c3316c
commit 879241d819
3 changed files with 12 additions and 6 deletions

View 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])
}
}

View 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) }

View 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()
}