Math.hx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (C)2005-2017 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. @:coreApi class Math
  23. {
  24. public static var PI(default,null) : Float;
  25. public static var NaN(default,null) : Float;
  26. public static var POSITIVE_INFINITY(default,null) : Float;
  27. public static var NEGATIVE_INFINITY(default,null) : Float;
  28. public static function abs(v : Float) : Float { return untyped __call__("abs", v); }
  29. public static function min(a : Float,b : Float) : Float { return untyped !isNaN(a) ? __call__("min", a, b) : NaN; }
  30. public static function max(a : Float,b : Float) : Float { return untyped !isNaN(b) ? __call__("max", a, b) : NaN; }
  31. public static function sin(v : Float) : Float { return untyped __call__("sin", v); }
  32. public static function cos(v : Float) : Float { return untyped __call__("cos", v); }
  33. public static function atan2(y : Float,x : Float) : Float { return untyped __call__("atan2", y, x); }
  34. public static function tan(v : Float) : Float { return untyped __call__("tan", v); }
  35. public static function exp(v : Float) : Float {
  36. if(isNaN(v))
  37. return NaN;
  38. if(untyped __call__("is_finite", v))
  39. return untyped __call__("exp", v);
  40. else if(v == POSITIVE_INFINITY)
  41. return POSITIVE_INFINITY;
  42. else
  43. return 0.0;
  44. }
  45. public static function log(v : Float) : Float { return untyped __call__("log", v); }
  46. public static function sqrt(v : Float) : Float { return untyped __call__("sqrt", v); }
  47. public static function round(v : Float) : Int { return untyped __call__("(int) floor", v + 0.5); }
  48. public static function floor(v : Float) : Int { return untyped __call__("(int) floor", v); }
  49. public static function ceil(v : Float) : Int { return untyped __call__("(int) ceil", v); }
  50. public static function atan(v : Float) : Float { return untyped __call__("atan", v); }
  51. public static function asin(v : Float) : Float { return untyped __call__("asin", v); }
  52. public static function acos(v : Float) : Float { return untyped __call__("acos", v); }
  53. public static function pow(v : Float,exp : Float) : Float { return untyped __call__("pow", v, exp); }
  54. public static function random() : Float { return untyped __call__("mt_rand") / __call__("mt_getrandmax"); }
  55. public static function isNaN(f : Float) : Bool { return untyped __call__("is_nan", f); }
  56. public static function isFinite(f : Float) : Bool { return untyped __call__("is_finite", f); }
  57. public static function fround(v : Float) : Float { return untyped __call__("floor", v + 0.5); }
  58. public static function ffloor(v : Float) : Float { return untyped __call__("floor", v); }
  59. public static function fceil(v : Float) : Float { return untyped __call__("ceil", v); }
  60. static function __init__() : Void {
  61. PI = untyped __php__("M_PI");
  62. NaN = untyped __php__("acos(1.01)");
  63. NEGATIVE_INFINITY = untyped __php__("log(0)");
  64. POSITIVE_INFINITY = -NEGATIVE_INFINITY;
  65. }
  66. }