AI for Games 1.1.1
|
Static Public Member Functions | |
static double | sqrt (final double a) |
static double | cosh (double x) |
static double | sinh (double x) |
static double | tanh (double x) |
static double | acosh (final double a) |
static double | asinh (double a) |
static double | atanh (double a) |
static double | signum (final double a) |
static float | signum (final float a) |
static double | nextUp (final double a) |
static float | nextUp (final float a) |
static double | random () |
static double | exp (double x) |
static double | expm1 (double x) |
static double | log (final double x) |
static double | log1p (final double x) |
static double | log10 (final double x) |
static double | pow (double x, double y) |
static double | sin (double x) |
static double | cos (double x) |
static double | tan (double x) |
static double | atan (double x) |
static double | atan2 (double y, double x) |
static double | asin (double x) |
static double | acos (double x) |
static double | cbrt (double x) |
static double | toRadians (double x) |
static double | toDegrees (double x) |
static int | abs (final int x) |
static long | abs (final long x) |
static float | abs (final float x) |
static double | abs (double x) |
static double | ulp (double x) |
static float | ulp (float x) |
static double | scalb (final double d, final int n) |
static float | scalb (final float f, final int n) |
static double | nextAfter (double d, double direction) |
static float | nextAfter (final float f, final double direction) |
static double | floor (double x) |
static double | ceil (double x) |
static double | rint (double x) |
static long | round (double x) |
static int | round (final float x) |
static int | min (final int a, final int b) |
static long | min (final long a, final long b) |
static float | min (final float a, final float b) |
static double | min (final double a, final double b) |
static int | max (final int a, final int b) |
static long | max (final long a, final long b) |
static float | max (final float a, final float b) |
static double | max (final double a, final double b) |
static double | hypot (final double x, final double y) |
static double | IEEEremainder (double dividend, double divisor) |
static double | copySign (double magnitude, double sign) |
static float | copySign (float magnitude, float sign) |
static int | getExponent (final double d) |
static int | getExponent (final float f) |
Static Public Attributes | |
static final double | PI = 105414357.0 / 33554432.0 + 1.984187159361080883e-9 |
static final double | TWO_PI = PI * 2.0 |
static final double | HALF_PI = PI * 0.5 |
static final double | QUARTER_PI = PI * 0.25 |
static final double | E = 2850325.0 / 1048576.0 + 8.254840070411028747e-8 |
static final double | SAFE_MIN = 0x1.0p-1022 |
Static Package Functions | |
[static initializer] | |
Private Member Functions | |
FastMath () | |
Static Private Member Functions | |
static double | doubleHighPart (double d) |
static double | exp (double x, double extra, double[] hiPrec) |
static double | expm1 (double x, double hiPrecOut[]) |
static double | slowexp (final double x, final double result[]) |
static void | split (final double d, final double split[]) |
static void | resplit (final double a[]) |
static void | splitMult (double a[], double b[], double ans[]) |
static void | splitAdd (final double a[], final double b[], final double ans[]) |
static void | splitReciprocal (final double in[], final double result[]) |
static void | quadMult (final double a[], final double b[], final double result[]) |
static double | expint (int p, final double result[]) |
static double | log (final double x, final double[] hiPrec) |
static double[] | slowLog (double xi) |
static double | slowSin (final double x, final double result[]) |
static double | slowCos (final double x, final double result[]) |
static void | buildSinCosTables () |
static double | polySine (final double x) |
static double | polyCosine (double x) |
static double | sinQ (double xa, double xb) |
static double | cosQ (double xa, double xb) |
static double | tanQ (double xa, double xb, boolean cotanFlag) |
static void | reducePayneHanek (double x, double result[]) |
static double | atan (double xa, double xb, boolean leftPlane) |
Static Private Attributes | |
static final double | EXP_INT_TABLE_A [] = new double[1500] |
static final double | EXP_INT_TABLE_B [] = new double[1500] |
static final double | EXP_FRAC_TABLE_A [] = new double[1025] |
static final double | EXP_FRAC_TABLE_B [] = new double[1025] |
static final double | FACT [] = new double[20] |
static final double | LN_MANT [][] = new double[1024][] |
static final double | LN_2_A = 0.693147063255310059 |
static final double | LN_2_B = 1.17304635250823482e-7 |
static final double | LN_SPLIT_COEF [][] |
static final double | LN_QUICK_COEF [][] |
static final double | LN_HI_PREC_COEF [][] |
static final double | SINE_TABLE_A [] = new double[14] |
static final double | SINE_TABLE_B [] = new double[14] |
static final double | COSINE_TABLE_A [] = new double[14] |
static final double | COSINE_TABLE_B [] = new double[14] |
static final double | TANGENT_TABLE_A [] = new double[14] |
static final double | TANGENT_TABLE_B [] = new double[14] |
static final long | RECIP_2PI [] |
static final long | PI_O_4_BITS [] |
static final double | EIGHTHS [] = {0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0, 1.125, 1.25, 1.375, 1.5, 1.625} |
static final double | CBRTTWO [] |
static final long | HEX_40000000 = 0x40000000L |
static final long | MASK_30BITS = -1L - (HEX_40000000 -1) |
static final double | TWO_POWER_52 = 4503599627370496.0 |
Faster, more accurate, portable alternative to StrictMath
.
Additionally implements the following methods not found in StrictMath:
The following methods are found in StrictMath since 1.6 only
copySign(double, double)
getExponent(double)
nextAfter(double,double)
nextUp(double)
scalb(double, int)
copySign(float, float)
getExponent(float)
nextAfter(float,double)
nextUp(float)
scalb(float, int)
|
private |
Private Constructor
|
static |
Absolute value.
x | number from which absolute value is requested |
|
static |
Absolute value.
x | number from which absolute value is requested |
|
static |
Absolute value.
x | number from which absolute value is requested |
|
static |
Absolute value.
x | number from which absolute value is requested |
|
static |
Compute the arc cosine of a number.
x | number on which evaluation is done |
|
static |
Compute the inverse hyperbolic cosine of a number.
a | number on which evaluation is done |
|
static |
Compute the arc sine of a number.
x | number on which evaluation is done |
|
static |
Compute the inverse hyperbolic sine of a number.
a | number on which evaluation is done |
|
static |
Arctangent function
x | a number |
|
staticprivate |
Internal helper function to compute arctangent.
xa | number from which arctangent is requested |
xb | extra bits for x (may be 0.0) |
leftPlane | if true, result angle must be put in the left half plane |
PI
if leftPlane is true)
|
static |
Two arguments arctangent function
y | ordinate |
x | abscissa |
-PI
and PI
|
static |
Compute the inverse hyperbolic tangent of a number.
a | number on which evaluation is done |
|
staticprivate |
Build the sine and cosine tables.
|
static |
Compute the cubic root of a number.
x | number on which evaluation is done |
|
static |
Get the smallest whole number larger than x.
x | number from which ceil is requested |
|
static |
Returns the first argument with the sign of the second argument. A NaN sign
argument is treated as positive.
magnitude | the value to return |
sign | the sign for the returned value |
sign
argument
|
static |
Returns the first argument with the sign of the second argument. A NaN sign
argument is treated as positive.
magnitude | the value to return |
sign | the sign for the returned value |
sign
argument
|
static |
Cosine function
x | a number |
|
static |
Compute the hyperbolic cosine of a number.
x | number on which evaluation is done |
|
staticprivate |
Compute cosine in the first quadrant by subtracting input from PI/2 and then calling sinQ. This is more accurate as the input approaches PI/2.
xa | number from which cosine is requested |
xb | extra bits for x (may be 0.0) |
|
staticprivate |
Get the high order bits from the mantissa. Equivalent to adding and subtracting HEX_40000 but also works for very large numbers
d | the value to split |
|
static |
Exponential function.
Computes exp(x), function result is nearly rounded. It will be correctly rounded to the theoretical value for 99.9% of input values, otherwise it will have a 1 UPL error.
Method: Lookup intVal = exp(int(x)) Lookup fracVal = exp(int(x-int(x) / 1024.0) * 1024.0 ); Compute z as the exponential of the remaining bits by a polynomial minus one exp(x) = intVal * fracVal * (1 + z)
Accuracy: Calculation is done with 63 bits of precision, so result should be correctly rounded for 99.9% of input values, with less than 1 ULP error otherwise.
x | a double |
|
staticprivate |
Internal helper method for exponential function.
x | original argument of the exponential function |
extra | extra bits of precision on input (To Be Confirmed) |
hiPrec | extra bits of precision on output (To Be Confirmed) |
|
staticprivate |
Compute exp(p) for a integer p in extended precision.
p | integer whose exponential is requested |
result | placeholder where to put the result in extended precision |
|
static |
Compute exp(x) - 1
x | number to compute shifted exponential |
|
staticprivate |
Internal helper method for expm1
x | number to compute shifted exponential |
hiPrecOut | receive high precision result for -1.0 < x < 1.0 |
|
static |
Get the largest whole number smaller than x.
x | number from which floor is requested |
|
static |
Return the exponent of a double number, removing the bias.
For double numbers of the form 2x, the unbiased exponent is exactly x.
d | number from which exponent is requested |
|
static |
Return the exponent of a float number, removing the bias.
For float numbers of the form 2x, the unbiased exponent is exactly x.
f | number from which exponent is requested |
|
static |
Returns the hypotenuse of a triangle with sides x
and y
x | a value |
y | a value |
|
static |
Computes the remainder as prescribed by the IEEE 754 standard. The remainder value is mathematically equal to x - y*n
where n
is the mathematical integer closest to the exact mathematical value of the quotient x/y
. If two mathematical integers are equally close to x/y
then n
is the integer that is even.
Note: this implementation currently delegates to StrictMath#IEEEremainder
dividend | the number to be divided |
divisor | the number by which to divide |
|
static |
Natural logarithm.
x | a double |
|
staticprivate |
Internal helper method for natural logarithm function.
x | original argument of the natural logarithm function |
hiPrec | extra bits of precision on output (To Be Confirmed) |
|
static |
Compute the base 10 logarithm.
x | a number |
|
static |
Compute log(1 + x).
x | a number |
|
static |
Compute the maximum of two values
a | first value |
b | second value |
|
static |
Compute the maximum of two values
a | first value |
b | second value |
|
static |
Compute the maximum of two values
a | first value |
b | second value |
|
static |
Compute the maximum of two values
a | first value |
b | second value |
|
static |
Compute the minimum of two values
a | first value |
b | second value |
|
static |
Compute the minimum of two values
a | first value |
b | second value |
|
static |
Compute the minimum of two values
a | first value |
b | second value |
|
static |
Compute the minimum of two values
a | first value |
b | second value |
|
static |
Get the next machine representable number after a number, moving in the direction of another number.
The ordering is as follows (increasing):
If arguments compare equal, then the second argument is returned.
If direction
is greater than d
, the smallest machine representable number strictly greater than d
is returned; if less, then the largest representable number strictly less than d
is returned.
If d
is infinite and direction does not bring it back to finite numbers, it is returned unchanged.
d | base number |
direction | (the only important thing is whether direction is greater or smaller than d ) |
|
static |
Get the next machine representable number after a number, moving in the direction of another number.
The ordering is as follows (increasing):
If arguments compare equal, then the second argument is returned.
If direction
is greater than f
, the smallest machine representable number strictly greater than f
is returned; if less, then the largest representable number strictly less than f
is returned.
If f
is infinite and direction does not bring it back to finite numbers, it is returned unchanged.
f | base number |
direction | (the only important thing is whether direction is greater or smaller than f ) |
|
static |
Compute next number towards positive infinity.
a | number to which neighbor should be computed |
|
static |
Compute next number towards positive infinity.
a | number to which neighbor should be computed |
|
staticprivate |
Computes cos(x) - 1, where |x| < 1/16. Use a Remez polynomial approximation.
x | a number smaller than 1/16 |
|
staticprivate |
Computes sin(x) - x, where |x| < 1/16. Use a Remez polynomial approximation.
x | a number smaller than 1/16 |
|
static |
Power function. Compute x^y.
x | a double |
y | a double |
|
staticprivate |
Compute (a[0] + a[1]) * (b[0] + b[1]) in extended precision.
a | first term of the multiplication |
b | second term of the multiplication |
result | placeholder where to put the result |
|
static |
Returns a pseudo-random number between 0.0 and 1.0.
Note: this implementation currently delegates to Math#random
|
staticprivate |
Reduce the input argument using the Payne and Hanek method. This is good for all inputs 0.0 < x < inf Output is remainder after dividing by PI/2 The result array should contain 3 numbers. result[0] is the integer portion, so mod 4 this gives the quadrant. result[1] is the upper bits of the remainder result[2] is the lower bits of the remainder
x | number to reduce |
result | placeholder where to put the result |
|
staticprivate |
Recompute a split.
a | input/out array containing the split, changed on output |
|
static |
Get the whole number that is the nearest to x, or the even one if x is exactly half way between two integers.
x | number from which nearest whole number is requested |
|
static |
Get the closest long to x.
x | number from which closest long is requested |
|
static |
Get the closest int to x.
x | number from which closest int is requested |
|
static |
Multiply a double number by a power of 2.
d | number to multiply |
n | power of 2 |
|
static |
Multiply a float number by a power of 2.
f | number to multiply |
n | power of 2 |
|
static |
Compute the signum of a number. The signum is -1 for negative numbers, +1 for positive numbers and 0 otherwise
a | number on which evaluation is done |
|
static |
Compute the signum of a number. The signum is -1 for negative numbers, +1 for positive numbers and 0 otherwise
a | number on which evaluation is done |
|
static |
Sine function.
x | a number |
|
static |
Compute the hyperbolic sine of a number.
x | number on which evaluation is done |
|
staticprivate |
Compute sine over the first quadrant (0 < x < pi/2). Use combination of table lookup and rational polynomial expansion.
xa | number from which sine is requested |
xb | extra bits for x (may be 0.0) |
|
staticprivate |
For x between 0 and pi/4 compute cosine
x | number from which cosine is requested |
result | placeholder where to put the result in extended precision |
|
staticprivate |
For x between 0 and 1, returns exp(x), uses extended precision
x | argument of exponential |
result | placeholder where to place exp(x) split in two terms for extra precision (i.e. exp(x) = result[0] result[1] |
|
staticprivate |
xi in the range of [1, 2]. 3 5 7 x+1 / x x x \ ln --— = 2 * | x + -— + -— + -— + ... | 1-x \ 3 5 7 /
So, compute a Remez approximation of the following function
ln ((sqrt(x)+1)/(1-sqrt(x))) / x
This will be an even function with only positive coefficents. x is in the range [0 - 1/3].
Transform xi for input to the above function by setting x = (xi-1)/(xi+1). Input to the polynomial is x^2, then the result is multiplied by x.
xi | number from which log is requested |
|
staticprivate |
For x between 0 and pi/4 compute sine.
x | number from which sine is requested |
result | placeholder where to put the result in extended precision |
|
staticprivate |
Compute split[0], split[1] such that their sum is equal to d, and split[0] has its 30 least significant bits as zero.
d | number to split |
split | placeholder where to place the result |
|
staticprivate |
Add two numbers in split form.
a | first term of addition |
b | second term of addition |
ans | placeholder where to put the result |
|
staticprivate |
Multiply two numbers in split form.
a | first term of multiplication |
b | second term of multiplication |
ans | placeholder where to put the result |
|
staticprivate |
Compute the reciprocal of in. Use the following algorithm. in = c + d. want to find x + y such that x+y = 1/(c+d) and x is much larger than y and x has several zero bits on the right.
Set b = 1/(2^22), a = 1 - b. Thus (a+b) = 1. Use following identity to compute (a+b)/(c+d)
(a+b)/(c+d) = a/c + (bc - ad) / (c^2 + cd) set x = a/c and y = (bc - ad) / (c^2 + cd) This will be close to the right answer, but there will be some rounding in the calculation of X. So by carefully computing 1 - (c+d)(x+y) we can compute an error and add that back in. This is done carefully so that terms of similar size are subtracted first.
in | initial number, in split form |
result | placeholder where to put the result |
|
static |
Compute the square root of a number.
Note: this implementation currently delegates to Math#sqrt
a | number on which evaluation is done |
|
static |
Tangent function
x | a number |
|
static |
Compute the hyperbolic tangent of a number.
x | number on which evaluation is done |
|
staticprivate |
Compute tangent (or cotangent) over the first quadrant. 0 < x < pi/2 Use combination of table lookup and rational polynomial expansion.
xa | number from which sine is requested |
xb | extra bits for x (may be 0.0) |
cotanFlag | if true, compute the cotangent instead of the tangent |
|
static |
Convert radians to degrees, with error of less than 0.5 ULP
x | angle in radians |
|
static |
Convert degrees to radians, with error of less than 0.5 ULP
x | angle in degrees |
|
static |
Compute least significant bit (Unit in Last Position) for a number.
x | number from which ulp is requested |
|
static |
Compute least significant bit (Unit in Last Position) for a number.
x | number from which ulp is requested |
|
staticprivate |
Table of 2^((n+2)/3)
|
staticprivate |
Cosine table (high bits).
|
staticprivate |
Cosine table (low bits).
|
static |
Napier's constant e, base of the natural logarithm.
|
staticprivate |
Eighths. This is used by sinQ, because its faster to do a table lookup than a multiply in this time-critical routine
|
staticprivate |
Exponential over the range of 0 - 1 in increments of 2^-10 exp(x/1024) = expFracTableA[x] + expFracTableB[x].
|
staticprivate |
Exponential over the range of 0 - 1 in increments of 2^-10 exp(x/1024) = expFracTableA[x] + expFracTableB[x].
|
staticprivate |
Exponential evaluated at integer values, exp(x) = expIntTableA[x + 750] + expIntTableB[x+750].
|
staticprivate |
Exponential evaluated at integer values, exp(x) = expIntTableA[x + 750] + expIntTableB[x+750]
|
staticprivate |
Factorial table, for Taylor series expansions.
|
staticprivate |
0x40000000 - used to split a double into two parts, both with the low order bits cleared. Equivalent to 2^30.
|
staticprivate |
log(2) (high bits).
|
staticprivate |
log(2) (low bits).
|
staticprivate |
Coefficients for log in the range of 1.0 < x < 1.0 + 2^-10.
|
staticprivate |
Extended precision logarithm table over the range 1 - 2 in increments of 2^-10.
|
staticprivate |
Coefficients for log, when input 0.99 < x < 1.01.
|
staticprivate |
Coefficients for slowLog.
|
staticprivate |
Mask used to clear low order 30 bits
|
static |
Archimede's constant PI, ratio of circle circumference to diameter.
|
staticprivate |
Bits of pi/4, need for reducePayneHanek().
|
staticprivate |
Bits of 1/(2*pi), need for reducePayneHanek().
|
static |
Safe minimum, such that 1 / SAFE_MIN does not overflow.
In IEEE 754 arithmetic, this is also the smallest normalized number 2-1022.
|
staticprivate |
Sine table (high bits).
|
staticprivate |
Sine table (low bits).
|
staticprivate |
Tangent table, used by atan() (high bits).
|
staticprivate |
Tangent table, used by atan() (low bits).
|
staticprivate |
2^52 - double numbers this large must be integral (no fraction) or NaN or Infinite