AI for Games 1.1.1
Loading...
Searching...
No Matches
game2dai.maths.FastMath Class Reference

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
 

Detailed Description

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

Since
2.2

Constructor & Destructor Documentation

◆ FastMath()

game2dai.maths.FastMath.FastMath ( )
private

Private Constructor

Member Function Documentation

◆ abs() [1/4]

static double game2dai.maths.FastMath.abs ( double  x)
static

Absolute value.

Parameters
xnumber from which absolute value is requested
Returns
abs(x)

◆ abs() [2/4]

static float game2dai.maths.FastMath.abs ( final float  x)
static

Absolute value.

Parameters
xnumber from which absolute value is requested
Returns
abs(x)

◆ abs() [3/4]

static int game2dai.maths.FastMath.abs ( final int  x)
static

Absolute value.

Parameters
xnumber from which absolute value is requested
Returns
abs(x)

◆ abs() [4/4]

static long game2dai.maths.FastMath.abs ( final long  x)
static

Absolute value.

Parameters
xnumber from which absolute value is requested
Returns
abs(x)

◆ acos()

static double game2dai.maths.FastMath.acos ( double  x)
static

Compute the arc cosine of a number.

Parameters
xnumber on which evaluation is done
Returns
arc cosine of x

◆ acosh()

static double game2dai.maths.FastMath.acosh ( final double  a)
static

Compute the inverse hyperbolic cosine of a number.

Parameters
anumber on which evaluation is done
Returns
inverse hyperbolic cosine of a

◆ asin()

static double game2dai.maths.FastMath.asin ( double  x)
static

Compute the arc sine of a number.

Parameters
xnumber on which evaluation is done
Returns
arc sine of x

◆ asinh()

static double game2dai.maths.FastMath.asinh ( double  a)
static

Compute the inverse hyperbolic sine of a number.

Parameters
anumber on which evaluation is done
Returns
inverse hyperbolic sine of a

◆ atan() [1/2]

static double game2dai.maths.FastMath.atan ( double  x)
static

Arctangent function

Parameters
xa number
Returns
atan(x)

◆ atan() [2/2]

static double game2dai.maths.FastMath.atan ( double  xa,
double  xb,
boolean  leftPlane 
)
staticprivate

Internal helper function to compute arctangent.

Parameters
xanumber from which arctangent is requested
xbextra bits for x (may be 0.0)
leftPlaneif true, result angle must be put in the left half plane
Returns
atan(xa + xb) (or angle shifted by PI if leftPlane is true)

◆ atan2()

static double game2dai.maths.FastMath.atan2 ( double  y,
double  x 
)
static

Two arguments arctangent function

Parameters
yordinate
xabscissa
Returns
phase angle of point (x,y) between -PI and PI

◆ atanh()

static double game2dai.maths.FastMath.atanh ( double  a)
static

Compute the inverse hyperbolic tangent of a number.

Parameters
anumber on which evaluation is done
Returns
inverse hyperbolic tangent of a

◆ buildSinCosTables()

static void game2dai.maths.FastMath.buildSinCosTables ( )
staticprivate

Build the sine and cosine tables.

◆ cbrt()

static double game2dai.maths.FastMath.cbrt ( double  x)
static

Compute the cubic root of a number.

Parameters
xnumber on which evaluation is done
Returns
cubic root of x

◆ ceil()

static double game2dai.maths.FastMath.ceil ( double  x)
static

Get the smallest whole number larger than x.

Parameters
xnumber from which ceil is requested
Returns
a double number c such that c is an integer c - 1.0 < x <= c

◆ copySign() [1/2]

static double game2dai.maths.FastMath.copySign ( double  magnitude,
double  sign 
)
static

Returns the first argument with the sign of the second argument. A NaN sign argument is treated as positive.

Parameters
magnitudethe value to return
signthe sign for the returned value
Returns
the magnitude with the same sign as the sign argument

◆ copySign() [2/2]

static float game2dai.maths.FastMath.copySign ( float  magnitude,
float  sign 
)
static

Returns the first argument with the sign of the second argument. A NaN sign argument is treated as positive.

Parameters
magnitudethe value to return
signthe sign for the returned value
Returns
the magnitude with the same sign as the sign argument

◆ cos()

