Math.hx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (C)2005-2016 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. /**
  23. This class defines mathematical functions and constants.
  24. @see http://haxe.org/manual/std-math.html
  25. **/
  26. #if cpp @:include("hxMath.h") #end
  27. extern class Math
  28. {
  29. /**
  30. Represents the ratio of the circumference of a circle to its diameter,
  31. specified by the constant, π. `PI` is approximately 3.141592653589793.
  32. **/
  33. static var PI(default,null) : Float;
  34. /**
  35. A special `Float` constant which denotes negative infinity.
  36. For example, this is the result of -1.0 / 0.0.
  37. Operations with `NEGATIVE_INFINITY` as an operand may result in
  38. `NEGATIVE_INFINITY`, `POSITIVE_INFINITY` or `NaN`.
  39. If this constant is converted to an `Int`, e.g. through `Std.int()`, the
  40. result is unspecified.
  41. **/
  42. static var NEGATIVE_INFINITY(default, null) : Float;
  43. /**
  44. A special `Float` constant which denotes negative infinity.
  45. For example, this is the result of 1.0 / 0.0.
  46. Operations with `POSITIVE_INFINITY` as an operand may result in
  47. `NEGATIVE_INFINITY`, `POSITIVE_INFINITY` or `NaN`.
  48. If this constant is converted to an `Int`, e.g. through `Std.int()`, the
  49. result is unspecified.
  50. **/
  51. static var POSITIVE_INFINITY(default,null) : Float;
  52. /**
  53. A special `Float` constant which denotes an invalid number.
  54. NaN stands for "Not a Number". It occurs when a mathematically incorrect
  55. operation is executed, such as taking the square root of a negative
  56. number: Math.sqrt(-1).
  57. All further operations with `NaN` as an operand will result in `NaN`.
  58. If this constant is converted to an `Int`, e.g. through `Std.int()`, the
  59. result is unspecified.
  60. In order to test if a value is `NaN`, you should use `Math.isNaN()` function.
  61. @php In PHP versions prior to 5.3.1 VC 9 there may be unexpected
  62. results when performing arithmetic operations with `NaN` on Windows,
  63. see <https://bugs.php.net/bug.php?id=42143>
  64. **/
  65. static var NaN(default, null) : Float;
  66. /**
  67. Returns the absolute value of `v`.
  68. If `v` is positive or 0, the result is unchanged. Otherwise the result
  69. is -`v`.
  70. If `v` is `NEGATIVE_INFINITY` or `POSITIVE_INFINITY`, the result is
  71. `POSITIVE_INFINITY`.
  72. If `v` is `NaN`, the result is `NaN`.
  73. **/
  74. static function abs(v:Float):Float;
  75. /**
  76. Returns the smaller of values `a` and `b`.
  77. If `a` or `b` are `NaN`, the result is `NaN`.
  78. If `a` or `b` are `NEGATIVE_INFINITY`, the result is `NEGATIVE_INFINITY`.
  79. If `a` and `b` are `POSITIVE_INFINITY`, the result is `POSITIVE_INFINITY`.
  80. **/
  81. static function min(a:Float, b:Float):Float;
  82. /**
  83. Returns the greater of values `a` and `b`.
  84. If `a` or `b` are `NaN`, the result is `NaN`.
  85. If `a` or `b` are `POSITIVE_INFINITY`, the result is `POSITIVE_INFINITY`.
  86. If `a` and `b` are `NEGATIVE_INFINITY`, the result is `NEGATIVE_INFINITY`.
  87. **/
  88. static function max(a:Float, b:Float):Float;
  89. /**
  90. Returns the trigonometric sine of the specified angle `v`, in radians.
  91. If `v` is `NaN` or infinite, the result is `NaN`.
  92. **/
  93. static function sin(v:Float):Float;
  94. /**
  95. Returns the trigonometric cosine of the specified angle `v`, in radians.
  96. If `v` is `NaN` or infinite, the result is `NaN`.
  97. **/
  98. static function cos(v:Float):Float;
  99. /**
  100. Returns the trigonometric tangent of the specified angle `v`, in radians.
  101. If `v` is `NaN` or infinite, the result is `NaN`.
  102. **/
  103. static function tan(v:Float):Float;
  104. /**
  105. Returns the trigonometric arc of the specified angle `v`, in radians.
  106. If `v` is `NaN` or infinite, the result is `NaN`.
  107. **/
  108. static function asin(v:Float):Float;
  109. /**
  110. Returns the trigonometric arc cosine of the specified angle `v`,
  111. in radians.
  112. If `v` is `NaN` or infinite, the result is `NaN`.
  113. **/
  114. static function acos(v:Float):Float;
  115. /**
  116. Returns the trigonometric arc tangent of the specified angle `v`,
  117. in radians.
  118. If `v` is `NaN` or infinite, the result is `NaN`.
  119. **/
  120. static function atan(v:Float):Float;
  121. /**
  122. Returns the trigonometric arc tangent whose tangent is the quotient of
  123. two specified numbers, in radians.
  124. If parameter `x` or `y` is `NaN`, `NEGATIVE_INFINITY` or `POSITIVE_INFINITY`,
  125. the result is `NaN`.
  126. **/
  127. static function atan2(y:Float, x:Float):Float;
  128. /**
  129. Returns Euler's number, raised to the power of `v`.
  130. exp(1.0) is approximately 2.718281828459.
  131. If `v` is `POSITIVE_INFINITY`, the result is `POSITIVE_INFINITY`.
  132. If `v` is `NEGATIVE_INFINITY`, the result is `0.0`.
  133. If `v` is `NaN`, the result is `NaN`.
  134. **/
  135. static function exp(v:Float):Float;
  136. /**
  137. Returns the natural logarithm of `v`.
  138. This is the mathematical inverse operation of exp,
  139. i.e. `log(exp(v)) == v` always holds.
  140. If `v` is negative (including `NEGATIVE_INFINITY`) or `NaN`, the result
  141. is `NaN`.
  142. If `v` is `POSITIVE_INFINITY`, the result is `POSITIVE_INFINITY`.
  143. If `v` is `0.0`, the result is `NEGATIVE_INFINITY`.
  144. **/
  145. static function log(v:Float):Float;
  146. /**
  147. Returns a specified base `v` raised to the specified power `exp`.
  148. **/
  149. static function pow(v:Float, exp:Float):Float;
  150. /**
  151. Returns the square root of `v`.
  152. If `v` is negative (including `NEGATIVE_INFINITY`) or `NaN`, the result
  153. is `NaN`.
  154. If `v` is `POSITIVE_INFINITY`, the result is `POSITIVE_INFINITY`.
  155. If `v` is `0.0`, the result is `0.0`.
  156. **/
  157. static function sqrt(v:Float):Float;
  158. /**
  159. Rounds `v` to the nearest integer value.
  160. Ties are rounded up, so that `0.5` becomes `1` and `-0.5` becomes `0`.
  161. If `v` is outside of the signed `Int32` range, or is `NaN`, `NEGATIVE_INFINITY`
  162. or `POSITIVE_INFINITY`, the result is unspecified.
  163. **/
  164. static function round(v:Float):Int;
  165. /**
  166. Returns the largest integer value that is not greater than `v`.
  167. If `v` is outside of the signed `Int32` range, or is `NaN`, `NEGATIVE_INFINITY`
  168. or `POSITIVE_INFINITY`, the result is unspecified.
  169. **/
  170. static function floor(v:Float):Int;
  171. /**
  172. Returns the smallest integer value that is not less than `v`.
  173. If `v` is outside of the signed `Int32` range, or is `NaN`, `NEGATIVE_INFINITY`
  174. or `POSITIVE_INFINITY`, the result is unspecified.
  175. **/
  176. static function ceil(v:Float):Int;
  177. /**
  178. Returns a pseudo-random number which is greater than or equal to 0.0,
  179. and less than 1.0.
  180. **/
  181. static function random() : Float;
  182. #if ((flash && !as3) || cpp)
  183. /**
  184. Returns the largest integer value that is not greater than `v`, as a `Float`.
  185. If `v` is is `NaN`, `NEGATIVE_INFINITY` or `POSITIVE_INFINITY`,
  186. the result is unspecified.
  187. **/
  188. static function ffloor( v : Float ) : Float;
  189. /**
  190. Returns the smallest integer value that is not less than `v`, as a `Float`.
  191. If `v` is is `NaN`, `NEGATIVE_INFINITY` or `POSITIVE_INFINITY`,
  192. the result is unspecified.
  193. **/
  194. static function fceil( v : Float ) : Float;
  195. /**
  196. Rounds `v` to the nearest integer value, as a Float.
  197. Ties are rounded up, so that `0.5` becomes `1` and `-0.5` becomes `0`.
  198. If `v` is is `NaN`, `NEGATIVE_INFINITY` or `POSITIVE_INFINITY`,
  199. the result is unspecified.
  200. **/
  201. static function fround( v : Float ) : Float;
  202. #else
  203. static inline function ffloor( v : Float ) : Float {
  204. return floor(v);
  205. }
  206. static inline function fceil( v : Float ) : Float {
  207. return ceil(v);
  208. }
  209. static inline function fround( v : Float ) : Float {
  210. return round(v);
  211. }
  212. #end
  213. /**
  214. Tells if `f` is a finite number.
  215. If `f` is `POSITIVE_INFINITY`, `NEGATIVE_INFINITY` or `NaN`, the result
  216. is `false`, otherwise the result is `true`.
  217. **/
  218. static function isFinite( f : Float ) : Bool;
  219. /**
  220. Tells if `f` is not a valid number.
  221. If `f` is `NaN`, the result is `true`, otherwise the result is `false`.
  222. In particular, both `POSITIVE_INFINITY` and `NEGATIVE_INFINITY` are
  223. not considered `NaN`.
  224. **/
  225. static function isNaN( f : Float ) : Bool;
  226. private static function __init__() : Void untyped {
  227. #if flash
  228. NaN = __global__["Number"].NaN;
  229. NEGATIVE_INFINITY = __global__["Number"].NEGATIVE_INFINITY;
  230. POSITIVE_INFINITY = __global__["Number"].POSITIVE_INFINITY;
  231. #else
  232. Math.__name__ = ["Math"];
  233. Math.NaN = Number["NaN"];
  234. Math.NEGATIVE_INFINITY = Number["NEGATIVE_INFINITY"];
  235. Math.POSITIVE_INFINITY = Number["POSITIVE_INFINITY"];
  236. #end
  237. Math.isFinite = function(i) {
  238. return
  239. #if flash
  240. __global__["isFinite"](i);
  241. #else
  242. false;
  243. #end
  244. };
  245. Math.isNaN = function(i) {
  246. return
  247. #if flash
  248. __global__["isNaN"](i);
  249. #else
  250. false;
  251. #end
  252. };
  253. }
  254. }