gitee.com/quant1x/num@v0.3.2/math32/bits.go (about) 1 package math32 2 3 const ( 4 uvnan = 0x7FE00000 5 uvinf = 0x7F800000 6 uvneginf = 0xFF800000 7 mask = 0xFF 8 shift = 32 - 8 - 1 9 bias = 127 10 signMask = 1 << 31 11 fracMask = 1<<shift - 1 12 ) 13 14 // Inf returns positive infinity if sign >= 0, negative infinity if sign < 0. 15 func Inf(sign int) float32 { 16 var v uint32 17 if sign >= 0 { 18 v = uvinf 19 } else { 20 v = uvneginf 21 } 22 return Float32frombits(v) 23 } 24 25 // NaN returns an IEEE 754 “not-a-number” value. 26 func NaN() float32 { return Float32frombits(uvnan) } 27 28 // IsNaN reports whether f is an IEEE 754 “not-a-number” value. 29 func IsNaN(f float32) (is bool) { 30 // IEEE 754 says that only NaNs satisfy f != f. 31 // To avoid the floating-point hardware, could use: 32 // x := Float32bits(f) 33 // return uint32(x>>shift)&mask == mask && x != uvinf && x != uvneginf 34 return f != f 35 } 36 37 // IsInf reports whether f is an infinity, according to sign. 38 // If sign > 0, IsInf reports whether f is positive infinity. 39 // If sign < 0, IsInf reports whether f is negative infinity. 40 // If sign == 0, IsInf reports whether f is either infinity. 41 func IsInf(f float32, sign int) bool { 42 // Test for infinity by comparing against maximum float. 43 // To avoid the floating-point hardware, could use: 44 // x := Float32bits(f) 45 // return sign >= 0 && x == uvinf || sign <= 0 && x == uvneginf 46 return sign >= 0 && f > MaxFloat32 || sign <= 0 && f < -MaxFloat32 47 } 48 49 // normalize returns a normal number y and exponent exp 50 // satisfying x == y × 2**exp. It assumes x is finite and non-zero. 51 func normalize(x float32) (y float32, exp int) { 52 const SmallestNormal = 1.1754943508222875079687365e-38 // 2**-(127 - 1) 53 if Abs(x) < SmallestNormal { 54 return x * (1 << shift), -shift 55 } 56 return x, 0 57 }