static double game2dai.maths.FastMath.cos ( double  x)
static

Cosine function

Parameters
xa number
Returns
cos(x)

◆ cosh()

static double game2dai.maths.FastMath.cosh ( double  x)
static

Compute the hyperbolic cosine of a number.

Parameters
xnumber on which evaluation is done
Returns
hyperbolic cosine of x

◆ cosQ()

static double game2dai.maths.FastMath.cosQ ( double  xa,
double  xb 
)
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.

Parameters
xanumber from which cosine is requested
xbextra bits for x (may be 0.0)
Returns
cos(xa + xb)

◆ doubleHighPart()

static double game2dai.maths.FastMath.doubleHighPart ( double  d)
staticprivate

Get the high order bits from the mantissa. Equivalent to adding and subtracting HEX_40000 but also works for very large numbers

Parameters
dthe value to split
Returns
the high order part of the mantissa

◆ exp() [1/2]

static double game2dai.maths.FastMath.exp ( double  x)
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.

Parameters
xa double
Returns
double ex

◆ exp() [2/2]

static double game2dai.maths.FastMath.exp ( double  x,
double  extra,
double[]  hiPrec 
)
staticprivate

Internal helper method for exponential function.

Parameters
xoriginal argument of the exponential function
extraextra bits of precision on input (To Be Confirmed)
hiPrecextra bits of precision on output (To Be Confirmed)
Returns
exp(x)

◆ expint()

static double game2dai.maths.FastMath.expint ( int  p,
final double  result[] 
)
staticprivate

Compute exp(p) for a integer p in extended precision.

Parameters
pinteger whose exponential is requested
resultplaceholder where to put the result in extended precision
Returns
exp(p) in standard precision (equal to result[0] + result[1])

◆ expm1() [1/2]

static double game2dai.maths.FastMath.expm1 ( double  x)
static

Compute exp(x) - 1

Parameters
xnumber to compute shifted exponential
Returns
exp(x) - 1

◆ expm1() [2/2]

static double game2dai.maths.FastMath.expm1 ( double  x,
double  hiPrecOut[] 
)
staticprivate

Internal helper method for expm1

Parameters
xnumber to compute shifted exponential
hiPrecOutreceive high precision result for -1.0 < x < 1.0
Returns
exp(x) - 1

◆ floor()

static double game2dai.maths.FastMath.floor ( double  x)
static

Get the largest whole number smaller than x.

Parameters
xnumber from which floor is requested
Returns
a double number f such that f is an integer f <= x < f + 1.0

◆ getExponent() [1/2]

static int game2dai.maths.FastMath.getExponent ( final double  d)
static

Return the exponent of a double number, removing the bias.

For double numbers of the form 2x, the unbiased exponent is exactly x.

Parameters
dnumber from which exponent is requested
Returns
exponent for d in IEEE754 representation, without bias

◆ getExponent() [2/2]

static int game2dai.maths.FastMath.getExponent ( final float  f)
static

Return the exponent of a float number, removing the bias.

For float numbers of the form 2x, the unbiased exponent is exactly x.

Parameters
fnumber from which exponent is requested
Returns
exponent for d in IEEE754 representation, without bias

◆ hypot()

static double game2dai.maths.FastMath.hypot ( final double  x,
final double  y 
)
static

Returns the hypotenuse of a triangle with sides x and y

  • sqrt(x2 +y2)
    avoiding intermediate overflow or underflow.
  • If either argument is infinite, then the result is positive infinity.
  • else, if either argument is NaN then the result is NaN.
Parameters
xa value
ya value
Returns
sqrt(x2 +y2)

◆ IEEEremainder()

static double game2dai.maths.FastMath.IEEEremainder ( double  dividend,
double  divisor 
)
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.

  • If either operand is NaN, the result is NaN.
  • If the result is not NaN, the sign of the result equals the sign of the dividend.
  • If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN.
  • If the dividend is finite and the divisor is an infinity, the result equals the dividend.
  • If the dividend is a zero and the divisor is finite, the result equals the dividend.

Note: this implementation currently delegates to StrictMath#IEEEremainder

Parameters
dividendthe number to be divided
divisorthe number by which to divide
Returns
the remainder, rounded

◆ log() [1/2]

static double game2dai.maths.FastMath.log ( final double  x)
static

