summaryrefslogtreecommitdiff
path: root/src/pkg/math
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2009-12-15 17:21:01 -0800
committerRuss Cox <rsc@golang.org>2009-12-15 17:21:01 -0800
commite7eda89e94417da96c6c9c311680ad0fde40b849 (patch)
treef272b3d5a9758e6a487bab25edd77d6ca2818975 /src/pkg/math
parent95c2d520268b58e378ddd3cb5ec4b3899a908b2f (diff)
downloadgolang-e7eda89e94417da96c6c9c311680ad0fde40b849.tar.gz
math: faster, easier to inline IsNaN, IsInf
R=r CC=golang-dev http://codereview.appspot.com/180046
Diffstat (limited to 'src/pkg/math')
-rw-r--r--src/pkg/math/bits.go14
1 files changed, 10 insertions, 4 deletions
diff --git a/src/pkg/math/bits.go b/src/pkg/math/bits.go
index 65eabfd60..9f93a192b 100644
--- a/src/pkg/math/bits.go
+++ b/src/pkg/math/bits.go
@@ -29,8 +29,11 @@ func NaN() float64 { return Float64frombits(uvnan) }
// IsNaN returns whether f is an IEEE 754 ``not-a-number'' value.
func IsNaN(f float64) (is bool) {
- x := Float64bits(f)
- return uint32(x>>shift)&mask == mask && x != uvinf && x != uvneginf
+ // IEEE 754 says that only NaNs satisfy f != f.
+ // To avoid the floating-point hardware, could use:
+ // x := Float64bits(f);
+ // return uint32(x>>shift)&mask == mask && x != uvinf && x != uvneginf
+ return f != f
}
// IsInf returns whether f is an infinity, according to sign.
@@ -38,8 +41,11 @@ func IsNaN(f float64) (is bool) {
// If sign < 0, IsInf returns whether f is negative infinity.
// If sign == 0, IsInf returns whether f is either infinity.
func IsInf(f float64, sign int) bool {
- x := Float64bits(f)
- return sign >= 0 && x == uvinf || sign <= 0 && x == uvneginf
+ // Test for infinity by comparing against maximum float.
+ // To avoid the floating-point hardware, could use:
+ // x := Float64bits(f);
+ // return sign >= 0 && x == uvinf || sign <= 0 && x == uvneginf;
+ return sign >= 0 && f > MaxFloat64 || sign <= 0 && f < -MaxFloat64
}
// Frexp breaks f into a normalized fraction