dim.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package math32
  2. // Dim returns the maximum of x-y or 0.
  3. //
  4. // Special cases are:
  5. //
  6. // Dim(+Inf, +Inf) = NaN
  7. // Dim(-Inf, -Inf) = NaN
  8. // Dim(x, NaN) = Dim(NaN, x) = NaN
  9. func Dim(x, y float32) float32 {
  10. return dim(x, y)
  11. }
  12. func dim(x, y float32) float32 {
  13. return max(x-y, 0)
  14. }
  15. // Max returns the larger of x or y.
  16. //
  17. // Special cases are:
  18. //
  19. // Max(x, +Inf) = Max(+Inf, x) = +Inf
  20. // Max(x, NaN) = Max(NaN, x) = NaN
  21. // Max(+0, ±0) = Max(±0, +0) = +0
  22. // Max(-0, -0) = -0
  23. func Max(x, y float32) float32 {
  24. return max(x, y)
  25. }
  26. func max(x, y float32) float32 {
  27. // special cases
  28. switch {
  29. case IsInf(x, 1) || IsInf(y, 1):
  30. return Inf(1)
  31. case IsNaN(x) || IsNaN(y):
  32. return NaN()
  33. case x == 0 && x == y:
  34. if Signbit(x) {
  35. return y
  36. }
  37. return x
  38. }
  39. if x > y {
  40. return x
  41. }
  42. return y
  43. }
  44. // Min returns the smaller of x or y.
  45. //
  46. // Special cases are:
  47. //
  48. // Min(x, -Inf) = Min(-Inf, x) = -Inf
  49. // Min(x, NaN) = Min(NaN, x) = NaN
  50. // Min(-0, ±0) = Min(±0, -0) = -0
  51. func Min(x, y float32) float32 {
  52. return min(x, y)
  53. }
  54. func min(x, y float32) float32 {
  55. // special cases
  56. switch {
  57. case IsInf(x, -1) || IsInf(y, -1):
  58. return Inf(-1)
  59. case IsNaN(x) || IsNaN(y):
  60. return NaN()
  61. case x == 0 && x == y:
  62. if Signbit(x) {
  63. return x
  64. }
  65. return y
  66. }
  67. if x < y {
  68. return x
  69. }
  70. return y
  71. }