modf.go 706 B

1234567891011121314151617181920212223242526272829303132333435
  1. package math32
  2. // Modf returns integer and fractional floating-point numbers
  3. // that sum to f. Both values have the same sign as f.
  4. //
  5. // Special cases are:
  6. // Modf(±Inf) = ±Inf, NaN
  7. // Modf(NaN) = NaN, NaN
  8. func Modf(f float32) (int float32, frac float32) {
  9. return modf(f)
  10. }
  11. func modf(f float32) (int float32, frac float32) {
  12. if f < 1 {
  13. switch {
  14. case f < 0:
  15. int, frac = Modf(-f)
  16. return -int, -frac
  17. case f == 0:
  18. return f, f // Return -0, -0 when f == -0
  19. }
  20. return 0, f
  21. }
  22. x := Float32bits(f)
  23. e := uint(x>>shift)&mask - bias
  24. // Keep the top 9+e bits, the integer part; clear the rest.
  25. if e < 32-9 {
  26. x &^= 1<<(32-9-e) - 1
  27. }
  28. int = Float32frombits(x)
  29. frac = f - int
  30. return
  31. }