Math.hx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright (C)2005-2012 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. **/
  25. #if cpp @:include("hxMath") #end
  26. extern class Math
  27. {
  28. static var PI(default,null) : Float;
  29. /**
  30. A special Float constant which denotes negative infinity.
  31. For example, this is the result of -1.0 / 0.0.
  32. Operations with NEGATIVE_INFINITY as an operand may result in
  33. Operations with NEGATIVE_INFINITY as an operand may result in
  34. NEGATIVE_INFINITY, POSITIVE_INFINITY or NaN. For detailed information,
  35. see ...
  36. If this constant is converted to an Int, e.g. through Std.int(), the
  37. result is unspecified.
  38. **/
  39. static var NEGATIVE_INFINITY(default, null) : Float;
  40. /**
  41. A special Float constant which denotes negative infinity.
  42. For example, this is the result of 1.0 / 0.0.
  43. Operations with POSITIVE_INFINITY as an operand may result in
  44. NEGATIVE_INFINITY, POSITIVE_INFINITY or NaN. For detailed information,
  45. see ...
  46. If this constant is converted to an Int, e.g. through Std.int(), the
  47. result is unspecified.
  48. **/
  49. static var POSITIVE_INFINITY(default,null) : Float;
  50. /**
  51. A special Float constant which denotes an invalid number.
  52. NaN stands for "Not a Number". It occurs when a mathematically incorrect
  53. operation is executed, such as taking the square root of a negative
  54. number: Math.sqrt(-1).
  55. All further operations with NaN as an operand will result in NaN.
  56. If this constant is converted to an Int, e.g. through Std.int(), the
  57. result is unspecified.
  58. In order to test if a value is NaN, you should use Math.isNaN() function.
  59. (Php) In PHP versions prior to 5.3.1 VC 9 there may be unexpected
  60. results when performing arithmetic operations with NaN on Windows, see:
  61. https://bugs.php.net/bug.php?id=42143
  62. **/
  63. static var NaN(default, null) : Float;
  64. /**
  65. Returns the absolute value of `v`.
  66. If `v` is positive or 0, the result is unchanged. Otherwise the result
  67. is -`v`.
  68. If `v` is NEGATIVE_INFINITY or POSITIVE_INFINITY, the result is
  69. POSITIVE_INFINITY.
  70. If `v` is NaN, the result is NaN.
  71. **/
  72. static function abs(v:Float):Float;
  73. /**
  74. Returns the smaller of values `a` and `b`.
  75. If `a` or `b` are NaN, the result is NaN.
  76. If `a` or `b` are NEGATIVE_INFINITY, the result is NEGATIVE_INFINITY.
  77. If `a` and `b` are POSITIVE_INFINITY, the result is POSITIVE_INFINITY.
  78. **/
  79. static function min(a:Float, b:Float):Float;
  80. /**
  81. Returns the greater of values `a` and `b`.
  82. If `a` or `b` are NaN, the result is NaN.
  83. If `a` or `b` are POSITIVE_INFINITY, the result is POSITIVE_INFINITY.
  84. If `a` and `b` are NEGATIVE_INFINITY, the result is NEGATIVE_INFINITY.
  85. **/
  86. static function max(a:Float, b:Float):Float;
  87. /**
  88. Returns the trigonometric sine of `v`.
  89. The unit of `v` is radians.
  90. If `v` is NaN or infinite, the result is NaN.
  91. **/
  92. static function sin(v:Float):Float;
  93. /**
  94. Returns the trigonometric cosine of `v`.
  95. The unit of `v` is radians.
  96. If `v` is NaN or infinite, the result is NaN.
  97. **/
  98. static function cos(v:Float):Float;
  99. // TODO
  100. static function tan(v:Float):Float;
  101. static function asin(v:Float):Float;
  102. static function acos(v:Float):Float;
  103. static function atan(v:Float):Float;
  104. static function atan2(y:Float, x:Float):Float;
  105. /**
  106. Returns Euler's number, raised to the power of `v`.
  107. exp(1.0) is approximately 2.718281828459.
  108. If `v` is POSITIVE_INFINITY, the result is POSITIVE_INFINITY.
  109. If `v` is NEGATIVE_INFINITY, the result is 0.0.
  110. If `v` is NaN, the result is NaN.
  111. **/
  112. static function exp(v:Float):Float;
  113. /**
  114. Returns the natural logarithm of `v`.
  115. If `v` is negative (including NEGATIVE_INFINITY) or NaN, the result is
  116. NaN.
  117. If `v` is POSITIVE_INFINITY, the result is POSITIVE_INFINITY.
  118. If `v` is 0.0, the result is NEGATIVE_INFINITY.
  119. This is the inverse operation of exp, i.e. log(exp(v)) == v always
  120. holds.
  121. **/
  122. static function log(v:Float):Float;
  123. // TODO
  124. // http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Math.html#pow(double, double) <-- wtf?
  125. static function pow(v:Float, exp:Float):Float;
  126. /**
  127. Returns the square root of `v`.
  128. If `v` is negative (including NEGATIVE_INFINITY) or NaN, the result is
  129. NaN.
  130. If `v` is POSITIVE_INFINITY, the result is POSITIVE_INFINITY.
  131. If `v` is 0.0, the result is 0.0.
  132. **/
  133. static function sqrt(v:Float):Float;
  134. /**
  135. Rounds `v` to the nearest Int value.
  136. If v is outside of the signed Int32 range, or is NaN, NEGATIVE_INFINITY or POSITIVE_INFINITY, the result is unspecified.
  137. TODO: need spec
  138. **/
  139. static function round(v:Float):Int;
  140. /**
  141. Returns the largest Int value that is not greater than `v`.
  142. If v is outside of the signed Int32 range, or is NaN, NEGATIVE_INFINITY or POSITIVE_INFINITY, the result is unspecified.
  143. TODO: need spec
  144. **/
  145. static function floor(v:Float):Int;
  146. /**
  147. Returns the smallest Int value that is not less than `v`.
  148. If v is outside of the signed Int32 range, or is NaN, NEGATIVE_INFINITY or POSITIVE_INFINITY, the result is unspecified.
  149. TODO: need spec
  150. **/
  151. static function ceil(v:Float):Int;
  152. /**
  153. Returns a pseudo-random number which is greater than or equal to 0.0,
  154. and less than 1.0.
  155. **/
  156. static function random() : Float;
  157. #if ((flash9 && !as3) || cpp)
  158. static function ffloor( v : Float ) : Float;
  159. static function fceil( v : Float ) : Float;
  160. static function fround( v : Float ) : Float;
  161. #else
  162. static inline function ffloor( v : Float ) : Float {
  163. return floor(v);
  164. }
  165. static inline function fceil( v : Float ) : Float {
  166. return ceil(v);
  167. }
  168. static inline function fround( v : Float ) : Float {
  169. return round(v);
  170. }
  171. #end
  172. /**
  173. Tells if `f` is a finite number.
  174. If `f` is POSITIVE_INFINITY, NEGATIVE_INFINITY or NaN, the result is
  175. false.
  176. Otherwise the result is true.
  177. **/
  178. static function isFinite( f : Float ) : Bool;
  179. /**
  180. Tells if `f` is not a valid number.
  181. If `f` is NaN, the result is true.
  182. Otherwise the result is false. In particular, both POSITIVE_INFINITY and
  183. NEGATIVE_INFINITY are not considered NaN.
  184. **/
  185. static function isNaN( f : Float ) : Bool;
  186. private static function __init__() : Void untyped {
  187. #if flash9
  188. NaN = __global__["Number"].NaN;
  189. NEGATIVE_INFINITY = __global__["Number"].NEGATIVE_INFINITY;
  190. POSITIVE_INFINITY = __global__["Number"].POSITIVE_INFINITY;
  191. #else
  192. #if !js // genjs.ml will insert this only if necessary.
  193. Math.__name__ = ["Math"];
  194. #end
  195. Math.NaN = Number["NaN"];
  196. Math.NEGATIVE_INFINITY = Number["NEGATIVE_INFINITY"];
  197. Math.POSITIVE_INFINITY = Number["POSITIVE_INFINITY"];
  198. #end
  199. #if js
  200. __feature__("Type.resolveClass",$hxClasses['Math'] = Math);
  201. #end
  202. Math.isFinite = function(i) {
  203. return
  204. #if flash9
  205. __global__["isFinite"](i);
  206. #elseif flash
  207. _global["isFinite"](i);
  208. #elseif js
  209. __js__("isFinite")(i);
  210. #else
  211. false;
  212. #end
  213. };
  214. Math.isNaN = function(i) {
  215. return
  216. #if flash9
  217. __global__["isNaN"](i);
  218. #elseif flash
  219. _global["isNaN"](i);
  220. #elseif js
  221. __js__("isNaN")(i);
  222. #else
  223. false;
  224. #end
  225. };
  226. }
  227. }