Natural logarithm.

Parameters
xa double
Returns
log(x)

◆ log() [2/2]

static double game2dai.maths.FastMath.log ( final double  x,
final double[]  hiPrec 
)
staticprivate

Internal helper method for natural logarithm function.

Parameters
xoriginal argument of the natural logarithm function
hiPrecextra bits of precision on output (To Be Confirmed)
Returns
log(x)

◆ log10()

static double game2dai.maths.FastMath.log10 ( final double  x)
static

Compute the base 10 logarithm.

Parameters
xa number
Returns
log10(x)

◆ log1p()

static double game2dai.maths.FastMath.log1p ( final double  x)
static

Compute log(1 + x).

Parameters
xa number
Returns
log(1 + x)

◆ max() [1/4]

static double game2dai.maths.FastMath.max ( final double  a,
final double  b 
)
static

Compute the maximum of two values

Parameters
afirst value
bsecond value
Returns
b if a is lesser or equal to b, a otherwise

◆ max() [2/4]

static float game2dai.maths.FastMath.max ( final float  a,
final float  b 
)
static

Compute the maximum of two values

Parameters
afirst value
bsecond value
Returns
b if a is lesser or equal to b, a otherwise

◆ max() [3/4]

static int game2dai.maths.FastMath.max ( final int  a,
final int  b 
)
static

Compute the maximum of two values

Parameters
afirst value
bsecond value
Returns
b if a is lesser or equal to b, a otherwise

◆ max() [4/4]

static long game2dai.maths.FastMath.max ( final long  a,
final long  b 
)
static

Compute the maximum of two values

Parameters
afirst value
bsecond value
Returns
b if a is lesser or equal to b, a otherwise

◆ min() [1/4]

static double game2dai.maths.FastMath.min ( final double  a,
final double  b 
)
static

Compute the minimum of two values

Parameters
afirst value
bsecond value
Returns
a if a is lesser or equal to b, b otherwise

◆ min() [2/4]

static float game2dai.maths.FastMath.min ( final float  a,
final float  b 
)
static

Compute the minimum of two values

Parameters
afirst value
bsecond value
Returns
a if a is lesser or equal to b, b otherwise

◆ min() [3/4]

static int game2dai.maths.FastMath.min ( final int  a,
final int  b 
)
static

Compute the minimum of two values

Parameters
afirst value
bsecond value
Returns
a if a is lesser or equal to b, b otherwise

◆ min() [4/4]

static long game2dai.maths.FastMath.min ( final long  a,
final long  b 
)
static

Compute the minimum of two values

Parameters
afirst value
bsecond value
Returns
a if a is lesser or equal to b, b otherwise

◆ nextAfter() [1/2]

static double game2dai.maths.FastMath.nextAfter ( double  d,
double  direction 
)
static

Get the next machine representable number after a number, moving in the direction of another number.

The ordering is as follows (increasing):

  • -INFINITY
  • -MAX_VALUE
  • -MIN_VALUE
  • -0.0
  • +0.0
  • +MIN_VALUE
  • +MAX_VALUE
  • +INFINITY
  • 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.

    Parameters
    dbase number
    direction(the only important thing is whether direction is greater or smaller than d)
    Returns
    the next machine representable number in the specified direction

◆ nextAfter() [2/2]

static float game2dai.maths.FastMath.nextAfter ( final float  f,
final double  direction 
)
static

Get the next machine representable number after a number, moving in the direction of another number.

The ordering is as follows (increasing):

  • -INFINITY
  • -MAX_VALUE
  • -MIN_VALUE
  • -0.0
  • +0.0
  • +MIN_VALUE
  • +MAX_VALUE
  • +INFINITY
  • 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.

    Parameters
    fbase number
    direction(the only important thing is whether direction is greater or smaller than f)
    Returns
    the next machine representable number in the specified direction

◆ nextUp() [1/2]

static double game2dai.maths.FastMath.nextUp ( final double  a)
static

Compute next number towards positive infinity.

Parameters
anumber to which neighbor should be computed
Returns
neighbor of a towards positive infinity

◆ nextUp() [2/2]

static float game2dai.maths.FastMath.nextUp ( final float  a)
static

Compute next number towards positive infinity.

