floor.go 983 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package math32
  2. // Floor returns the greatest integer value less than or equal to x.
  3. //
  4. // Special cases are:
  5. // Floor(±0) = ±0
  6. // Floor(±Inf) = ±Inf
  7. // Floor(NaN) = NaN
  8. func Floor(x float32) float32 {
  9. return floor(x)
  10. }
  11. func floor(x float32) float32 {
  12. if x == 0 || IsNaN(x) || IsInf(x, 0) {
  13. return x
  14. }
  15. if x < 0 {
  16. d, fract := Modf(-x)
  17. if fract != 0.0 {
  18. d = d + 1
  19. }
  20. return -d
  21. }
  22. d, _ := Modf(x)
  23. return d
  24. }
  25. // Ceil returns the least integer value greater than or equal to x.
  26. //
  27. // Special cases are:
  28. // Ceil(±0) = ±0
  29. // Ceil(±Inf) = ±Inf
  30. // Ceil(NaN) = NaN
  31. func Ceil(x float32) float32 {
  32. return ceil(x)
  33. }
  34. func ceil(x float32) float32 {
  35. return -Floor(-x)
  36. }
  37. // Trunc returns the integer value of x.
  38. //
  39. // Special cases are:
  40. // Trunc(±0) = ±0
  41. // Trunc(±Inf) = ±Inf
  42. // Trunc(NaN) = NaN
  43. func Trunc(x float32) float32 {
  44. return trunc(x)
  45. }
  46. func trunc(x float32) float32 {
  47. if x == 0 || IsNaN(x) || IsInf(x, 0) {
  48. return x
  49. }
  50. d, _ := Modf(x)
  51. return d
  52. }