|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.commons.math3.util.FastMath
public class FastMath
Faster, more accurate, portable alternative to Math
and
StrictMath
for large scale computation.
FastMath is a drop-in replacement for both Math and StrictMath. This
means that for any method in Math (say Math.sin(x)
or
Math.cbrt(y)
), user can directly change the class and use the
methods as is (using FastMath.sin(x)
or FastMath.cbrt(y)
in the previous example).
FastMath speed is achieved by relying heavily on optimizing compilers to native code present in many JVMs today and use of large tables. The larger tables are lazily initialised on first use, so that the setup time does not penalise methods that don't need them.
Note that FastMath is extensively used inside Apache Commons Math, so by calling some algorithms, the overhead when the the tables need to be intialised will occur regardless of the end-user calling FastMath methods directly or not. Performance figures for a specific JVM and hardware can be evaluated by running the FastMathTestPerformance tests in the test directory of the source distribution.
FastMath accuracy should be mostly independent of the JVM as it relies only on IEEE-754 basic operations and on embedded tables. Almost all operations are accurate to about 0.5 ulp throughout the domain range. This statement, of course is only a rough global observed behavior, it is not a guarantee for every double numbers input (see William Kahan's Table Maker's Dilemma).
FastMath additionally implements the following methods not found in Math/StrictMath:
The following methods are found in Math/StrictMath since 1.6 only, they are provided by FastMath even in 1.5 Java virtual machinescopySign(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)
Nested Class Summary | |
---|---|
private static class |
FastMath.ExpFracTable
Enclose large data table in nested static class so it's only loaded on first access. |
private static class |
FastMath.ExpIntTable
Enclose large data table in nested static class so it's only loaded on first access. |
private static class |
FastMath.lnMant
Enclose large data table in nested static class so it's only loaded on first access. |
Field Summary | |
---|---|
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. |
(package private) static int |
EXP_FRAC_TABLE_LEN
Exponential fractions table length. |
(package private) static int |
EXP_INT_TABLE_LEN
Length of the array of integer exponentials. |
(package private) static int |
EXP_INT_TABLE_MAX_INDEX
Index of exp(0) in the array of integer exponentials. |
private static double |
F_1_11
Constant: 0.09090909090909091. |
private static double |
F_1_13
Constant: 0.07692307692307693. |
private static double |
F_1_15
Constant: 0.06666666666666667. |
private static double |
F_1_17
Constant: 0.058823529411764705. |
private static double |
F_1_2
Constant: 0.5. |
private static double |
F_1_3
Constant: 0.3333333333333333. |
private static double |
F_1_4
Constant: 0.25. |
private static double |
F_1_5
Constant: 0.2. |
private static double |
F_1_7
Constant: 0.14285714285714285. |
private static double |
F_1_9
Constant: 0.1111111111111111. |
private static double |
F_11_12
Constant: 0.9166666666666666. |
private static double |
F_13_14
Constant: 0.9285714285714286. |
private static double |
F_15_16
Constant: 0.9375. |
private static double |
F_3_4
Constant: 0.75. |
private static double |
F_5_6
Constant: 0.8333333333333334. |
private static double |
F_7_8
Constant: 0.875. |
private static double |
F_9_10
Constant: 0.9. |
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. |
(package private) static int |
LN_MANT_LEN
Logarithm table length. |
private static double[][] |
LN_QUICK_COEF
Coefficients for log, when input 0.99 < x < 1.01. |
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 boolean |
RECOMPUTE_TABLES_AT_RUNTIME
Indicator for tables initialization. |
private static double[] |
SINE_TABLE_A
Sine table (high bits). |
private static double[] |
SINE_TABLE_B
Sine table (low bits). |
private static int |
SINE_TABLE_LEN
Sine, Cosine, Tangent tables are for 0, 1/8, 2/8, ... |
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 |
Constructor Summary | |
---|---|
private |
FastMath()
Private Constructor |
Method Summary | |
---|---|
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. |
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. |
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. |
static double |
log(double base,
double x)
Computes the logarithm in a given base. |
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 void |
main(String[] a)
Print out contents of arrays, and check the length. |
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. |
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. |
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). |
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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final double PI
public static final double E
static final int EXP_INT_TABLE_MAX_INDEX
static final int EXP_INT_TABLE_LEN
static final int LN_MANT_LEN
static final int EXP_FRAC_TABLE_LEN
private static final boolean RECOMPUTE_TABLES_AT_RUNTIME
This compile-time constant should be set to true only if one explicitly wants to compute the tables at class loading time instead of using the already computed ones provided as literal arrays below.
private static final double LN_2_A
private static final double LN_2_B
private static final double[][] LN_QUICK_COEF
private static final double[][] LN_HI_PREC_COEF
private static final int SINE_TABLE_LEN
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 final double F_1_3
private static final double F_1_5
private static final double F_1_7
private static final double F_1_9
private static final double F_1_11
private static final double F_1_13
private static final double F_1_15
private static final double F_1_17
private static final double F_3_4
private static final double F_15_16
private static final double F_13_14
private static final double F_11_12
private static final double F_9_10
private static final double F_7_8
private static final double F_5_6
private static final double F_1_2
private static final double F_1_4
Constructor Detail |
---|
private FastMath()
Method Detail |
---|
private static double doubleHighPart(double d)
d
- the value to split
public static double sqrt(double a)
Note: this implementation currently delegates to Math.sqrt(double)
a
- number on which evaluation is done
public static double cosh(double x)
x
- number on which evaluation is done
public static double sinh(double x)
x
- number on which evaluation is done
public static double tanh(double x)
x
- number on which evaluation is done
public static double acosh(double a)
a
- number on which evaluation is done
public static double asinh(double a)
a
- number on which evaluation is done
public static double atanh(double a)
a
- number on which evaluation is done
public static double signum(double a)
a
- number on which evaluation is done
public static float signum(float a)
a
- number on which evaluation is done
public static double nextUp(double a)
a
- number to which neighbor should be computed
public static float nextUp(float a)
a
- number to which neighbor should be computed
public static double random()
Note: this implementation currently delegates to Math.random()
public static double exp(double x)
x
- a double
private 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 exponential
private static double expm1(double x, double[] hiPrecOut)
x
- number to compute shifted exponentialhiPrecOut
- receive high precision result for -1.0 < x < 1.0
public static double log(double x)
x
- a double
private 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 number
public static double log10(double x)
x
- a number
public static double log(double base, double x)
NaN
if either argument is negative.
If base
is 0 and x
is positive, 0 is returned.
If base
is positive and x
is 0,
Double.NEGATIVE_INFINITY
is returned.
If both arguments are 0, the result is NaN
.
base
- Base of the logarithm, must be greater than 0.x
- Argument, must be greater than 0.
y
such that
basey = x
.MathUtils
, moved as of version 3.0)public static double pow(double x, double y)
x
- a doubley
- a double
private static double polySine(double x)
x
- a number smaller than 1/16
private static double polyCosine(double x)
x
- a number smaller than 1/16
private 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 tangent
private static void reducePayneHanek(double x, double[] result)
x
- number to reduceresult
- placeholder where to put the resultpublic static double sin(double x)
x
- a number
public static double cos(double x)
x
- a number
public static double tan(double x)
x
- a number
public static double atan(double x)
x
- a number
private 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 plane
PI
if leftPlane is true)public static double atan2(double y, double x)
y
- ordinatex
- abscissa
-PI
and PI
public static double asin(double x)
x
- number on which evaluation is done
public static double acos(double x)
x
- number on which evaluation is done
public static double cbrt(double x)
x
- number on which evaluation is done
public static double toRadians(double x)
x
- angle in degrees
public static double toDegrees(double x)
x
- angle in radians
public static int abs(int x)
x
- number from which absolute value is requested
public static long abs(long x)
x
- number from which absolute value is requested
public static float abs(float x)
x
- number from which absolute value is requested
public static double abs(double x)
x
- number from which absolute value is requested
public static double ulp(double x)
x
- number from which ulp is requested
public static float ulp(float x)
x
- number from which ulp is requested
public static double scalb(double d, int n)
d
- number to multiplyn
- power of 2
public static float scalb(float f, int n)
f
- number to multiplyn
- power of 2
public 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 requested
public static double ceil(double x)
x
- number from which ceil is requested
public static double rint(double x)
x
- number from which nearest whole number is requested
public static long round(double x)
x
- number from which closest long is requested
public static int round(float x)
x
- number from which closest int is requested
public static int min(int a, int b)
a
- first valueb
- second value
public static long min(long a, long b)
a
- first valueb
- second value
public static float min(float a, float b)
a
- first valueb
- second value
public static double min(double a, double b)
a
- first valueb
- second value
public static int max(int a, int b)
a
- first valueb
- second value
public static long max(long a, long b)
a
- first valueb
- second value
public static float max(float a, float b)
a
- first valueb
- second value
public static double max(double a, double b)
a
- first valueb
- second value
public static double hypot(double x, double y)
x
and y
- sqrt(x2 +y2)
x
- a valuey
- a value
public 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 divide
public static double copySign(double magnitude, double sign)
sign
argument is treated as positive.
magnitude
- the value to returnsign
- the sign for the returned value
sign
argumentpublic static float copySign(float magnitude, float sign)
sign
argument is treated as positive.
magnitude
- the value to returnsign
- the sign for the returned value
sign
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 requested
public static int getExponent(float f)
For float numbers of the form 2x, the unbiased exponent is exactly x.
f
- number from which exponent is requested
public static void main(String[] a)
used to generate the preset arrays originally.
a
- unused
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |