Math.hx 8.9 KB

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