Math.hx 3.9 KB

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