Math.hx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. import python.lib.Builtin;
  23. /**
  24. This class defines mathematical functions and constants.
  25. **/
  26. @:pythonImport("math")
  27. @:coreApi
  28. extern class Math
  29. {
  30. static var PI(default,null) : Float;
  31. /**
  32. A special Float constant which denotes negative infinity.
  33. For example, this is the result of -1.0 / 0.0.
  34. Operations with NEGATIVE_INFINITY as an operand may result in
  35. Operations with NEGATIVE_INFINITY as an operand may result in
  36. NEGATIVE_INFINITY, POSITIVE_INFINITY or NaN. For detailed information,
  37. see ...
  38. If this constant is converted to an Int, e.g. through Std.int(), the
  39. result is unspecified.
  40. **/
  41. static var NEGATIVE_INFINITY(default, null) : Float;
  42. /**
  43. A special Float constant which denotes negative infinity.
  44. For example, this is the result of 1.0 / 0.0.
  45. Operations with POSITIVE_INFINITY as an operand may result in
  46. NEGATIVE_INFINITY, POSITIVE_INFINITY or NaN. For detailed information,
  47. see ...
  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, see:
  63. 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. public static inline function abs(v:Float):Float
  75. {
  76. return (Math:Dynamic).fabs(v);
  77. }
  78. /**
  79. Returns the smaller of values `a` and `b`.
  80. If `a` or `b` are NaN, the result is NaN.
  81. If `a` or `b` are NEGATIVE_INFINITY, the result is NEGATIVE_INFINITY.
  82. If `a` and `b` are POSITIVE_INFINITY, the result is POSITIVE_INFINITY.
  83. **/
  84. public static inline function min(a:Float, b:Float):Float {
  85. return if (isNaN(a)) a else if (isNaN(b)) b else Builtin.min(a,b);
  86. }
  87. /**
  88. Returns the greater of values `a` and `b`.
  89. If `a` or `b` are NaN, the result is NaN.
  90. If `a` or `b` are POSITIVE_INFINITY, the result is POSITIVE_INFINITY.
  91. If `a` and `b` are NEGATIVE_INFINITY, the result is NEGATIVE_INFINITY.
  92. **/
  93. public static inline function max(a:Float, b:Float):Float
  94. {
  95. return if (isNaN(a)) a else if (isNaN(b)) b else Builtin.max(a,b);
  96. }
  97. /**
  98. Returns the trigonometric sine of `v`.
  99. The unit of `v` is radians.
  100. If `v` is NaN or infinite, the result is NaN.
  101. **/
  102. public static inline function sin(v:Float):Float {
  103. return if (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY) NaN else python.lib.Math.sin(v);
  104. }
  105. /**
  106. Returns the trigonometric cosine of `v`.
  107. The unit of `v` is radians.
  108. If `v` is NaN or infinite, the result is NaN.
  109. **/
  110. public static inline function cos(v:Float):Float {
  111. return if (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY) NaN else python.lib.Math.cos(v);
  112. }
  113. static function tan(v:Float):Float {
  114. return if (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY) NaN else python.lib.Math.tan(v);
  115. }
  116. static function asin(v:Float):Float {
  117. return if (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY) NaN else python.lib.Math.asin(v);
  118. }
  119. static function acos(v:Float):Float {
  120. return if (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY) NaN else python.lib.Math.acos(v);
  121. }
  122. static function atan(v:Float):Float {
  123. return if (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY) NaN else python.lib.Math.atan(v);
  124. }
  125. static function atan2(y:Float, x:Float):Float {
  126. return if (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY) NaN else python.lib.Math.atan2(v);
  127. }
  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. public static inline function exp(v:Float):Float
  136. {
  137. if (v == NEGATIVE_INFINITY) {
  138. return 0.0;
  139. } else if (v == POSITIVE_INFINITY) {
  140. return POSITIVE_INFINITY;
  141. } else {
  142. return (Math:Dynamic).exp(v);
  143. }
  144. }
  145. /**
  146. Returns the natural logarithm of `v`.
  147. If `v` is negative (including NEGATIVE_INFINITY) or NaN, the result is
  148. NaN.
  149. If `v` is POSITIVE_INFINITY, the result is POSITIVE_INFINITY.
  150. If `v` is 0.0, the result is NEGATIVE_INFINITY.
  151. This is the inverse operation of exp, i.e. log(exp(v)) == v always
  152. holds.
  153. **/
  154. public static inline function log(v:Float):Float {
  155. return if (v == 0.0) NEGATIVE_INFINITY else if (v < 0.0) NaN else python.lib.Math.log(v);
  156. }
  157. // TODO
  158. // http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Math.html#pow(double, double) <-- wtf?
  159. static function pow(v:Float, exp:Float):Float;
  160. /**
  161. Returns the square root of `v`.
  162. If `v` is negative (including NEGATIVE_INFINITY) or NaN, the result is
  163. NaN.
  164. If `v` is POSITIVE_INFINITY, the result is POSITIVE_INFINITY.
  165. If `v` is 0.0, the result is 0.0.
  166. **/
  167. public static inline function sqrt(v:Float):Float
  168. {
  169. return if (v < 0) NaN else python.lib.Math.sqrt(v);
  170. }
  171. /**
  172. Rounds `v` to the nearest Int value.
  173. If v is outside of the signed Int32 range, or is NaN, NEGATIVE_INFINITY or POSITIVE_INFINITY, the result is unspecified.
  174. TODO: need spec
  175. **/
  176. public static inline function round(v:Float):Int {
  177. return Math.floor(v + 0.5);
  178. }
  179. /**
  180. Returns the largest Int value that is not greater than `v`.
  181. If v is outside of the signed Int32 range, or is NaN, NEGATIVE_INFINITY or POSITIVE_INFINITY, the result is unspecified.
  182. TODO: need spec
  183. **/
  184. static function floor(v:Float):Int;
  185. /**
  186. Returns the smallest Int value that is not less than `v`.
  187. If v is outside of the signed Int32 range, or is NaN, NEGATIVE_INFINITY or POSITIVE_INFINITY, the result is unspecified.
  188. TODO: need spec
  189. **/
  190. static function ceil(v:Float):Int;
  191. /**
  192. Returns a pseudo-random number which is greater than or equal to 0.0,
  193. and less than 1.0.
  194. **/
  195. inline static function random() : Float {
  196. return python.lib.Random.random();
  197. }
  198. static inline function ffloor( v : Float ) : Float
  199. {
  200. if (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY) return v;
  201. if (isNaN(v)) return NaN;
  202. return floor(v);
  203. }
  204. static inline function fceil( v : Float ) : Float
  205. {
  206. if (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY) return v;
  207. if (isNaN(v)) return NaN;
  208. return ceil(v);
  209. }
  210. static inline function fround( v : Float ) : Float
  211. {
  212. if (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY) return v;
  213. if (isNaN(v)) return NaN;
  214. return round(v);
  215. }
  216. /**
  217. Tells if `f` is a finite number.
  218. If `f` is POSITIVE_INFINITY, NEGATIVE_INFINITY or NaN, the result is
  219. false.
  220. Otherwise the result is true.
  221. **/
  222. static inline function isFinite( f : Float ) : Bool return f != POSITIVE_INFINITY && f != NEGATIVE_INFINITY && !isNaN(f);
  223. /**
  224. Tells if `f` is not a valid number.
  225. If `f` is NaN, the result is true.
  226. Otherwise the result is false. In particular, both POSITIVE_INFINITY and
  227. NEGATIVE_INFINITY are not considered NaN.
  228. **/
  229. static inline function isNaN( f : Float ) : Bool {
  230. return python.lib.Math.isnan(f);
  231. }
  232. static function __init__():Void {
  233. NEGATIVE_INFINITY = Builtin.float('-inf');
  234. POSITIVE_INFINITY = Builtin.float('inf');
  235. NaN = Builtin.float("nan");
  236. PI = python.lib.Math.pi;
  237. }
  238. }