gitee.com/quant1x/num@v0.3.2/math32/frexp.go (about) 1 package math32 2 3 // Frexp breaks f into a normalized fraction 4 // and an integral power of two. 5 // It returns frac and exp satisfying f == frac × 2**exp, 6 // with the absolute value of frac in the interval [½, 1). 7 // 8 // Special cases are: 9 // 10 // Frexp(±0) = ±0, 0 11 // Frexp(±Inf) = ±Inf, 0 12 // Frexp(NaN) = NaN, 0 13 func Frexp(f float32) (frac float32, exp int) { 14 return frexp(f) 15 } 16 17 func frexp(f float32) (frac float32, exp int) { 18 // special cases 19 switch { 20 case f == 0: 21 return f, 0 // correctly return -0 22 case IsInf(f, 0) || IsNaN(f): 23 return f, 0 24 } 25 f, exp = normalize(f) 26 x := Float32bits(f) 27 exp += int((x>>shift)&mask) - bias + 1 28 x &^= mask << shift 29 x |= (-1 + bias) << shift 30 frac = Float32frombits(x) 31 return 32 }