math.monkey2 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. Returns the inverse tangent of `y`/`x`, using the signs of the arguments to determine the quadrant of the result.
  42. @param `y` The numerator.
  43. @param `z` The denominator.
  44. @return The inverse tangent of `y`/`x`, in radians.
  45. #end
  46. Function ATan2:Double( y:Double,x: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. #if __TARGET__="android"
  67. Public
  68. Function Round:Double( x:Double )
  69. Return Floor( x+.5 )
  70. End
  71. Extern
  72. #else
  73. Function Round:Double( x:Double )="std::round"
  74. #endif
  75. #rem monkeydoc Raises a number to a power.
  76. @param `x` The number.
  77. @return `x` raised to the power of `y`.
  78. #end
  79. Function Pow:Double( x:Double,y:Double )="std::pow"
  80. #rem monkeydoc Computes the natural logarithm of a number.
  81. @param `x` The number.
  82. @return The natural logarithm of `x`.
  83. #end
  84. Function Log:Double( x:Double )="std::log"
  85. #rem monkeydoc Computes the base 2 logarithm of a number.
  86. @param `x` The number.
  87. @return The base 2 logarithm of `x`.
  88. #end
  89. #if __TARGET__="android"
  90. Public
  91. Function Log2:Double( x:Double )
  92. Return Log(x)/Log(2)
  93. End
  94. Extern
  95. #else
  96. Function Log2:Double( x:Double )="std::log2"
  97. #endif
  98. #rem monkeydoc Computes the base 10 logarithm of a number.
  99. @param `x` The number.
  100. @return The base 10 logarithm of `x`.
  101. #end
  102. Function Log10:Double( x:Double )="std::log10"
  103. #rem monkeydoc Raise _e_ to a power.
  104. @param `x` The number.
  105. @return The value _e_ raised to the power of `x`.
  106. #end
  107. Function Exp:Double( x:Double )="std::exp"
  108. Public
  109. #rem monkeydoc Gets the smaller of two numbers.
  110. @return The smaller of `x` and `y`.
  111. #end
  112. Function Min<T>:T( x:T,y:T )
  113. If x<=y Return x
  114. Return y
  115. End
  116. #rem monkeydoc Gets the larger of two number.
  117. @return The larger of `x` and `y`.
  118. #end
  119. Function Max<T>:T( x:T,y:T )
  120. If x>=y Return x
  121. Return y
  122. End
  123. #rem monkeydoc Clamps a value to a range.
  124. If `x` is less than `min`, `min` is returned.
  125. If `x` is greater than `max`, `max` is returned.
  126. Otherwise, `x` is returned.
  127. @return `x` clamped to the range [`min`,`max`].
  128. #end
  129. Function Clamp<T>:T( value:T,min:T,max:T )
  130. If value<=min Return min
  131. If value>=max Return max
  132. Return value
  133. End
  134. #rem monkeydoc Gets the absolute value of a number.
  135. If `x` is less than 0, then `-x` is returned.
  136. If `x` is greater than or equal to 0, then `x` is returned.
  137. @return The absolute value of `x`.
  138. #end
  139. Function Abs<T>:T( x:T ) Where T Implements INumeric
  140. If x>=0 Return x
  141. Return -x
  142. End
  143. #rem monkeydoc Gets the sign of a number.
  144. Returns -1 is `x` less than 0, 1 if `x` is greater than 0 or 0 if `x` is equal to 0.
  145. @return The sign of `x`.
  146. #end
  147. Function Sgn<T>:Int( x:T ) Where T Implements IIntegral
  148. If x<0 Return -1
  149. If x>0 Return 1
  150. Return 0
  151. End
  152. Function Sgn<T>:Double( x:T ) Where T Implements IReal
  153. If x<0 Return -1
  154. If x>0 Return 1
  155. Return 0
  156. End