Math.hx 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. import neko.Lib;
  23. @:native("Math")
  24. @:keep
  25. private class MathImpl {
  26. static var __rnd;
  27. static var _rand_float = Lib.load("std","random_float",1);
  28. static var _rand_int = Lib.load("std","random_int",2);
  29. public static function min(a:Float,b:Float) : Float { return if( a < b ) a else if( untyped $isnan(a) ) a else b; }
  30. public static function max(a:Float,b:Float) : Float { return if( a < b ) b else if( untyped $isnan(b) ) b else a; }
  31. public static function random() : Float { return _rand_float(__rnd); }
  32. public static function isNaN(f:Float) : Bool { return untyped __dollar__isnan(f); }
  33. public static function isFinite(f:Float) : Bool { return !untyped (__dollar__isinfinite(f) || __dollar__isnan(f)); }
  34. static function __init__() : Void {
  35. var M : Dynamic = MathImpl;
  36. __rnd = Lib.load("std","random_new",0)();
  37. M.PI = Lib.load("std","math_pi",0)();
  38. M.NaN = 0.0 / 0.0;
  39. M.POSITIVE_INFINITY = 1.0 / 0.0;
  40. M.NEGATIVE_INFINITY = -M.POSITIVE_INFINITY;
  41. M.abs = Lib.load("std","math_abs",1);
  42. M.sin = Lib.load("std","math_sin",1);
  43. M.cos = Lib.load("std","math_cos",1);
  44. M.atan2 = Lib.load("std","math_atan2",2);
  45. M.tan = Lib.load("std","math_tan",1);
  46. M.exp = Lib.load("std","math_exp",1);
  47. M.log = Lib.load("std","math_log",1);
  48. M.sqrt = Lib.load("std","math_sqrt",1);
  49. M.round = Lib.load("std","math_round",1);
  50. M.floor = Lib.load("std","math_floor",1);
  51. M.ceil = Lib.load("std","math_ceil",1);
  52. M.atan = Lib.load("std","math_atan",1);
  53. M.asin = Lib.load("std","math_asin",1);
  54. M.acos = Lib.load("std","math_acos",1);
  55. M.pow = Lib.load("std", "math_pow", 2);
  56. M.fceil = try Lib.load("std", "math_fceil", 1) catch( e : Dynamic ) M.ceil;
  57. M.ffloor = try Lib.load("std", "math_ffloor", 1) catch( e : Dynamic ) M.floor;
  58. M.fround = try Lib.load("std", "math_fround", 1) catch( e : Dynamic ) M.round;
  59. }
  60. }
  61. @:coreApi
  62. @:final
  63. extern class Math {
  64. public static var PI(default,null) : Float;
  65. public static var NaN(default,null) : Float;
  66. public static var POSITIVE_INFINITY(default,null) : Float;
  67. public static var NEGATIVE_INFINITY(default,null) : Float;
  68. public static function min(a:Float,b:Float) : Float;
  69. public static function max(a:Float,b:Float) : Float;
  70. public static function abs( v : Float ) : Float;
  71. public static function sin( v : Float ) : Float;
  72. public static function cos( v : Float ) : Float;
  73. public static function atan2( y : Float, x : Float ) : Float;
  74. public static function tan( v : Float ) : Float;
  75. public static function exp( v : Float ) : Float;
  76. public static function log( v : Float ) : Float;
  77. public static function sqrt( v : Float ) : Float;
  78. public static function round( v : Float ) : Int;
  79. public static function floor( v : Float ) : Int;
  80. public static function ceil( v : Float ) : Int;
  81. public static function atan( v : Float ) : Float;
  82. public static function asin( v : Float ) : Float;
  83. public static function acos( v : Float ) : Float;
  84. public static function pow( v : Float, exp : Float ) : Float;
  85. public static function fround( v : Float ) : Float;
  86. public static function ffloor( v : Float ) : Float;
  87. public static function fceil( v : Float ) : Float;
  88. public static function random() : Float;
  89. public static function isNaN(f:Float) : Bool;
  90. public static function isFinite(f:Float) : Bool;
  91. }