Parameters
anumber to which neighbor should be computed
Returns
neighbor of a towards positive infinity

◆ polyCosine()

static double game2dai.maths.FastMath.polyCosine ( double  x)
staticprivate

Computes cos(x) - 1, where |x| < 1/16. Use a Remez polynomial approximation.

Parameters
xa number smaller than 1/16
Returns
cos(x) - 1

◆ polySine()

static double game2dai.maths.FastMath.polySine ( final double  x)
staticprivate

Computes sin(x) - x, where |x| < 1/16. Use a Remez polynomial approximation.

Parameters
xa number smaller than 1/16
Returns
sin(x) - x

◆ pow()

static double game2dai.maths.FastMath.pow ( double  x,
double  y 
)
static

Power function. Compute x^y.

Parameters
xa double
ya double
Returns
double

◆ quadMult()

static void game2dai.maths.FastMath.quadMult ( final double  a[],
final double  b[],
final double  result[] 
)
staticprivate

Compute (a[0] + a[1]) * (b[0] + b[1]) in extended precision.

Parameters
afirst term of the multiplication
bsecond term of the multiplication
resultplaceholder where to put the result

◆ random()

static double game2dai.maths.FastMath.random ( )
static

Returns a pseudo-random number between 0.0 and 1.0.

Note: this implementation currently delegates to Math#random

Returns
a random number between 0.0 and 1.0

◆ reducePayneHanek()

static void game2dai.maths.FastMath.reducePayneHanek ( double  x,
double  result[] 
)
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

Parameters
xnumber to reduce
resultplaceholder where to put the result

◆ resplit()

static void game2dai.maths.FastMath.resplit ( final double  a[])
staticprivate

Recompute a split.

Parameters
ainput/out array containing the split, changed on output

◆ rint()

static double game2dai.maths.FastMath.rint ( double  x)
static

Get the whole number that is the nearest to x, or the even one if x is exactly half way between two integers.

Parameters
xnumber from which nearest whole number is requested
Returns
a double number r such that r is an integer r - 0.5 <= x <= r + 0.5

◆ round() [1/2]

static long game2dai.maths.FastMath.round ( double  x)
static

Get the closest long to x.

Parameters
xnumber from which closest long is requested
Returns
closest long to x

◆ round() [2/2]

static int game2dai.maths.FastMath.round ( final float  x)
static

Get the closest int to x.

Parameters
xnumber from which closest int is requested
Returns
closest int to x

◆ scalb() [1/2]

static double game2dai.maths.FastMath.scalb ( final double  d,
final int  n 
)
static

Multiply a double number by a power of 2.

Parameters
dnumber to multiply
npower of 2
Returns
d × 2n

◆ scalb() [2/2]

static float game2dai.maths.FastMath.scalb ( final float  f,
final int  n 
)
static

Multiply a float number by a power of 2.

Parameters
fnumber to multiply
npower of 2
Returns
f × 2n

◆ signum() [1/2]

static double game2dai.maths.FastMath.signum ( final double  a)
static

Compute the signum of a number. The signum is -1 for negative numbers, +1 for positive numbers and 0 otherwise

Parameters
anumber on which evaluation is done
Returns
-1.0, -0.0, +0.0, +1.0 or NaN depending on sign of a

◆ signum() [2/2]

static float game2dai.maths.FastMath.signum ( final float  a)
static

Compute the signum of a number. The signum is -1 for negative numbers, +1 for positive numbers and 0 otherwise

Parameters
anumber on which evaluation is done
Returns
-1.0, -0.0, +0.0, +1.0 or NaN depending on sign of a

◆ sin()

static double game2dai.maths.FastMath.sin ( double  x)
static

Sine function.

Parameters
xa number
Returns
sin(x)

◆ sinh()

static double game2dai.maths.FastMath.sinh ( double  x)
static

Compute the hyperbolic sine of a number.

Parameters
xnumber on which evaluation is done
Returns
hyperbolic sine of x

◆ sinQ()

static double game2dai.maths.FastMath.sinQ ( double  xa,
double  xb 
)
staticprivate

Compute sine over the first quadrant (0 < x < pi/2). Use combination of table lookup and rational polynomial expansion.

Parameters
xanumber from which sine is requested
xbextra bits for x (may be 0.0)
Returns
sin(xa + xb)

