Math.hx 3.7 KB

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