Math package for float32 / compex64 types
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

frexp.go 655B

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