◆ slowCos()

static double game2dai.maths.FastMath.slowCos ( final double  x,
final double  result[] 
)
staticprivate

For x between 0 and pi/4 compute cosine

Parameters
xnumber from which cosine is requested
resultplaceholder where to put the result in extended precision
Returns
cos(x)

◆ slowexp()

static double game2dai.maths.FastMath.slowexp ( final double  x,
final double  result[] 
)
staticprivate

For x between 0 and 1, returns exp(x), uses extended precision

Parameters
xargument of exponential
resultplaceholder where to place exp(x) split in two terms for extra precision (i.e. exp(x) = result[0] result[1]
Returns
exp(x)

◆ slowLog()

static double[] game2dai.maths.FastMath.slowLog ( double  xi)
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.

Parameters
xinumber from which log is requested
Returns
log(xi)

◆ slowSin()

static double game2dai.maths.FastMath.slowSin ( final double  x,
final double  result[] 
)
staticprivate

For x between 0 and pi/4 compute sine.

Parameters
xnumber from which sine is requested
resultplaceholder where to put the result in extended precision
Returns
sin(x)

◆ split()

static void game2dai.maths.FastMath.split ( final double  d,
final double  split[] 
)
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.

Parameters
dnumber to split
splitplaceholder where to place the result

◆ splitAdd()

static void game2dai.maths.FastMath.splitAdd ( final double  a[],
final double  b[],
final double  ans[] 
)
staticprivate

Add two numbers in split form.

Parameters
afirst term of addition
bsecond term of addition
ansplaceholder where to put the result

◆ splitMult()

static void game2dai.maths.FastMath.splitMult ( double  a[],
double  b[],
double  ans[] 
)
staticprivate

Multiply two numbers in split form.

Parameters
afirst term of multiplication
bsecond term of multiplication
ansplaceholder where to put the result

◆ splitReciprocal()

static void game2dai.maths.FastMath.splitReciprocal ( final double  in[],
final double  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.

Parameters
ininitial number, in split form
resultplaceholder where to put the result

◆ sqrt()

static double game2dai.maths.FastMath.sqrt ( final double  a)
static

Compute the square root of a number.

Note: this implementation currently delegates to Math#sqrt

Parameters
anumber on which evaluation is done
Returns
square root of a

◆ tan()

static double game2dai.maths.FastMath.tan ( double  x)
static

Tangent function

Parameters
xa number
Returns
tan(x)

◆ tanh()

static double game2dai.maths.FastMath.tanh ( double  x)
static

Compute the hyperbolic tangent of a number.

Parameters
xnumber on which evaluation is done
Returns
hyperbolic tangent of x

◆ tanQ()

static double game2dai.maths.FastMath.tanQ ( double  xa,
double  xb,
boolean  cotanFlag 
)
staticprivate

Compute tangent (or cotangent) over the first quadrant. 0 < x < pi/2 Use combination of table lookup and rational polynomial expansion.

Parameters
xanumber from which sine is requested
xbextra bits for x (may be 0.0)
cotanFlagif true, compute the cotangent instead of the tangent
Returns
tan(xa+xb) (or cotangent, depending on cotanFlag)

◆ toDegrees()

static double game2dai.maths.FastMath.toDegrees ( double  x)
static

Convert radians to degrees, with error of less than 0.5 ULP

Parameters
xangle in radians
Returns
x converted into degrees

◆ toRadians()

static double game2dai.maths.FastMath.toRadians ( double  x)
static

Convert degrees to radians, with error of less than 0.5 ULP

Parameters
xangle in degrees
Returns
x converted into radians

◆ ulp() [1/2]

static double game2dai.maths.FastMath.ulp ( double  x)
static

Compute least significant bit (Unit in Last Position) for a number.

Parameters
xnumber from which ulp is requested
Returns
ulp(x)

◆ ulp() [2/2]

static float game2dai.maths.FastMath.ulp ( float  x)
static

Compute least significant bit (Unit in Last Position) for a number.

Parameters
xnumber from which ulp is requested
Returns
ulp(x)

Member Data Documentation

◆ CBRTTWO

final double game2dai.maths.FastMath.CBRTTWO[]
staticprivate
Initial value:
= { 0.6299605249474366,
0.7937005259840998,
1.0,
1.2599210498948732,
1.5874010519681994 }

Table of 2^((n+2)/3)

◆ COSINE_TABLE_A

final double game2dai.maths.FastMath.COSINE_TABLE_A[] = new double[14]
staticprivate

Cosine table (high bits).

◆ COSINE_TABLE_B

final double game2dai.maths.FastMath.COSINE_TABLE_B[] = new double[14]
staticprivate

Cosine table (low bits).

◆ E

final double game2dai.maths.FastMath.E = 2850325.0 / 1048576.0 + 8.254840070411028747e-8
static

Napier's constant e, base of the natural logarithm.

◆ EIGHTHS

final double game2dai.maths.FastMath.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}
staticprivate

Eighths. This is used by sinQ, because its faster to do a table lookup than a multiply in this time-critical routine

◆ EXP_FRAC_TABLE_A

final double game2dai.maths.FastMath.EXP_FRAC_TABLE_A[] = new double[1025]
staticprivate

Exponential over the range of 0 - 1 in increments of 2^-10 exp(x/1024) = expFracTableA[x] + expFracTableB[x].

◆ EXP_FRAC_TABLE_B

final double game2dai.maths.FastMath.EXP_FRAC_TABLE_B[] = new double[1025]
staticprivate

Exponential over the range of 0 - 1 in increments of 2^-10 exp(x/1024) = expFracTableA[x] + expFracTableB[x].

◆ EXP_INT_TABLE_A

final double game2dai.maths.FastMath.EXP_INT_TABLE_A[] = new double[1500]
staticprivate

Exponential evaluated at integer values, exp(x) = expIntTableA[x + 750] + expIntTableB[x+750].

◆ EXP_INT_TABLE_B

final double game2dai.maths.FastMath.EXP_INT_TABLE_B[] = new double[1500]
staticprivate

Exponential evaluated at integer values, exp(x) = expIntTableA[x + 750] + expIntTableB[x+750]

◆ FACT

final double game2dai.maths.FastMath.FACT[] = new double[20]
staticprivate

Factorial table, for Taylor series expansions.

◆ HEX_40000000

final long game2dai.maths.FastMath.HEX_40000000 = 0x40000000L
staticprivate

0x40000000 - used to split a double into two parts, both with the low order bits cleared. Equivalent to 2^30.

◆ LN_2_A

final double game2dai.maths.FastMath.LN_2_A = 0.693147063255310059
staticprivate

log(2) (high bits).

◆ LN_2_B

final double game2dai.maths.FastMath.LN_2_B = 1.17304635250823482e-7
staticprivate

log(2) (low bits).

◆ LN_HI_PREC_COEF

final double game2dai.maths.FastMath.LN_HI_PREC_COEF[][]
staticprivate
Initial value:
= {
{1.0, -6.032174644509064E-23},
{-0.25, -0.25},
{0.3333333134651184, 1.9868161777724352E-8},
{-0.2499999701976776, -2.957007209750105E-8},
{0.19999954104423523, 1.5830993332061267E-10},
{-0.16624879837036133, -2.6033824355191673E-8}
}

Coefficients for log in the range of 1.0 < x < 1.0 + 2^-10.

◆ LN_MANT

final double game2dai.maths.FastMath.LN_MANT[][] = new double[1024][]
staticprivate

Extended precision logarithm table over the range 1 - 2 in increments of 2^-10.

◆ LN_QUICK_COEF

final double game2dai.maths.FastMath.LN_QUICK_COEF[][]
staticprivate
Initial value:
= {
{1.0, 5.669184079525E-24},
{-0.25, -0.25},
{0.3333333134651184, 1.986821492305628E-8},
{-0.25, -6.663542893624021E-14},
{0.19999998807907104, 1.1921056801463227E-8},
{-0.1666666567325592, -7.800414592973399E-9},
{0.1428571343421936, 5.650007086920087E-9},
{-0.12502530217170715, -7.44321345601866E-11},
{0.11113807559013367, 9.219544613762692E-9},
}

Coefficients for log, when input 0.99 < x < 1.01.

◆ LN_SPLIT_COEF

final double game2dai.maths.FastMath.LN_SPLIT_COEF[][]
staticprivate
Initial value:
= {
{2.0, 0.0},
{0.6666666269302368, 3.9736429850260626E-8},
{0.3999999761581421, 2.3841857910019882E-8},
{0.2857142686843872, 1.7029898543501842E-8},
{0.2222222089767456, 1.3245471311735498E-8},
{0.1818181574344635, 2.4384203044354907E-8},
{0.1538461446762085, 9.140260083262505E-9},
{0.13333332538604736, 9.220590270857665E-9},
{0.11764700710773468, 1.2393345855018391E-8},
{0.10526403784751892, 8.251545029714408E-9},
{0.0952233225107193, 1.2675934823758863E-8},
{0.08713622391223907, 1.1430250008909141E-8},
{0.07842259109020233, 2.404307984052299E-9},
{0.08371849358081818, 1.176342548272881E-8},
{0.030589580535888672, 1.2958646899018938E-9},
{0.14982303977012634, 1.225743062930824E-8},
}

Coefficients for slowLog.

◆ MASK_30BITS

final long game2dai.maths.FastMath.MASK_30BITS = -1L - (HEX_40000000 -1)
staticprivate

Mask used to clear low order 30 bits

◆ PI

final double game2dai.maths.FastMath.PI = 105414357.0 / 33554432.0 + 1.984187159361080883e-9
static

Archimede's constant PI, ratio of circle circumference to diameter.

◆ PI_O_4_BITS

final long game2dai.maths.FastMath.PI_O_4_BITS[]
staticprivate
Initial value:
= new long[] {
(0xc90fdaa2L << 32) | 0x2168c234L,
(0xc4c6628bL << 32) | 0x80dc1cd1L }

Bits of pi/4, need for reducePayneHanek().

◆ RECIP_2PI

final long game2dai.maths.FastMath.RECIP_2PI[]
staticprivate
Initial value:
= new long[] {
(0x28be60dbL << 32) | 0x9391054aL,
(0x7f09d5f4L << 32) | 0x7d4d3770L,
(0x36d8a566L << 32) | 0x4f10e410L,
(0x7f9458eaL << 32) | 0xf7aef158L,
(0x6dc91b8eL << 32) | 0x909374b8L,
(0x01924bbaL << 32) | 0x82746487L,
(0x3f877ac7L << 32) | 0x2c4a69cfL,
(0xba208d7dL << 32) | 0x4baed121L,
(0x3a671c09L << 32) | 0xad17df90L,
(0x4e64758eL << 32) | 0x60d4ce7dL,
(0x272117e2L << 32) | 0xef7e4a0eL,
(0xc7fe25ffL << 32) | 0xf7816603L,
(0xfbcbc462L << 32) | 0xd6829b47L,
(0xdb4d9fb3L << 32) | 0xc9f2c26dL,
(0xd3d18fd9L << 32) | 0xa797fa8bL,
(0x5d49eeb1L << 32) | 0xfaf97c5eL,
(0xcf41ce7dL << 32) | 0xe294a4baL,
0x9afed7ecL << 32 }

Bits of 1/(2*pi), need for reducePayneHanek().

◆ SAFE_MIN

final double game2dai.maths.FastMath.SAFE_MIN = 0x1.0p-1022
static

Safe minimum, such that 1 / SAFE_MIN does not overflow.

In IEEE 754 arithmetic, this is also the smallest normalized number 2-1022.

◆ SINE_TABLE_A

final double game2dai.maths.FastMath.SINE_TABLE_A[] = new double[14]
staticprivate

Sine table (high bits).

◆ SINE_TABLE_B

final double game2dai.maths.FastMath.SINE_TABLE_B[] = new double[14]
staticprivate

Sine table (low bits).

◆ TANGENT_TABLE_A

final double game2dai.maths.FastMath.TANGENT_TABLE_A[] = new double[14]
staticprivate

Tangent table, used by atan() (high bits).

◆ TANGENT_TABLE_B

final double game2dai.maths.FastMath.TANGENT_TABLE_B[] = new double[14]
staticprivate

Tangent table, used by atan() (low bits).

◆ TWO_POWER_52

final double game2dai.maths.FastMath.TWO_POWER_52 = 4503599627370496.0
staticprivate

2^52 - double numbers this large must be integral (no fraction) or NaN or Infinite


The documentation for this class was generated from the following file: