math.monkey2 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. Namespace monkey.math
  2. Using monkey.types
  3. #rem monkeydoc The value _Pi_.
  4. #end
  5. Const Pi:Double=3.1415926535897931
  6. #rem monkeydoc The value _Pi_ times 2.
  7. #end
  8. Const TwoPi:Double=6.2831853071795862
  9. Extern
  10. #rem monkeydoc Computes the sine of an angle.
  11. @param `x` the angle, in radians.
  12. @return The sine of `x`.
  13. #end
  14. Function Sin:Double( x:Double )="std::sin"
  15. #rem monkeydoc Computes the cosine of an angle.
  16. @param `x` the angle, in radians.
  17. @return The cosine of `x`.
  18. #end
  19. Function Cos:Double( x:Double )="std::cos"
  20. #rem monkeydoc Computes the tangent of an angle.
  21. @param `x` the angle, in radians.
  22. @return The tangent of `x`.
  23. #end
  24. Function Tan:Double( x:Double )="std::tan"
  25. #rem monkeydoc Computes the inverse sine of a number.
  26. @param `x` The number.
  27. @return The inverse sine of `x`, in radians.
  28. #end
  29. Function ASin:Double( x:Double )="std::asin"
  30. #rem monkeydoc Computes the inverse cosine of a number.
  31. @param `x` The number.
  32. @return The inverse cosine of `x`, in radians.
  33. #end
  34. Function ACos:Double( x:Double )="std::acos"
  35. #rem monkeydoc Computes the inverse tagent of a number.
  36. @param `x` The number.
  37. @return The inverse tangent of `x`, in radians.
  38. #end
  39. Function ATan:Double( x:Double )="std::atan"
  40. #rem monkeydoc Computes the inverse tangent of a ratio.
  41. The function uses the signs of `x` and `y` to compute the correct sign for the result.
  42. @param `x` The numerator.
  43. @param `y` The denominator.
  44. @return The inverse tangent of `x`/`y`, in radians.
  45. #end
  46. Function ATan2:Double( x:Double,y:Double )="std::atan2"
  47. #rem monkeydoc Computes the square root of a number.
  48. @param `x` The number.
  49. @return The square root of `x`.
  50. #end
  51. Function Sqrt:Double( x:Double )="std::sqrt"
  52. #rem monkeydoc Computes the floor of a number.
  53. @param `x` The number.
  54. @return The largest integral value not greater than `x`.
  55. #end
  56. Function Floor:Double( x:Double )="std::floor"
  57. #rem monkeydoc Computes the ceiling of a number.
  58. @param `x` The number.
  59. @return The smallest integral value not less than `x`.
  60. #end
  61. Function Ceil:Double( x:Double )="std::ceil"
  62. #rem monkeydoc Rounds a number to the nearest integral value.
  63. @param `x` The number.
  64. @return The integral value nearest to `x`.
  65. #end
  66. Function Round:Double( x:Double )="std::round"
  67. #rem monkeydoc Raises a number to a power.
  68. @param `x` The number.
  69. @return `x` raised to the power of `y`.
  70. #end
  71. Function Pow:Double( x:Double,y:Double )="std::pow"
  72. #rem monkeydoc Computes the natural logarithm of a number.
  73. @param `x` The number.
  74. @return The natural logarithm of `x`.
  75. #end
  76. Function Log:Double( x:Double )="std::log"
  77. #rem monkeydoc Computes the base 2 logarithm of a number.
  78. @param `x` The number.
  79. @return The base 2 logarithm of `x`.
  80. #end
  81. Function Log2:Double( x:Double )="std::log2"
  82. #rem monkeydoc Computes the base 10 logarithm of a number.
  83. @param `x` The number.
  84. @return The base 10 logarithm of `x`.
  85. #end
  86. Function Log10:Double( x:Double )="std::log10"
  87. #rem monkeydoc Raise _e_ to a power.
  88. @param `x` The number.
  89. @return The value _e_ raised to the power of `x`.
  90. #end
  91. Function Exp:Double( x:Double )="std::exp"
  92. Public
  93. #rem monkeydoc Gets the smaller of two numbers.
  94. @return The smaller of `x` and `y`.
  95. #end
  96. Function Min<T>:T( x:T,y:T )
  97. If x<=y Return x
  98. Return y
  99. End
  100. #rem monkeydoc Gets the larger of two number.
  101. @return The larger of `x` and `y`.
  102. #end
  103. Function Max<T>:T( x:T,y:T )
  104. If x>=y Return x
  105. Return y
  106. End
  107. #rem monkeydoc Clamps a value to a range.
  108. If `x` is less than `min`, `min` is returned.
  109. If `x` is greater than `max`, `max` is returned.
  110. Otherwise, `x` is returned.
  111. @return `x` clamped to the range [`min`,`max`].
  112. #end
  113. Function Clamp<T>:T( value:T,min:T,max:T )
  114. If value<=min Return min
  115. If value>=max Return max
  116. Return value
  117. End
  118. #rem monkeydoc Gets the absolute value of a number.
  119. If `x` is less than 0, then `-x` is returned.
  120. If `x` is greater than or equal to 0, then `x` is returned.
  121. @return The absolute value of `x`.
  122. #end
  123. Function Abs<T>:T( x:T ) Where T Implements INumeric
  124. If x>=0 Return x
  125. Return -x
  126. End
  127. #rem monkeydoc Gets the sign of a number.
  128. Returns -1 is `x` less than 0, 1 if `x` is greater than 0 or 0 if `x` is equal to 0.
  129. @return The sign of `x`.
  130. #end
  131. Function Sgn<T>:Int( x:T ) Where T Implements IIntegral
  132. If x<0 Return -1
  133. If x>0 Return 1
  134. Return 0
  135. End
  136. Function Sgn<T>:Double( x:T ) Where T Implements IReal
  137. If x<0 Return -1
  138. If x>0 Return 1
  139. Return 0
  140. End