diff options
author | Charles L. Dorian <cldorian@gmail.com> | 2010-06-30 23:34:33 -0700 |
---|---|---|
committer | Charles L. Dorian <cldorian@gmail.com> | 2010-06-30 23:34:33 -0700 |
commit | 24bbb4db940f47782887040a2fd985adb0f04634 (patch) | |
tree | 45bf61b34a01855fafa99eacc35ef36b339a5fa3 | |
parent | 8d6f97bb91a23b0dcde75e887e956d4d5b9b0e4b (diff) | |
download | golang-24bbb4db940f47782887040a2fd985adb0f04634.tar.gz |
cmath: correct IsNaN for argument cmplx(Inf, NaN)
R=rsc
CC=golang-dev
http://codereview.appspot.com/1705041
Committer: Russ Cox <rsc@golang.org>
-rw-r--r-- | src/pkg/cmath/cmath_test.go | 36 | ||||
-rw-r--r-- | src/pkg/cmath/isnan.go | 8 |
2 files changed, 38 insertions, 6 deletions
diff --git a/src/pkg/cmath/cmath_test.go b/src/pkg/cmath/cmath_test.go index 44706d362..25e4f2254 100644 --- a/src/pkg/cmath/cmath_test.go +++ b/src/pkg/cmath/cmath_test.go @@ -38,8 +38,7 @@ var vc = []complex128{ // at http://keisan.casio.com/. More exact input values (array vc[], above) // were obtained by printing them with "%.26f". The answers were calculated // to 26 digits (by using the "Digit number" drop-down control of each -// calculator). Twenty-six digits were chosen so that the answers would be -// accurate even for a float128 type. +// calculator). var abs = []float64{ 9.2022120669932650313380972e+00, @@ -355,6 +354,28 @@ var vcExpSC = []complex128{ var expSC = []complex128{ NaN(), } +var vcIsNaNSC = []complex128{ + cmplx(math.Inf(-1), math.Inf(-1)), + cmplx(math.Inf(-1), math.NaN()), + cmplx(math.NaN(), math.Inf(-1)), + cmplx(0, math.NaN()), + cmplx(math.NaN(), 0), + cmplx(math.Inf(1), math.Inf(1)), + cmplx(math.Inf(1), math.NaN()), + cmplx(math.NaN(), math.Inf(1)), + cmplx(math.NaN(), math.NaN()), +} +var isNaNSC = []bool{ + false, + false, + false, + true, + true, + false, + false, + false, + true, +} var vcLogSC = []complex128{ NaN(), } @@ -432,7 +453,7 @@ func alike(a, b float64) bool { case a != a && b != b: // math.IsNaN(a) && math.IsNaN(b): return true case a == b: - return true + return math.Signbit(a) == math.Signbit(b) } return false } @@ -454,7 +475,7 @@ func cAlike(a, b complex128) bool { case IsNaN(a) && IsNaN(b): return true case a == b: - return true + return math.Signbit(real(a)) == math.Signbit(real(b)) && math.Signbit(imag(a)) == math.Signbit(imag(b)) } return false } @@ -591,6 +612,13 @@ func TestExp(t *testing.T) { } } } +func TestIsNaN(t *testing.T) { + for i := 0; i < len(vcIsNaNSC); i++ { + if f := IsNaN(vcIsNaNSC[i]); isNaNSC[i] != f { + t.Errorf("IsNaN(%g) = %g, want %g\n", vcIsNaNSC[i], f, isNaNSC[i]) + } + } +} func TestLog(t *testing.T) { for i := 0; i < len(vc); i++ { if f := Log(vc[i]); !cVeryclose(log[i], f) { diff --git a/src/pkg/cmath/isnan.go b/src/pkg/cmath/isnan.go index 29b760135..8e971dbd3 100644 --- a/src/pkg/cmath/isnan.go +++ b/src/pkg/cmath/isnan.go @@ -6,9 +6,13 @@ package cmath import "math" -// IsNaN returns true if either real(x) or imag(x) is NaN. +// IsNaN returns true if either real(x) or imag(x) is NaN +// and neither is an infinity. func IsNaN(x complex128) bool { - if math.IsNaN(real(x)) || math.IsNaN(imag(x)) { + switch { + case math.IsInf(real(x), 0) || math.IsInf(imag(x), 0): + return false + case math.IsNaN(real(x)) || math.IsNaN(imag(x)): return true } return false |