Math.hx 3.8 KB

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