public class FastMath
extends java.lang.Object
StrictMath.
Additionally implements the following methods not found in StrictMath:
The following methods are found in StrictMath since 1.6 only| Modifier and Type | Field and Description |
|---|---|
private static double[] |
CBRTTWO
Table of 2^((n+2)/3)
|
private static double[] |
COSINE_TABLE_A
Cosine table (high bits).
|
private static double[] |
COSINE_TABLE_B
Cosine table (low bits).
|
static double |
E
Napier's constant e, base of the natural logarithm.
|
private static double[] |
EIGHTHS
Eighths.
|
private static double[] |
EXP_FRAC_TABLE_A
Exponential over the range of 0 - 1 in increments of 2^-10
exp(x/1024) = expFracTableA[x] + expFracTableB[x].
|
private static double[] |
EXP_FRAC_TABLE_B
Exponential over the range of 0 - 1 in increments of 2^-10
exp(x/1024) = expFracTableA[x] + expFracTableB[x].
|
private static double[] |
EXP_INT_TABLE_A
Exponential evaluated at integer values,
exp(x) = expIntTableA[x + 750] + expIntTableB[x+750].
|
private static double[] |
EXP_INT_TABLE_B
Exponential evaluated at integer values,
exp(x) = expIntTableA[x + 750] + expIntTableB[x+750]
|
private static double[] |
FACT
Factorial table, for Taylor series expansions.
|
private static long |
HEX_40000000
0x40000000 - used to split a double into two parts, both with the low order bits cleared.
|
private static double |
LN_2_A
log(2) (high bits).
|
private static double |
LN_2_B
log(2) (low bits).
|
private static double[][] |
LN_HI_PREC_COEF
Coefficients for log in the range of 1.0 < x < 1.0 + 2^-10.
|
private static double[][] |
LN_MANT
Extended precision logarithm table over the range 1 - 2 in increments of 2^-10.
|
private static double[][] |
LN_QUICK_COEF
Coefficients for log, when input 0.99 < x < 1.01.
|
private static double[][] |
LN_SPLIT_COEF
Coefficients for slowLog.
|
private static long |
MASK_30BITS
Mask used to clear low order 30 bits
|
static double |
PI
Archimede's constant PI, ratio of circle circumference to diameter.
|
private static long[] |
PI_O_4_BITS
Bits of pi/4, need for reducePayneHanek().
|
private static long[] |
RECIP_2PI
Bits of 1/(2*pi), need for reducePayneHanek().
|
private static double[] |
SINE_TABLE_A
Sine table (high bits).
|
private static double[] |
SINE_TABLE_B
Sine table (low bits).
|
private static double[] |
TANGENT_TABLE_A
Tangent table, used by atan() (high bits).
|
private static double[] |
TANGENT_TABLE_B
Tangent table, used by atan() (low bits).
|
private static double |
TWO_POWER_52
2^52 - double numbers this large must be integral (no fraction) or NaN or Infinite
|
| Modifier | Constructor and Description |
|---|---|
private |
FastMath()
Private Constructor
|
| Modifier and Type | Method and Description |
|---|---|
static double |
abs(double x)
Absolute value.
|
static float |
abs(float x)
Absolute value.
|
static int |
abs(int x)
Absolute value.
|
static long |
abs(long x)
Absolute value.
|
static double |
acos(double x)
Compute the arc cosine of a number.
|
static double |
acosh(double a)
Compute the inverse hyperbolic cosine of a number.
|
static double |
asin(double x)
Compute the arc sine of a number.
|
static double |
asinh(double a)
Compute the inverse hyperbolic sine of a number.
|
static double |
atan(double x)
Arctangent function
|
private static double |
atan(double xa,
double xb,
boolean leftPlane)
Internal helper function to compute arctangent.
|
static double |
atan2(double y,
double x)
Two arguments arctangent function
|
static double |
atanh(double a)
Compute the inverse hyperbolic tangent of a number.
|
private static void |
buildSinCosTables()
Build the sine and cosine tables.
|
static double |
cbrt(double x)
Compute the cubic root of a number.
|
static double |
ceil(double x)
Get the smallest whole number larger than x.
|
static double |
copySign(double magnitude,
double sign)
Returns the first argument with the sign of the second argument.
|
static float |
copySign(float magnitude,
float sign)
Returns the first argument with the sign of the second argument.
|
static double |
cos(double x)
Cosine function
|
static double |
cosh(double x)
Compute the hyperbolic cosine of a number.
|
private static double |
cosQ(double xa,
double xb)
Compute cosine in the first quadrant by subtracting input from PI/2 and
then calling sinQ.
|
private static double |
doubleHighPart(double d)
Get the high order bits from the mantissa.
|
static double |
exp(double x)
Exponential function.
|
private static double |
exp(double x,
double extra,
double[] hiPrec)
Internal helper method for exponential function.
|
private static double |
expint(int p,
double[] result)
Compute exp(p) for a integer p in extended precision.
|
static double |
expm1(double x)
Compute exp(x) - 1
|
private static double |
expm1(double x,
double[] hiPrecOut)
Internal helper method for expm1
|
static double |
floor(double x)
Get the largest whole number smaller than x.
|
static int |
getExponent(double d)
Return the exponent of a double number, removing the bias.
|
static int |
getExponent(float f)
Return the exponent of a float number, removing the bias.
|
static double |
hypot(double x,
double y)
Returns the hypotenuse of a triangle with sides
x and y
- sqrt(x2 +y2)avoiding intermediate overflow or underflow. |
static double |
IEEEremainder(double dividend,
double divisor)
Computes the remainder as prescribed by the IEEE 754 standard.
|
static double |
log(double x)
Natural logarithm.
|
private static double |
log(double x,
double[] hiPrec)
Internal helper method for natural logarithm function.
|
static double |
log10(double x)
Compute the base 10 logarithm.
|
static double |
log1p(double x)
Compute log(1 + x).
|
static double |
max(double a,
double b)
Compute the maximum of two values
|
static float |
max(float a,
float b)
Compute the maximum of two values
|
static int |
max(int a,
int b)
Compute the maximum of two values
|
static long |
max(long a,
long b)
Compute the maximum of two values
|
static double |
min(double a,
double b)
Compute the minimum of two values
|
static float |
min(float a,
float b)
Compute the minimum of two values
|
static int |
min(int a,
int b)
Compute the minimum of two values
|
static long |
min(long a,
long b)
Compute the minimum of two values
|
static double |
nextAfter(double d,
double direction)
Get the next machine representable number after a number, moving
in the direction of another number.
|
static float |
nextAfter(float f,
double direction)
Get the next machine representable number after a number, moving
in the direction of another number.
|
static double |
nextUp(double a)
Compute next number towards positive infinity.
|
static float |
nextUp(float a)
Compute next number towards positive infinity.
|
private static double |
polyCosine(double x)
Computes cos(x) - 1, where |x| < 1/16.
|
private static double |
polySine(double x)
Computes sin(x) - x, where |x| < 1/16.
|
static double |
pow(double x,
double y)
Power function.
|
private static void |
quadMult(double[] a,
double[] b,
double[] result)
Compute (a[0] + a[1]) * (b[0] + b[1]) in extended precision.
|
static double |
random()
Returns a pseudo-random number between 0.0 and 1.0.
|
private static void |
reducePayneHanek(double x,
double[] result)
Reduce the input argument using the Payne and Hanek method.
|
private static void |
resplit(double[] a)
Recompute a split.
|
static double |
rint(double x)
Get the whole number that is the nearest to x, or the even one if x is exactly half way between two integers.
|
static long |
round(double x)
Get the closest long to x.
|
static int |
round(float x)
Get the closest int to x.
|
static double |
scalb(double d,
int n)
Multiply a double number by a power of 2.
|
static float |
scalb(float f,
int n)
Multiply a float number by a power of 2.
|
static double |
signum(double a)
Compute the signum of a number.
|
static float |
signum(float a)
Compute the signum of a number.
|
static double |
sin(double x)
Sine function.
|
static double |
sinh(double x)
Compute the hyperbolic sine of a number.
|
private static double |
sinQ(double xa,
double xb)
Compute sine over the first quadrant (0 < x < pi/2).
|
private static double |
slowCos(double x,
double[] result)
For x between 0 and pi/4 compute cosine
|
private static double |
slowexp(double x,
double[] result)
For x between 0 and 1, returns exp(x), uses extended precision
|
private static double[] |
slowLog(double xi)
xi in the range of [1, 2].
|
private static double |
slowSin(double x,
double[] result)
For x between 0 and pi/4 compute sine.
|
private static void |
split(double d,
double[] split)
Compute split[0], split[1] such that their sum is equal to d,
and split[0] has its 30 least significant bits as zero.
|
private static void |
splitAdd(double[] a,
double[] b,
double[] ans)
Add two numbers in split form.
|
private static void |
splitMult(double[] a,
double[] b,
double[] ans)
Multiply two numbers in split form.
|
private static void |
splitReciprocal(double[] in,
double[] result)
Compute the reciprocal of in.
|
static double |
sqrt(double a)
Compute the square root of a number.
|
static double |
tan(double x)
Tangent function
|
static double |
tanh(double x)
Compute the hyperbolic tangent of a number.
|
private static double |
tanQ(double xa,
double xb,
boolean cotanFlag)
Compute tangent (or cotangent) over the first quadrant.
|
static double |
toDegrees(double x)
Convert radians to degrees, with error of less than 0.5 ULP
|
static double |
toRadians(double x)
Convert degrees to radians, with error of less than 0.5 ULP
|
static double |
ulp(double x)
Compute least significant bit (Unit in Last Position) for a number.
|
static float |
ulp(float x)
Compute least significant bit (Unit in Last Position) for a number.
|
public static final double PI
public static final double E
private static final double[] EXP_INT_TABLE_A
private static final double[] EXP_INT_TABLE_B
private static final double[] EXP_FRAC_TABLE_A
private static final double[] EXP_FRAC_TABLE_B
private static final double[] FACT
private static final double[][] LN_MANT
private static final double LN_2_A
private static final double LN_2_B
private static final double[][] LN_SPLIT_COEF
private static final double[][] LN_QUICK_COEF
private static final double[][] LN_HI_PREC_COEF
private static final double[] SINE_TABLE_A
private static final double[] SINE_TABLE_B
private static final double[] COSINE_TABLE_A
private static final double[] COSINE_TABLE_B
private static final double[] TANGENT_TABLE_A
private static final double[] TANGENT_TABLE_B
private static final long[] RECIP_2PI
private static final long[] PI_O_4_BITS
private static final double[] EIGHTHS
private static final double[] CBRTTWO
private static final long HEX_40000000
private static final long MASK_30BITS
private static final double TWO_POWER_52
private static double doubleHighPart(double d)
d - the value to splitpublic static double sqrt(double a)
Note: this implementation currently delegates to Math.sqrt(double)
a - number on which evaluation is donepublic static double cosh(double x)
x - number on which evaluation is donepublic static double sinh(double x)
x - number on which evaluation is donepublic static double tanh(double x)
x - number on which evaluation is donepublic static double acosh(double a)
a - number on which evaluation is donepublic static double asinh(double a)
a - number on which evaluation is donepublic static double atanh(double a)
a - number on which evaluation is donepublic static double signum(double a)
a - number on which evaluation is donepublic static float signum(float a)
a - number on which evaluation is donepublic static double nextUp(double a)
a - number to which neighbor should be computedpublic static float nextUp(float a)
a - number to which neighbor should be computedpublic static double random()
Note: this implementation currently delegates to Math.random()
public static double exp(double x)
x - a doubleprivate static double exp(double x,
double extra,
double[] hiPrec)
x - original argument of the exponential functionextra - extra bits of precision on input (To Be Confirmed)hiPrec - extra bits of precision on output (To Be Confirmed)public static double expm1(double x)
x - number to compute shifted exponentialprivate static double expm1(double x,
double[] hiPrecOut)
x - number to compute shifted exponentialhiPrecOut - receive high precision result for -1.0 < x < 1.0private static double slowexp(double x,
double[] result)
x - argument of exponentialresult - placeholder where to place exp(x) split in two terms
for extra precision (i.e. exp(x) = result[0] ?? result[1]private static void split(double d,
double[] split)
d - number to splitsplit - placeholder where to place the resultprivate static void resplit(double[] a)
a - input/out array containing the split, changed
on outputprivate static void splitMult(double[] a,
double[] b,
double[] ans)
a - first term of multiplicationb - second term of multiplicationans - placeholder where to put the resultprivate static void splitAdd(double[] a,
double[] b,
double[] ans)
a - first term of additionb - second term of additionans - placeholder where to put the resultprivate static void splitReciprocal(double[] in,
double[] result)
in - initial number, in split formresult - placeholder where to put the resultprivate static void quadMult(double[] a,
double[] b,
double[] result)
a - first term of the multiplicationb - second term of the multiplicationresult - placeholder where to put the resultprivate static double expint(int p,
double[] result)
p - integer whose exponential is requestedresult - placeholder where to put the result in extended precisionpublic static double log(double x)
x - a doubleprivate static double log(double x,
double[] hiPrec)
x - original argument of the natural logarithm functionhiPrec - extra bits of precision on output (To Be Confirmed)public static double log1p(double x)
x - a numberpublic static double log10(double x)
x - a numberpublic static double pow(double x,
double y)
x - a doubley - a doubleprivate static double[] slowLog(double xi)
xi - number from which log is requestedprivate static double slowSin(double x,
double[] result)
x - number from which sine is requestedresult - placeholder where to put the result in extended precisionprivate static double slowCos(double x,
double[] result)
x - number from which cosine is requestedresult - placeholder where to put the result in extended precisionprivate static void buildSinCosTables()
private static double polySine(double x)
x - a number smaller than 1/16private static double polyCosine(double x)
x - a number smaller than 1/16private static double sinQ(double xa,
double xb)
xa - number from which sine is requestedxb - extra bits for x (may be 0.0)private static double cosQ(double xa,
double xb)
xa - number from which cosine is requestedxb - extra bits for x (may be 0.0)private static double tanQ(double xa,
double xb,
boolean cotanFlag)
xa - number from which sine is requestedxb - extra bits for x (may be 0.0)cotanFlag - if true, compute the cotangent instead of the tangentprivate static void reducePayneHanek(double x,
double[] result)
x - number to reduceresult - placeholder where to put the resultpublic static double sin(double x)
x - a numberpublic static double cos(double x)
x - a numberpublic static double tan(double x)
x - a numberpublic static double atan(double x)
x - a numberprivate static double atan(double xa,
double xb,
boolean leftPlane)
xa - number from which arctangent is requestedxb - extra bits for x (may be 0.0)leftPlane - if true, result angle must be put in the left half planePI if leftPlane is true)public static double atan2(double y,
double x)
y - ordinatex - abscissa-PI and PIpublic static double asin(double x)
x - number on which evaluation is donepublic static double acos(double x)
x - number on which evaluation is donepublic static double cbrt(double x)
x - number on which evaluation is donepublic static double toRadians(double x)
x - angle in degreespublic static double toDegrees(double x)
x - angle in radianspublic static int abs(int x)
x - number from which absolute value is requestedpublic static long abs(long x)
x - number from which absolute value is requestedpublic static float abs(float x)
x - number from which absolute value is requestedpublic static double abs(double x)
x - number from which absolute value is requestedpublic static double ulp(double x)
x - number from which ulp is requestedpublic static float ulp(float x)
x - number from which ulp is requestedpublic static double scalb(double d,
int n)
d - number to multiplyn - power of 2public static float scalb(float f,
int n)
f - number to multiplyn - power of 2public static double nextAfter(double d,
double direction)
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 numberdirection - (the only important thing is whether
direction is greater or smaller than d)public static float nextAfter(float f,
double direction)
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 numberdirection - (the only important thing is whether
direction is greater or smaller than f)public static double floor(double x)
x - number from which floor is requestedpublic static double ceil(double x)
x - number from which ceil is requestedpublic static double rint(double x)
x - number from which nearest whole number is requestedpublic static long round(double x)
x - number from which closest long is requestedpublic static int round(float x)
x - number from which closest int is requestedpublic static int min(int a,
int b)
a - first valueb - second valuepublic static long min(long a,
long b)
a - first valueb - second valuepublic static float min(float a,
float b)
a - first valueb - second valuepublic static double min(double a,
double b)
a - first valueb - second valuepublic static int max(int a,
int b)
a - first valueb - second valuepublic static long max(long a,
long b)
a - first valueb - second valuepublic static float max(float a,
float b)
a - first valueb - second valuepublic static double max(double a,
double b)
a - first valueb - second valuepublic static double hypot(double x,
double y)
x and y
- sqrt(x2 +y2)x - a valuey - a valuepublic static double IEEEremainder(double dividend,
double divisor)
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(double, double)
dividend - the number to be divideddivisor - the number by which to dividepublic static double copySign(double magnitude,
double sign)
sign argument is treated as positive.magnitude - the value to returnsign - the sign for the returned valuesign argumentpublic static float copySign(float magnitude,
float sign)
sign argument is treated as positive.magnitude - the value to returnsign - the sign for the returned valuesign argumentpublic static int getExponent(double d)
For double numbers of the form 2x, the unbiased exponent is exactly x.
d - number from which exponent is requestedpublic static int getExponent(float f)
For float numbers of the form 2x, the unbiased exponent is exactly x.
f - number from which exponent is requestedCopyright (c) 2003-2013 Apache Software Foundation