Math.hx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (C)2005-2018 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. package;
  23. class Math
  24. {
  25. public static var PI(get,null) : Float;
  26. static inline function get_PI () : Float return lua.Math.pi;
  27. public static var NEGATIVE_INFINITY(get, null) : Float;
  28. static inline function get_NEGATIVE_INFINITY () : Float return -lua.Math.huge;
  29. public static var POSITIVE_INFINITY(get,null) : Float;
  30. static inline function get_POSITIVE_INFINITY () : Float return lua.Math.huge;
  31. public static var NaN(get, null) : Float;
  32. // Note: this has to be an untyped literal, otherwise the compiler tries
  33. // to unify it to an Int, which defeats useful numeric reflection behavior.
  34. static inline function get_NaN () : Float return untyped __lua__("(0/0)");
  35. public static function isNaN( f : Float ) : Bool return (f != f);
  36. public static function isFinite( f : Float ) : Bool {
  37. return (f > Math.NEGATIVE_INFINITY && f < Math.POSITIVE_INFINITY);
  38. }
  39. public static inline function abs(v:Float) :Float return lua.Math.abs(v);
  40. public static inline function acos(v:Float):Float return lua.Math.acos(v);
  41. public static inline function asin(v:Float):Float return lua.Math.asin(v);
  42. public static inline function atan(v:Float):Float return lua.Math.atan(v);
  43. public static inline function ceil(v:Float):Int return lua.Math.ceil(v);
  44. public static inline function cos(v:Float):Float return lua.Math.cos(v);
  45. public static inline function exp(v:Float):Float return lua.Math.exp(v);
  46. public static inline function sin(v:Float):Float return lua.Math.sin(v);
  47. public static inline function sqrt(v:Float):Float return lua.Math.sqrt(v);
  48. public static inline function tan(v:Float):Float return lua.Math.tan(v);
  49. public static inline function floor(v:Float):Int return lua.Math.floor(v);
  50. public static inline function log(v:Float):Float return lua.Math.log(v);
  51. public static inline function random() : Float return untyped __define_feature__("Math.random", lua.Math.random());
  52. public static inline function atan2(y:Float, x:Float):Float return lua.Math.atan2(y,x);
  53. public static function max(a:Float, b:Float):Float {
  54. return Math.isNaN(a) || Math.isNaN(b) ? Math.NaN : lua.Math.max(a,b);
  55. }
  56. public static function min(a:Float, b:Float):Float {
  57. return Math.isNaN(a) || Math.isNaN(b) ? Math.NaN : lua.Math.min(a,b);
  58. }
  59. public static inline function pow(v:Float, exp:Float):Float return lua.Math.pow(v,exp);
  60. public static inline function round(v:Float):Int return Math.floor(v + 0.5);
  61. public static inline function ffloor( v : Float ) : Float return floor(v);
  62. public static inline function fceil( v : Float ) : Float return ceil(v);
  63. public static inline function fround( v : Float ) : Float return round(v);
  64. }