2
0

Math.hx 3.8 KB

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