1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
/*
* Copyright (C)2005-2012 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/**
This class defines mathematical functions and constants.
**/
#if cpp @:include("hxMath.h") #end
extern class Math
{
/**
Represents the ratio of the circumference of a circle to its diameter,
specified by the constant, π. `PI` is approximately 3.141592653589793.
**/
static var PI(default,null) : Float;
/**
A special `Float` constant which denotes negative infinity.
For example, this is the result of -1.0 / 0.0.
Operations with `NEGATIVE_INFINITY` as an operand may result in
`NEGATIVE_INFINITY`, `POSITIVE_INFINITY` or `NaN`.
If this constant is converted to an `Int`, e.g. through `Std.int()`, the
result is unspecified.
**/
static var NEGATIVE_INFINITY(default, null) : Float;
/**
A special `Float` constant which denotes negative infinity.
For example, this is the result of 1.0 / 0.0.
Operations with `POSITIVE_INFINITY` as an operand may result in
`NEGATIVE_INFINITY`, `POSITIVE_INFINITY` or `NaN`.
If this constant is converted to an `Int`, e.g. through `Std.int()`, the
result is unspecified.
**/
static var POSITIVE_INFINITY(default,null) : Float;
/**
A special `Float` constant which denotes an invalid number.
NaN stands for "Not a Number". It occurs when a mathematically incorrect
operation is executed, such as taking the square root of a negative
number: Math.sqrt(-1).
All further operations with `NaN` as an operand will result in `NaN`.
If this constant is converted to an `Int`, e.g. through `Std.int()`, the
result is unspecified.
In order to test if a value is `NaN`, you should use `Math.isNaN()` function.
@php In PHP versions prior to 5.3.1 VC 9 there may be unexpected
results when performing arithmetic operations with `NaN` on Windows,
see [https://bugs.php.net/bug.php?id=42143]
**/
static var NaN(default, null) : Float;
/**
Returns the absolute value of `v`.
If `v` is positive or 0, the result is unchanged. Otherwise the result
is -`v`.
If `v` is `NEGATIVE_INFINITY` or `POSITIVE_INFINITY`, the result is
`POSITIVE_INFINITY`.
If `v` is `NaN`, the result is `NaN`.
**/
static function abs(v:Float):Float;
/**
Returns the smaller of values `a` and `b`.
If `a` or `b` are `NaN`, the result is `NaN`.
If `a` or `b` are `NEGATIVE_INFINITY`, the result is `NEGATIVE_INFINITY`.
If `a` and `b` are `POSITIVE_INFINITY`, the result is `POSITIVE_INFINITY`.
**/
static function min(a:Float, b:Float):Float;
/**
Returns the greater of values `a` and `b`.
If `a` or `b` are `NaN`, the result is `NaN`.
If `a` or `b` are `POSITIVE_INFINITY`, the result is `POSITIVE_INFINITY`.
If `a` and `b` are `NEGATIVE_INFINITY`, the result is `NEGATIVE_INFINITY`.
**/
static function max(a:Float, b:Float):Float;
/**
Returns the trigonometric sine of the specified angle `v`, in radians.
If `v` is `NaN` or infinite, the result is `NaN`.
**/
static function sin(v:Float):Float;
/**
Returns the trigonometric cosine of the specified angle `v`, in radians.
If `v` is `NaN` or infinite, the result is `NaN`.
**/
static function cos(v:Float):Float;
/**
Returns the trigonometric tangent of the specified angle `v`, in radians.
If `v` is `NaN` or infinite, the result is `NaN`.
**/
static function tan(v:Float):Float;
/**
Returns the trigonometric arc of the specified angle `v`, in radians.
If `v` is `NaN` or infinite, the result is `NaN`.
**/
static function asin(v:Float):Float;
/**
Returns the trigonometric arc cosine of the specified angle `v`,
in radians.
If `v` is `NaN` or infinite, the result is `NaN`.
**/
static function acos(v:Float):Float;
/**
Returns the trigonometric arc tangent of the specified angle `v`,
in radians.
If `v` is `NaN` or infinite, the result is `NaN`.
**/
static function atan(v:Float):Float;
/**
Returns the trigonometric arc tangent whose tangent is the quotient of
two specified numbers, in radians.
If parameter `x` or `y` is `NaN`, `NEGATIVE_INFINITY` or `POSITIVE_INFINITY`,
the result is `NaN`.
**/
static function atan2(y:Float, x:Float):Float;
/**
Returns Euler's number, raised to the power of `v`.
exp(1.0) is approximately 2.718281828459.
If `v` is `POSITIVE_INFINITY`, the result is `POSITIVE_INFINITY`.
If `v` is `NEGATIVE_INFINITY`, the result is `0.0`.
If `v` is `NaN`, the result is `NaN`.
**/
static function exp(v:Float):Float;
/**
Returns the natural logarithm of `v`.
This is the mathematical inverse operation of exp,
i.e. `log(exp(v)) == v` always holds.
If `v` is negative (including `NEGATIVE_INFINITY`) or `NaN`, the result
is `NaN`.
If `v` is `POSITIVE_INFINITY`, the result is `POSITIVE_INFINITY`.
If `v` is `0.0`, the result is `NEGATIVE_INFINITY`.
**/
static function log(v:Float):Float;
/**
Returns a specified base `v` raised to the specified power `exp`.
**/
static function pow(v:Float, exp:Float):Float;
/**
Returns the square root of `v`.
If `v` is negative (including `NEGATIVE_INFINITY`) or `NaN`, the result
is `NaN`.
If `v` is `POSITIVE_INFINITY`, the result is `POSITIVE_INFINITY`.
If `v` is `0.0`, the result is `0.0`.
**/
static function sqrt(v:Float):Float;
/**
Rounds `v` to the nearest integer value.
If `v` is outside of the signed `Int32` range, or is `NaN`, `NEGATIVE_INFINITY`
or `POSITIVE_INFINITY`, the result is unspecified.
**/
static function round(v:Float):Int;
/**
Returns the largest integer value that is not greater than `v`.
If `v` is outside of the signed `Int32` range, or is `NaN`, `NEGATIVE_INFINITY`
or `POSITIVE_INFINITY`, the result is unspecified.
**/
static function floor(v:Float):Int;
/**
Returns the smallest integer value that is not less than `v`.
If `v` is outside of the signed `Int32` range, or is `NaN`, `NEGATIVE_INFINITY`
or `POSITIVE_INFINITY`, the result is unspecified.
**/
static function ceil(v:Float):Int;
/**
Returns a pseudo-random number which is greater than or equal to 0.0,
and less than 1.0.
**/
static function random() : Float;
#if ((flash && !as3) || cpp)
/**
Returns the largest integer value that is not greater than `v`, as a `Float`.
If `v` is is `NaN`, `NEGATIVE_INFINITY` or `POSITIVE_INFINITY`,
the result is unspecified.
**/
static function ffloor( v : Float ) : Float;
/**
Returns the smallest integer value that is not less than `v`, as a `Float`.
If `v` is is `NaN`, `NEGATIVE_INFINITY` or `POSITIVE_INFINITY`,
the result is unspecified.
**/
static function fceil( v : Float ) : Float;
/**
Rounds `v` to the nearest integer value, as a Float.
If `v` is is `NaN`, `NEGATIVE_INFINITY` or `POSITIVE_INFINITY`,
the result is unspecified.
**/
static function fround( v : Float ) : Float;
#else
static inline function ffloor( v : Float ) : Float {
return floor(v);
}
static inline function fceil( v : Float ) : Float {
return ceil(v);
}
static inline function fround( v : Float ) : Float {
return round(v);
}
#end
/**
Tells if `f` is a finite number.
If `f` is `POSITIVE_INFINITY`, `NEGATIVE_INFINITY` or `NaN`, the result
is `false`, otherwise the result is `true`.
**/
static function isFinite( f : Float ) : Bool;
/**
Tells if `f` is not a valid number.
If `f` is `NaN`, the result is `true`, otherwise the result is `false`.
In particular, both `POSITIVE_INFINITY` and `NEGATIVE_INFINITY` are
not considered `NaN`.
**/
static function isNaN( f : Float ) : Bool;
private static function __init__() : Void untyped {
#if flash
NaN = __global__["Number"].NaN;
NEGATIVE_INFINITY = __global__["Number"].NEGATIVE_INFINITY;
POSITIVE_INFINITY = __global__["Number"].POSITIVE_INFINITY;
#else
Math.__name__ = ["Math"];
Math.NaN = Number["NaN"];
Math.NEGATIVE_INFINITY = Number["NEGATIVE_INFINITY"];
Math.POSITIVE_INFINITY = Number["POSITIVE_INFINITY"];
#end
Math.isFinite = function(i) {
return
#if flash
__global__["isFinite"](i);
#else
false;
#end
};
Math.isNaN = function(i) {
return
#if flash
__global__["isNaN"](i);
#else
false;
#end
};
}
}
|