2
0

Math.hx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. import python.internal.UBuiltins;
  23. @:pythonImport("math")
  24. @:coreApi
  25. extern class Math
  26. {
  27. static var PI(default,null) : Float;
  28. static var NEGATIVE_INFINITY(default, null) : Float;
  29. static var POSITIVE_INFINITY(default,null) : Float;
  30. static var NaN(default, null) : Float;
  31. public static inline function abs(v:Float):Float
  32. {
  33. return (Math:Dynamic).fabs(v);
  34. }
  35. public static inline function min(a:Float, b:Float):Float {
  36. return if (isNaN(a)) a else if (isNaN(b)) b else UBuiltins.min(a,b);
  37. }
  38. public static inline function max(a:Float, b:Float):Float
  39. {
  40. return if (isNaN(a)) a else if (isNaN(b)) b else UBuiltins.max(a,b);
  41. }
  42. public static inline function sin(v:Float):Float {
  43. return if (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY) NaN else python.lib.Math.sin(v);
  44. }
  45. public static inline function cos(v:Float):Float {
  46. return if (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY) NaN else python.lib.Math.cos(v);
  47. }
  48. static function tan(v:Float):Float {
  49. return if (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY) NaN else python.lib.Math.tan(v);
  50. }
  51. static function asin(v:Float):Float {
  52. return if (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY) NaN else python.lib.Math.asin(v);
  53. }
  54. static function acos(v:Float):Float {
  55. return if (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY) NaN else python.lib.Math.acos(v);
  56. }
  57. static function atan(v:Float):Float {
  58. return if (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY) NaN else python.lib.Math.atan(v);
  59. }
  60. static function atan2(y:Float, x:Float):Float {
  61. return if (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY) NaN else python.lib.Math.atan2(v);
  62. }
  63. public static inline function exp(v:Float):Float
  64. {
  65. if (v == NEGATIVE_INFINITY) {
  66. return 0.0;
  67. } else if (v == POSITIVE_INFINITY) {
  68. return POSITIVE_INFINITY;
  69. } else {
  70. return (Math:Dynamic).exp(v);
  71. }
  72. }
  73. public static inline function log(v:Float):Float {
  74. return if (v == 0.0) NEGATIVE_INFINITY else if (v < 0.0) NaN else python.lib.Math.log(v);
  75. }
  76. static function pow(v:Float, exp:Float):Float;
  77. public static inline function sqrt(v:Float):Float
  78. {
  79. return if (v < 0) NaN else python.lib.Math.sqrt(v);
  80. }
  81. public static inline function round(v:Float):Int {
  82. return Math.floor(v + 0.5);
  83. }
  84. static function floor(v:Float):Int;
  85. static function ceil(v:Float):Int;
  86. inline static function random() : Float {
  87. return python.lib.Random.random();
  88. }
  89. static inline function ffloor( v : Float ) : Float {
  90. if (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY) return v;
  91. if (isNaN(v)) return NaN;
  92. return floor(v);
  93. }
  94. static inline function fceil( v : Float ) : Float
  95. {
  96. if (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY) return v;
  97. if (isNaN(v)) return NaN;
  98. return ceil(v);
  99. }
  100. static inline function fround( v : Float ) : Float {
  101. if (v == POSITIVE_INFINITY || v == NEGATIVE_INFINITY) return v;
  102. if (isNaN(v)) return NaN;
  103. return round(v);
  104. }
  105. static inline function isFinite( f : Float ) : Bool return f != POSITIVE_INFINITY && f != NEGATIVE_INFINITY && !isNaN(f);
  106. static inline function isNaN( f : Float ) : Bool {
  107. return python.lib.Math.isnan(f);
  108. }
  109. static function __init__():Void {
  110. NEGATIVE_INFINITY = UBuiltins.float('-inf');
  111. POSITIVE_INFINITY = UBuiltins.float('inf');
  112. NaN = UBuiltins.float("nan");
  113. PI = python.lib.Math.pi;
  114. }
  115. }