Math.hx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. @:coreApi @:nativeGen class Math {
  23. @:readOnly
  24. private static var rand = new cs.system.Random();
  25. @:readOnly
  26. public static var PI(default, null) = cs.system.Math.PI;
  27. @:readOnly
  28. public static var NaN(default, null) = cs.system.Double.NaN;
  29. @:readOnly
  30. public static var NEGATIVE_INFINITY(default, null) = cs.system.Double.NegativeInfinity;
  31. @:readOnly
  32. public static var POSITIVE_INFINITY(default, null) = cs.system.Double.PositiveInfinity;
  33. public static inline function abs(v:Float):Float {
  34. return cs.system.Math.Abs(v);
  35. }
  36. public static inline function min(a:Float, b:Float):Float {
  37. return cs.system.Math.Min(a, b);
  38. }
  39. public static inline function max(a:Float, b:Float):Float {
  40. return cs.system.Math.Max(a, b);
  41. }
  42. public static inline function sin(v:Float):Float {
  43. return cs.system.Math.Sin(v);
  44. }
  45. public static inline function cos(v:Float):Float {
  46. return cs.system.Math.Cos(v);
  47. }
  48. public static inline function atan2(y:Float, x:Float):Float {
  49. return cs.system.Math.Atan2(y, x);
  50. }
  51. public static inline function tan(v:Float):Float {
  52. return cs.system.Math.Tan(v);
  53. }
  54. public static inline function exp(v:Float):Float {
  55. return cs.system.Math.Exp(v);
  56. }
  57. public static inline function log(v:Float):Float {
  58. return cs.system.Math.Log(v);
  59. }
  60. public static inline function sqrt(v:Float):Float {
  61. return cs.system.Math.Sqrt(v);
  62. }
  63. public static inline function fround(v:Float):Float {
  64. return cs.system.Math.Floor(v + 0.5);
  65. }
  66. public static inline function ffloor(v:Float):Float {
  67. return cs.system.Math.Floor(v);
  68. }
  69. public static inline function fceil(v:Float):Float {
  70. return cs.system.Math.Ceiling(v);
  71. }
  72. public static function round(v:Float):Int {
  73. var vint = Std.int(v);
  74. var dec = v - vint;
  75. if (dec >= 1 || dec <= -1)
  76. return vint; // overflow
  77. if (dec >= .5)
  78. return vint + 1;
  79. if (dec < -.5)
  80. return vint - 1;
  81. return vint;
  82. }
  83. public static inline function floor(v:Float):Int {
  84. return Std.int(cs.system.Math.Floor(v));
  85. }
  86. public static inline function ceil(v:Float):Int {
  87. return Std.int(cs.system.Math.Ceiling(v));
  88. }
  89. public static inline function atan(v:Float):Float {
  90. return cs.system.Math.Atan(v);
  91. }
  92. public static inline function asin(v:Float):Float {
  93. return cs.system.Math.Asin(v);
  94. }
  95. public static inline function acos(v:Float):Float {
  96. return cs.system.Math.Acos(v);
  97. }
  98. public static inline function pow(v:Float, exp:Float):Float {
  99. return cs.system.Math.Pow(v, exp);
  100. }
  101. public static inline function random():Float {
  102. return rand.NextDouble();
  103. }
  104. public static inline function isFinite(f:Float):Bool {
  105. return !cs.system.Double.IsInfinity(f) && !cs.system.Double.IsNaN(f);
  106. }
  107. public static inline function isNaN(f:Float):Bool {
  108. return cs.system.Double.IsNaN(f);
  109. }
  110. }