1
0
mirror of https://github.com/golang/go synced 2024-11-22 08:44:41 -07:00

math: faster, easier to inline IsNaN, IsInf

R=r
CC=golang-dev
https://golang.org/cl/180046
This commit is contained in:
Russ Cox 2009-12-15 17:21:01 -08:00
parent d16bc7a9f2
commit 1e9e7ec4b3

View File

@ -29,8 +29,11 @@ func NaN() float64 { return Float64frombits(uvnan) }
// IsNaN returns whether f is an IEEE 754 ``not-a-number'' value. // IsNaN returns whether f is an IEEE 754 ``not-a-number'' value.
func IsNaN(f float64) (is bool) { func IsNaN(f float64) (is bool) {
x := Float64bits(f) // IEEE 754 says that only NaNs satisfy f != f.
return uint32(x>>shift)&mask == mask && x != uvinf && x != uvneginf // 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. // 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 negative infinity.
// If sign == 0, IsInf returns whether f is either infinity. // If sign == 0, IsInf returns whether f is either infinity.
func IsInf(f float64, sign int) bool { func IsInf(f float64, sign int) bool {
x := Float64bits(f) // Test for infinity by comparing against maximum float.
return sign >= 0 && x == uvinf || sign <= 0 && x == uvneginf // 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 // Frexp breaks f into a normalized fraction