Math.hx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (C)2005-2019 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 https://haxe.org/manual/std-math.html
  25. **/
  26. #if cpp
  27. @:include("hxMath.h")
  28. #end
  29. @:pure
  30. extern class Math {
  31. /**
  32. Represents the ratio of the circumference of a circle to its diameter,
  33. specified by the constant, π. `PI` is approximately `3.141592653589793`.
  34. **/
  35. static var PI(default, null):Float;
  36. /**
  37. A special `Float` constant which denotes negative infinity.
  38. For example, this is the result of `-1.0 / 0.0`.
  39. Operations with `NEGATIVE_INFINITY` as an operand may result in
  40. `NEGATIVE_INFINITY`, `POSITIVE_INFINITY` or `NaN`.
  41. If this constant is converted to an `Int`, e.g. through `Std.int()`, the
  42. result is unspecified.
  43. **/
  44. static var NEGATIVE_INFINITY(default, null):Float;
  45. /**
  46. A special `Float` constant which denotes positive infinity.
  47. For example, this is the result of `1.0 / 0.0`.
  48. Operations with `POSITIVE_INFINITY` as an operand may result in
  49. `NEGATIVE_INFINITY`, `POSITIVE_INFINITY` or `NaN`.
  50. If this constant is converted to an `Int`, e.g. through `Std.int()`, the
  51. result is unspecified.
  52. **/
  53. static var POSITIVE_INFINITY(default, null):Float;
  54. /**
  55. A special `Float` constant which denotes an invalid number.
  56. `NaN` stands for "Not a Number". It occurs when a mathematically incorrect
  57. operation is executed, such as taking the square root of a negative
  58. number: `Math.sqrt(-1)`.
  59. All further operations with `NaN` as an operand will result in `NaN`.
  60. If this constant is converted to an `Int`, e.g. through `Std.int()`, the
  61. result is unspecified.
  62. In order to test if a value is `NaN`, you should use `Math.isNaN()` function.
  63. **/
  64. static var NaN(default, null):Float;
  65. /**
  66. Returns the absolute value of `v`.
  67. - If `v` is positive or `0`, the result is unchanged. Otherwise the result is `-v`.
  68. - If `v` is `NEGATIVE_INFINITY` or `POSITIVE_INFINITY`, the result is `POSITIVE_INFINITY`.
  69. - If `v` is `NaN`, the result is `NaN`.
  70. **/
  71. static function abs(v:Float):Float;
  72. /**
  73. Returns the smaller of values `a` and `b`.
  74. - If `a` or `b` are `NaN`, the result is `NaN`.
  75. - If `a` or `b` are `NEGATIVE_INFINITY`, the result is `NEGATIVE_INFINITY`.
  76. - If `a` and `b` are `POSITIVE_INFINITY`, the result is `POSITIVE_INFINITY`.
  77. **/
  78. static function min(a:Float, b:Float):Float;
  79. /**
  80. Returns the greater of values `a` and `b`.
  81. - If `a` or `b` are `NaN`, the result is `NaN`.
  82. - If `a` or `b` are `POSITIVE_INFINITY`, the result is `POSITIVE_INFINITY`.
  83. - If `a` and `b` are `NEGATIVE_INFINITY`, the result is `NEGATIVE_INFINITY`.
  84. **/
  85. static function max(a:Float, b:Float):Float;
  86. /**
  87. Returns the trigonometric sine of the specified angle `v`, in radians.
  88. If `v` is `NaN` or infinite, the result is `NaN`.
  89. **/
  90. static function sin(v:Float):Float;
  91. /**
  92. Returns the trigonometric cosine of the specified angle `v`, in radians.
  93. If `v` is `NaN` or infinite, the result is `NaN`.
  94. **/
  95. static function cos(v:Float):Float;
  96. /**
  97. Returns the trigonometric tangent of the specified angle `v`, in radians.
  98. If `v` is `NaN` or infinite, the result is `NaN`.
  99. **/
  100. static function tan(v:Float):Float;
  101. /**
  102. Returns the trigonometric arc of the specified angle `v`, in radians.
  103. If `v` is `NaN` or infinite, the result is `NaN`.
  104. **/
  105. static function asin(v:Float):Float;
  106. /**
  107. Returns the trigonometric arc cosine of the specified angle `v`,
  108. in radians.
  109. If `v` is `NaN` or infinite, the result is `NaN`.
  110. **/
  111. static function acos(v:Float):Float;
  112. /**
  113. Returns the trigonometric arc tangent of the specified angle `v`,
  114. in radians.
  115. If `v` is `NaN` or infinite, the result is `NaN`.
  116. **/
  117. static function atan(v:Float):Float;
  118. /**
  119. Returns the trigonometric arc tangent whose tangent is the quotient of
  120. two specified numbers, in radians.
  121. If parameter `x` or `y` is `NaN`, `NEGATIVE_INFINITY` or `POSITIVE_INFINITY`,
  122. the result is `NaN`.
  123. **/
  124. static function atan2(y:Float, x:Float):Float;
  125. /**
  126. Returns Euler's number, raised to the power of `v`.
  127. `exp(1.0)` is approximately `2.718281828459`.
  128. - If `v` is `POSITIVE_INFINITY`, the result is `POSITIVE_INFINITY`.
  129. - If `v` is `NEGATIVE_INFINITY`, the result is `0.0`.
  130. - If `v` is `NaN`, the result is `NaN`.
  131. **/
  132. static function exp(v:Float):Float;
  133. /**
  134. Returns the natural logarithm of `v`.
  135. This is the mathematical inverse operation of exp,
  136. i.e. `log(exp(v)) == v` always holds.
  137. - If `v` is negative (including `NEGATIVE_INFINITY`) or `NaN`, the result is `NaN`.
  138. - If `v` is `POSITIVE_INFINITY`, the result is `POSITIVE_INFINITY`.
  139. - If `v` is `0.0`, the result is `NEGATIVE_INFINITY`.
  140. **/
  141. static function log(v:Float):Float;
  142. /**
  143. Returns a specified base `v` raised to the specified power `exp`.
  144. **/
  145. static function pow(v:Float, exp:Float):Float;
  146. /**
  147. Returns the square root of `v`.
  148. - If `v` is negative (including `NEGATIVE_INFINITY`) or `NaN`, the result is `NaN`.
  149. - If `v` is `POSITIVE_INFINITY`, the result is `POSITIVE_INFINITY`.
  150. - If `v` is `0.0`, the result is `0.0`.
  151. **/
  152. static function sqrt(v:Float):Float;
  153. /**
  154. Rounds `v` to the nearest integer value.
  155. Ties are rounded up, so that `0.5` becomes `1` and `-0.5` becomes `0`.
  156. If `v` is outside of the signed `Int32` range, or is `NaN`, `NEGATIVE_INFINITY`
  157. or `POSITIVE_INFINITY`, the result is unspecified.
  158. **/
  159. static function round(v:Float):Int;
  160. /**
  161. Returns the largest integer value that is not greater than `v`.
  162. If `v` is outside of the signed `Int32` range, or is `NaN`, `NEGATIVE_INFINITY`
  163. or `POSITIVE_INFINITY`, the result is unspecified.
  164. **/
  165. static function floor(v:Float):Int;
  166. /**
  167. Returns the smallest integer value that is not less than `v`.
  168. If `v` is outside of the signed `Int32` range, or is `NaN`, `NEGATIVE_INFINITY`
  169. or `POSITIVE_INFINITY`, the result is unspecified.
  170. **/
  171. static function ceil(v:Float):Int;
  172. /**
  173. Returns a pseudo-random number which is greater than or equal to `0.0`,
  174. and less than `1.0`.
  175. **/
  176. static function random():Float;
  177. #if (flash || cpp || eval)
  178. /**
  179. Returns the largest integer value that is not greater than `v`, as a `Float`.
  180. If `v` is is `NaN`, `NEGATIVE_INFINITY` or `POSITIVE_INFINITY`,
  181. the result is unspecified.
  182. **/
  183. static function ffloor(v:Float):Float;
  184. /**
  185. Returns the smallest integer value that is not less than `v`, as a `Float`.
  186. If `v` is is `NaN`, `NEGATIVE_INFINITY` or `POSITIVE_INFINITY`,
  187. the result is unspecified.
  188. **/
  189. static function fceil(v:Float):Float;
  190. /**
  191. Rounds `v` to the nearest integer value, as a Float.
  192. Ties are rounded up, so that `0.5` becomes `1` and `-0.5` becomes `0`.
  193. If `v` is is `NaN`, `NEGATIVE_INFINITY` or `POSITIVE_INFINITY`,
  194. the result is unspecified.
  195. **/
  196. static function fround(v:Float):Float;
  197. #else
  198. static inline function ffloor(v:Float):Float {
  199. return floor(v);
  200. }
  201. static inline function fceil(v:Float):Float {
  202. return ceil(v);
  203. }
  204. static inline function fround(v:Float):Float {
  205. return round(v);
  206. }
  207. #end
  208. /**
  209. Tells if `f` is a finite number.
  210. If `f` is `POSITIVE_INFINITY`, `NEGATIVE_INFINITY` or `NaN`, the result
  211. is `false`, otherwise the result is `true`.
  212. **/
  213. static function isFinite(f:Float):Bool;
  214. /**
  215. Tells if `f` is not a valid number.
  216. If `f` is `NaN`, the result is `true`, otherwise the result is `false`.
  217. In particular, both `POSITIVE_INFINITY` and `NEGATIVE_INFINITY` are
  218. not considered `NaN`.
  219. **/
  220. static function isNaN(f:Float):Bool;
  221. #if !eval
  222. private static function __init__():Void
  223. untyped {
  224. #if flash
  225. NaN = __global__["Number"].NaN;
  226. NEGATIVE_INFINITY = __global__["Number"].NEGATIVE_INFINITY;
  227. POSITIVE_INFINITY = __global__["Number"].POSITIVE_INFINITY;
  228. #else
  229. // TODO: Abandoned code block? Js has its own _std/Math.hx
  230. Math.__name__ = ["Math"];
  231. Math.NaN = Number["NaN"];
  232. Math.NEGATIVE_INFINITY = Number["NEGATIVE_INFINITY"];
  233. Math.POSITIVE_INFINITY = Number["POSITIVE_INFINITY"];
  234. #end
  235. Math.isFinite = function(i) {
  236. return #if flash __global__["isFinite"](i); #else false; #end
  237. };
  238. Math.isNaN = function(i) {
  239. return #if flash __global__["isNaN"](i); #else false; #end
  240. };
  241. }
  242. #end
  243. }