Math.hx 4.2 KB

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