Class FastMath
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 machines- Since:
- 2.2
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classEnclose the Cody/Waite reduction (used in "sin", "cos" and "tan").private static classEnclose large data table in nested static class so it's only loaded on first access.private static classEnclose large data table in nested static class so it's only loaded on first access.private static classEnclose large data table in nested static class so it's only loaded on first access.private static classClass operator on double numbers split into one 26 bits number and one 27 bits number. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final double[]Table of 2^((n+2)/3)private static final double[]Cosine table (high bits).private static final double[]Cosine table (low bits).static final doubleNapier's constant e, base of the natural logarithm.private static final double[]Eighths.(package private) static final intExponential fractions table length.(package private) static final intLength of the array of integer exponentials.(package private) static final intIndex of exp(0) in the array of integer exponentials.private static final doubleConstant: 0.09090909090909091.private static final doubleConstant: 0.07692307692307693.private static final doubleConstant: 0.06666666666666667.private static final doubleConstant: 0.058823529411764705.private static final doubleConstant: 0.5.private static final doubleConstant: 0.3333333333333333.private static final doubleConstant: 0.25.private static final doubleConstant: 0.2.private static final doubleConstant: 0.14285714285714285.private static final doubleConstant: 0.1111111111111111.private static final doubleConstant: 0.9166666666666666.private static final doubleConstant: 0.9285714285714286.private static final doubleConstant: 0.9375.private static final doubleConstant: 0.75.private static final doubleConstant: 0.8333333333333334.private static final doubleConstant: 0.875.private static final doubleConstant: 0.9.private static final long0x40000000 - used to split a double into two parts, both with the low order bits cleared.private static final longMask used to add implicit high order bit for normalized double.private static final doublelog(2) (high bits).private static final doublelog(2) (low bits).private static final double[][]Coefficients for log in the range of 1.0 invalid input: '<' x invalid input: '<' 1.0 + 2^-10.(package private) static final intLogarithm table length.private static final double[][]Coefficients for log, when input 0.99 invalid input: '<' x invalid input: '<' 1.01.private static final doubleStrictMath.log(Double.MAX_VALUE):private static final longMask used to clear low order 30 bitsprivate static final longMask used to extract exponent from double bits.private static final longMask used to extract mantissa from double bits.private static final intMask used to clear the non-sign part of an int.private static final longMask used to clear the non-sign part of a long.static final doubleArchimede's constant PI, ratio of circle circumference to diameter.private static final long[]Bits of pi/4, need for reducePayneHanek().private static final long[]Bits of 1/(2*pi), need for reducePayneHanek().private static final booleanIndicator for tables initialization.private static final double[]Sine table (high bits).private static final double[]Sine table (low bits).private static final intSine, Cosine, Tangent tables are for 0, 1/8, 2/8, ...private static final double[]Tangent table, used by atan() (high bits).private static final double[]Tangent table, used by atan() (low bits).private static final double2^52 - double numbers this large must be integral (no fraction) or NaN or Infinite -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic doubleabs(double x) Absolute value.static floatabs(float x) Absolute value.static intabs(int x) Absolute value.static longabs(long x) Absolute value.static doubleacos(double x) Compute the arc cosine of a number.static doubleacosh(double a) Compute the inverse hyperbolic cosine of a number.static intaddExact(int a, int b) Add two numbers, detecting overflows.static longaddExact(long a, long b) Add two numbers, detecting overflows.static doubleasin(double x) Compute the arc sine of a number.static doubleasinh(double a) Compute the inverse hyperbolic sine of a number.static doubleatan(double x) Arctangent functionprivate static doubleatan(double xa, double xb, boolean leftPlane) Internal helper function to compute arctangent.static doubleatan2(double y, double x) Two arguments arctangent functionstatic doubleatanh(double a) Compute the inverse hyperbolic tangent of a number.static doublecbrt(double x) Compute the cubic root of a number.static doubleceil(double x) Get the smallest whole number larger than x.static doublecopySign(double magnitude, double sign) Returns the first argument with the sign of the second argument.static floatcopySign(float magnitude, float sign) Returns the first argument with the sign of the second argument.static doublecos(double x) Cosine function.static doublecosh(double x) Compute the hyperbolic cosine of a number.private static doublecosQ(double xa, double xb) Compute cosine in the first quadrant by subtracting input from PI/2 and then calling sinQ.static intdecrementExact(int n) Decrement a number, detecting overflows.static longdecrementExact(long n) Decrement a number, detecting overflows.private static doubledoubleHighPart(double d) Get the high order bits from the mantissa.static doubleexp(double x) Exponential function.private static doubleexp(double x, double extra, double[] hiPrec) Internal helper method for exponential function.static doubleexpm1(double x) Compute exp(x) - 1private static doubleexpm1(double x, double[] hiPrecOut) Internal helper method for expm1static doublefloor(double x) Get the largest whole number smaller than x.static intfloorDiv(int a, int b) Finds q such that a = q b + r with 0 invalid input: '<'= r invalid input: '<' b if b > 0 and b invalid input: '<' r invalid input: '<'= 0 if b invalid input: '<' 0.static longfloorDiv(long a, long b) Finds q such that a = q b + r with 0 invalid input: '<'= r invalid input: '<' b if b > 0 and b invalid input: '<' r invalid input: '<'= 0 if b invalid input: '<' 0.static intfloorMod(int a, int b) Finds r such that a = q b + r with 0 invalid input: '<'= r invalid input: '<' b if b > 0 and b invalid input: '<' r invalid input: '<'= 0 if b invalid input: '<' 0.static longfloorMod(long a, long b) Finds r such that a = q b + r with 0 invalid input: '<'= r invalid input: '<' b if b > 0 and b invalid input: '<' r invalid input: '<'= 0 if b invalid input: '<' 0.static intgetExponent(double d) Return the exponent of a double number, removing the bias.static intgetExponent(float f) Return the exponent of a float number, removing the bias.static doublehypot(double x, double y) Returns the hypotenuse of a triangle with sidesxandy- sqrt(x2 +y2)
avoiding intermediate overflow or underflow.static doubleIEEEremainder(double dividend, double divisor) Computes the remainder as prescribed by the IEEE 754 standard.static intincrementExact(int n) Increment a number, detecting overflows.static longincrementExact(long n) Increment a number, detecting overflows.static doublelog(double x) Natural logarithm.static doublelog(double base, double x) Computes the logarithm in a given base.private static doublelog(double x, double[] hiPrec) Internal helper method for natural logarithm function.static doublelog10(double x) Compute the base 10 logarithm.static doublelog1p(double x) Computes log(1 + x).static voidPrint out contents of arrays, and check the length.static doublemax(double a, double b) Compute the maximum of two valuesstatic floatmax(float a, float b) Compute the maximum of two valuesstatic intmax(int a, int b) Compute the maximum of two valuesstatic longmax(long a, long b) Compute the maximum of two valuesstatic doublemin(double a, double b) Compute the minimum of two valuesstatic floatmin(float a, float b) Compute the minimum of two valuesstatic intmin(int a, int b) Compute the minimum of two valuesstatic longmin(long a, long b) Compute the minimum of two valuesstatic intmultiplyExact(int a, int b) Multiply two numbers, detecting overflows.static longmultiplyExact(long a, long b) Multiply two numbers, detecting overflows.static doublenextAfter(double d, double direction) Get the next machine representable number after a number, moving in the direction of another number.static floatnextAfter(float f, double direction) Get the next machine representable number after a number, moving in the direction of another number.static doublenextDown(double a) Compute next number towards negative infinity.static floatnextDown(float a) Compute next number towards negative infinity.static doublenextUp(double a) Compute next number towards positive infinity.static floatnextUp(float a) Compute next number towards positive infinity.private static doublepolyCosine(double x) Computes cos(x) - 1, where |x| invalid input: '<' 1/16.private static doublepolySine(double x) Computes sin(x) - x, where |x| invalid input: '<' 1/16.static doublepow(double x, double y) Power function.static doublepow(double d, int e) Raise a double to an int power.static doublepow(double d, long e) Raise a double to a long power.static doublerandom()Returns a pseudo-random number between 0.0 and 1.0.private static voidreducePayneHanek(double x, double[] result) Reduce the input argument using the Payne and Hanek method.static doublerint(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 longround(double x) Get the closest long to x.static intround(float x) Get the closest int to x.static doublescalb(double d, int n) Multiply a double number by a power of 2.static floatscalb(float f, int n) Multiply a float number by a power of 2.static doublesignum(double a) Compute the signum of a number.static floatsignum(float a) Compute the signum of a number.static doublesin(double x) Sine function.static doublesinh(double x) Compute the hyperbolic sine of a number.private static doublesinQ(double xa, double xb) Compute sine over the first quadrant (0 invalid input: '<' x invalid input: '<' pi/2).static doublesqrt(double a) Compute the square root of a number.static intsubtractExact(int a, int b) Subtract two numbers, detecting overflows.static longsubtractExact(long a, long b) Subtract two numbers, detecting overflows.static doubletan(double x) Tangent function.static doubletanh(double x) Compute the hyperbolic tangent of a number.private static doubletanQ(double xa, double xb, boolean cotanFlag) Compute tangent (or cotangent) over the first quadrant.static doubletoDegrees(double x) Convert radians to degrees, with error of less than 0.5 ULPstatic inttoIntExact(long n) Convert a long to interger, detecting overflowsstatic doubletoRadians(double x) Convert degrees to radians, with error of less than 0.5 ULPstatic doubleulp(double x) Compute least significant bit (Unit in Last Position) for a number.static floatulp(float x) Compute least significant bit (Unit in Last Position) for a number.
-
Field Details
-
PI
public static final double PIArchimede's constant PI, ratio of circle circumference to diameter.- See Also:
-
E
public static final double ENapier's constant e, base of the natural logarithm.- See Also:
-
EXP_INT_TABLE_MAX_INDEX
static final int EXP_INT_TABLE_MAX_INDEXIndex of exp(0) in the array of integer exponentials.- See Also:
-
EXP_INT_TABLE_LEN
static final int EXP_INT_TABLE_LENLength of the array of integer exponentials.- See Also:
-
LN_MANT_LEN
static final int LN_MANT_LENLogarithm table length.- See Also:
-
EXP_FRAC_TABLE_LEN
static final int EXP_FRAC_TABLE_LENExponential fractions table length.- See Also:
-
LOG_MAX_VALUE
private static final double LOG_MAX_VALUEStrictMath.log(Double.MAX_VALUE): -
RECOMPUTE_TABLES_AT_RUNTIME
private static final boolean RECOMPUTE_TABLES_AT_RUNTIMEIndicator for tables initialization.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.
- See Also:
-
LN_2_A
private static final double LN_2_Alog(2) (high bits).- See Also:
-
LN_2_B
private static final double LN_2_Blog(2) (low bits).- See Also:
-
LN_QUICK_COEF
private static final double[][] LN_QUICK_COEFCoefficients for log, when input 0.99 invalid input: '<' x invalid input: '<' 1.01. -
LN_HI_PREC_COEF
private static final double[][] LN_HI_PREC_COEFCoefficients for log in the range of 1.0 invalid input: '<' x invalid input: '<' 1.0 + 2^-10. -
SINE_TABLE_LEN
private static final int SINE_TABLE_LENSine, Cosine, Tangent tables are for 0, 1/8, 2/8, ... 13/8 = PI/2 approx.- See Also:
-
SINE_TABLE_A
private static final double[] SINE_TABLE_ASine table (high bits). -
SINE_TABLE_B
private static final double[] SINE_TABLE_BSine table (low bits). -
COSINE_TABLE_A
private static final double[] COSINE_TABLE_ACosine table (high bits). -
COSINE_TABLE_B
private static final double[] COSINE_TABLE_BCosine table (low bits). -
TANGENT_TABLE_A
private static final double[] TANGENT_TABLE_ATangent table, used by atan() (high bits). -
TANGENT_TABLE_B
private static final double[] TANGENT_TABLE_BTangent table, used by atan() (low bits). -
RECIP_2PI
private static final long[] RECIP_2PIBits of 1/(2*pi), need for reducePayneHanek(). -
PI_O_4_BITS
private static final long[] PI_O_4_BITSBits of pi/4, need for reducePayneHanek(). -
EIGHTHS
private static final double[] EIGHTHSEighths. This is used by sinQ, because its faster to do a table lookup than a multiply in this time-critical routine -
CBRTTWO
private static final double[] CBRTTWOTable of 2^((n+2)/3) -
HEX_40000000
private static final long HEX_400000000x40000000 - used to split a double into two parts, both with the low order bits cleared. Equivalent to 2^30.- See Also:
-
MASK_30BITS
private static final long MASK_30BITSMask used to clear low order 30 bits- See Also:
-
MASK_NON_SIGN_INT
private static final int MASK_NON_SIGN_INTMask used to clear the non-sign part of an int.- See Also:
-
MASK_NON_SIGN_LONG
private static final long MASK_NON_SIGN_LONGMask used to clear the non-sign part of a long.- See Also:
-
MASK_DOUBLE_EXPONENT
private static final long MASK_DOUBLE_EXPONENTMask used to extract exponent from double bits.- See Also:
-
MASK_DOUBLE_MANTISSA
private static final long MASK_DOUBLE_MANTISSAMask used to extract mantissa from double bits.- See Also:
-
IMPLICIT_HIGH_BIT
private static final long IMPLICIT_HIGH_BITMask used to add implicit high order bit for normalized double.- See Also:
-
TWO_POWER_52
private static final double TWO_POWER_522^52 - double numbers this large must be integral (no fraction) or NaN or Infinite- See Also:
-
F_1_3
private static final double F_1_3Constant: 0.3333333333333333.- See Also:
-
F_1_5
private static final double F_1_5Constant: 0.2.- See Also:
-
F_1_7
private static final double F_1_7Constant: 0.14285714285714285.- See Also:
-
F_1_9
private static final double F_1_9Constant: 0.1111111111111111.- See Also:
-
F_1_11
private static final double F_1_11Constant: 0.09090909090909091.- See Also:
-
F_1_13
private static final double F_1_13Constant: 0.07692307692307693.- See Also:
-
F_1_15
private static final double F_1_15Constant: 0.06666666666666667.- See Also:
-
F_1_17
private static final double F_1_17Constant: 0.058823529411764705.- See Also:
-
F_3_4
private static final double F_3_4Constant: 0.75.- See Also:
-
F_15_16
private static final double F_15_16Constant: 0.9375.- See Also:
-
F_13_14
private static final double F_13_14Constant: 0.9285714285714286.- See Also:
-
F_11_12
private static final double F_11_12Constant: 0.9166666666666666.- See Also:
-
F_9_10
private static final double F_9_10Constant: 0.9.- See Also:
-
F_7_8
private static final double F_7_8Constant: 0.875.- See Also:
-
F_5_6
private static final double F_5_6Constant: 0.8333333333333334.- See Also:
-
F_1_2
private static final double F_1_2Constant: 0.5.- See Also:
-
F_1_4
private static final double F_1_4Constant: 0.25.- See Also:
-
-
Constructor Details
-
FastMath
private FastMath()Private Constructor
-
-
Method Details
-
doubleHighPart
private static double doubleHighPart(double d) Get the high order bits from the mantissa. Equivalent to adding and subtracting HEX_40000 but also works for very large numbers- Parameters:
d- the value to split- Returns:
- the high order part of the mantissa
-
sqrt
public static double sqrt(double a) Compute the square root of a number.Note: this implementation currently delegates to
Math.sqrt(double)- Parameters:
a- number on which evaluation is done- Returns:
- square root of a
-
cosh
public static double cosh(double x) Compute the hyperbolic cosine of a number.- Parameters:
x- number on which evaluation is done- Returns:
- hyperbolic cosine of x
-
sinh
public static double sinh(double x) Compute the hyperbolic sine of a number.- Parameters:
x- number on which evaluation is done- Returns:
- hyperbolic sine of x
-
tanh
public static double tanh(double x) Compute the hyperbolic tangent of a number.- Parameters:
x- number on which evaluation is done- Returns:
- hyperbolic tangent of x
-
acosh
public static double acosh(double a) Compute the inverse hyperbolic cosine of a number.- Parameters:
a- number on which evaluation is done- Returns:
- inverse hyperbolic cosine of a
-
asinh
public static double asinh(double a) Compute the inverse hyperbolic sine of a number.- Parameters:
a- number on which evaluation is done- Returns:
- inverse hyperbolic sine of a
-
atanh
public static double atanh(double a) Compute the inverse hyperbolic tangent of a number.- Parameters:
a- number on which evaluation is done- Returns:
- inverse hyperbolic tangent of a
-
signum
public static double signum(double a) Compute the signum of a number. The signum is -1 for negative numbers, +1 for positive numbers and 0 otherwise- Parameters:
a- number on which evaluation is done- Returns:
- -1.0, -0.0, +0.0, +1.0 or NaN depending on sign of a
-
signum
public static float signum(float a) Compute the signum of a number. The signum is -1 for negative numbers, +1 for positive numbers and 0 otherwise- Parameters:
a- number on which evaluation is done- Returns:
- -1.0, -0.0, +0.0, +1.0 or NaN depending on sign of a
-
nextUp
public static double nextUp(double a) Compute next number towards positive infinity.- Parameters:
a- number to which neighbor should be computed- Returns:
- neighbor of a towards positive infinity
-
nextUp
public static float nextUp(float a) Compute next number towards positive infinity.- Parameters:
a- number to which neighbor should be computed- Returns:
- neighbor of a towards positive infinity
-
nextDown
public static double nextDown(double a) Compute next number towards negative infinity.- Parameters:
a- number to which neighbor should be computed- Returns:
- neighbor of a towards negative infinity
- Since:
- 3.4
-
nextDown
public static float nextDown(float a) Compute next number towards negative infinity.- Parameters:
a- number to which neighbor should be computed- Returns:
- neighbor of a towards negative infinity
- Since:
- 3.4
-
random
public static double random()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
-
exp
public static double exp(double x) 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 ULP 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:
x- a double- Returns:
- double ex
-
exp
private static double exp(double x, double extra, double[] hiPrec) Internal helper method for exponential function.- Parameters:
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)- Returns:
- exp(x)
-
expm1
public static double expm1(double x) Compute exp(x) - 1- Parameters:
x- number to compute shifted exponential- Returns:
- exp(x) - 1
-
expm1
private static double expm1(double x, double[] hiPrecOut) Internal helper method for expm1- Parameters:
x- number to compute shifted exponentialhiPrecOut- receive high precision result for -1.0 invalid input: '<' x invalid input: '<' 1.0- Returns:
- exp(x) - 1
-
log
public static double log(double x) Natural logarithm.- Parameters:
x- a double- Returns:
- log(x)
-
log
private static double log(double x, double[] hiPrec) Internal helper method for natural logarithm function.- Parameters:
x- original argument of the natural logarithm functionhiPrec- extra bits of precision on output (To Be Confirmed)- Returns:
- log(x)
-
log1p
public static double log1p(double x) Computes log(1 + x).- Parameters:
x- Number.- Returns:
log(1 + x).
-
log10
public static double log10(double x) Compute the base 10 logarithm.- Parameters:
x- a number- Returns:
- log10(x)
-
log
public static double log(double base, double x) Computes the logarithm in a given base. ReturnsNaNif either argument is negative. Ifbaseis 0 andxis positive, 0 is returned. Ifbaseis positive andxis 0,Double.NEGATIVE_INFINITYis returned. If both arguments are 0, the result isNaN.- Parameters:
base- Base of the logarithm, must be greater than 0.x- Argument, must be greater than 0.- Returns:
- the value of the logarithm, i.e. the number
ysuch thatbasey = x. - Since:
- 1.2 (previously in
MathUtils, moved as of version 3.0)
-
pow
public static double pow(double x, double y) Power function. Compute x^y.- Parameters:
x- a doubley- a double- Returns:
- double
-
pow
public static double pow(double d, int e) Raise a double to an int power.- Parameters:
d- Number to raise.e- Exponent.- Returns:
- de
- Since:
- 3.1
-
pow
public static double pow(double d, long e) Raise a double to a long power.- Parameters:
d- Number to raise.e- Exponent.- Returns:
- de
- Since:
- 3.6
-
polySine
private static double polySine(double x) Computes sin(x) - x, where |x| invalid input: '<' 1/16. Use a Remez polynomial approximation.- Parameters:
x- a number smaller than 1/16- Returns:
- sin(x) - x
-
polyCosine
private static double polyCosine(double x) Computes cos(x) - 1, where |x| invalid input: '<' 1/16. Use a Remez polynomial approximation.- Parameters:
x- a number smaller than 1/16- Returns:
- cos(x) - 1
-
sinQ
private static double sinQ(double xa, double xb) Compute sine over the first quadrant (0 invalid input: '<' x invalid input: '<' pi/2). Use combination of table lookup and rational polynomial expansion.- Parameters:
xa- number from which sine is requestedxb- extra bits for x (may be 0.0)- Returns:
- sin(xa + xb)
-
cosQ
private static double cosQ(double xa, double xb) 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:
xa- number from which cosine is requestedxb- extra bits for x (may be 0.0)- Returns:
- cos(xa + xb)
-
tanQ
private static double tanQ(double xa, double xb, boolean cotanFlag) Compute tangent (or cotangent) over the first quadrant. 0 invalid input: '<' x invalid input: '<' pi/2 Use combination of table lookup and rational polynomial expansion.- Parameters:
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- Returns:
- tan(xa+xb) (or cotangent, depending on cotanFlag)
-
reducePayneHanek
private static void reducePayneHanek(double x, double[] result) Reduce the input argument using the Payne and Hanek method. This is good for all inputs 0.0 invalid input: '<' x invalid input: '<' 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:
x- number to reduceresult- placeholder where to put the result
-
sin
public static double sin(double x) Sine function.- Parameters:
x- Argument.- Returns:
- sin(x)
-
cos
public static double cos(double x) Cosine function.- Parameters:
x- Argument.- Returns:
- cos(x)
-
tan
public static double tan(double x) Tangent function.- Parameters:
x- Argument.- Returns:
- tan(x)
-
atan
public static double atan(double x) Arctangent function- Parameters:
x- a number- Returns:
- atan(x)
-
atan
private static double atan(double xa, double xb, boolean leftPlane) Internal helper function to compute arctangent.- Parameters:
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- Returns:
- atan(xa + xb) (or angle shifted by
PIif leftPlane is true)
-
atan2
public static double atan2(double y, double x) Two arguments arctangent function- Parameters:
y- ordinatex- abscissa- Returns:
- phase angle of point (x,y) between
-PIandPI
-
asin
public static double asin(double x) Compute the arc sine of a number.- Parameters:
x- number on which evaluation is done- Returns:
- arc sine of x
-
acos
public static double acos(double x) Compute the arc cosine of a number.- Parameters:
x- number on which evaluation is done- Returns:
- arc cosine of x
-
cbrt
public static double cbrt(double x) Compute the cubic root of a number.- Parameters:
x- number on which evaluation is done- Returns:
- cubic root of x
-
toRadians
public static double toRadians(double x) Convert degrees to radians, with error of less than 0.5 ULP- Parameters:
x- angle in degrees- Returns:
- x converted into radians
-
toDegrees
public static double toDegrees(double x) Convert radians to degrees, with error of less than 0.5 ULP- Parameters:
x- angle in radians- Returns:
- x converted into degrees
-
abs
public static int abs(int x) Absolute value.- Parameters:
x- number from which absolute value is requested- Returns:
- abs(x)
-
abs
public static long abs(long x) Absolute value.- Parameters:
x- number from which absolute value is requested- Returns:
- abs(x)
-
abs
public static float abs(float x) Absolute value.- Parameters:
x- number from which absolute value is requested- Returns:
- abs(x)
-
abs
public static double abs(double x) Absolute value.- Parameters:
x- number from which absolute value is requested- Returns:
- abs(x)
-
ulp
public static double ulp(double x) Compute least significant bit (Unit in Last Position) for a number.- Parameters:
x- number from which ulp is requested- Returns:
- ulp(x)
-
ulp
public static float ulp(float x) Compute least significant bit (Unit in Last Position) for a number.- Parameters:
x- number from which ulp is requested- Returns:
- ulp(x)
-
scalb
public static double scalb(double d, int n) Multiply a double number by a power of 2.- Parameters:
d- number to multiplyn- power of 2- Returns:
- d × 2n
-
scalb
public static float scalb(float f, int n) Multiply a float number by a power of 2.- Parameters:
f- number to multiplyn- power of 2- Returns:
- f × 2n
-
nextAfter
public static double nextAfter(double d, double direction) 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
directionis greater thand, the smallest machine representable number strictly greater thandis returned; if less, then the largest representable number strictly less thandis returned.If
dis infinite and direction does not bring it back to finite numbers, it is returned unchanged.- Parameters:
d- base numberdirection- (the only important thing is whetherdirectionis greater or smaller thand)- Returns:
- the next machine representable number in the specified direction
-
nextAfter
public static float nextAfter(float f, double direction) 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
directionis greater thanf, the smallest machine representable number strictly greater thanfis returned; if less, then the largest representable number strictly less thanfis returned.If
fis infinite and direction does not bring it back to finite numbers, it is returned unchanged.- Parameters:
f- base numberdirection- (the only important thing is whetherdirectionis greater or smaller thanf)- Returns:
- the next machine representable number in the specified direction
-
floor
public static double floor(double x) Get the largest whole number smaller than x.- Parameters:
x- number from which floor is requested- Returns:
- a double number f such that f is an integer f invalid input: '<'= x invalid input: '<' f + 1.0
-
ceil
public static double ceil(double x) Get the smallest whole number larger than x.- Parameters:
x- number from which ceil is requested- Returns:
- a double number c such that c is an integer c - 1.0 invalid input: '<' x invalid input: '<'= c
-
rint
public 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.- Parameters:
x- number from which nearest whole number is requested- Returns:
- a double number r such that r is an integer r - 0.5 invalid input: '<'= x invalid input: '<'= r + 0.5
-
round
public static long round(double x) Get the closest long to x.- Parameters:
x- number from which closest long is requested- Returns:
- closest long to x
-
round
public static int round(float x) Get the closest int to x.- Parameters:
x- number from which closest int is requested- Returns:
- closest int to x
-
min
public static int min(int a, int b) Compute the minimum of two values- Parameters:
a- first valueb- second value- Returns:
- a if a is lesser or equal to b, b otherwise
-
min
public static long min(long a, long b) Compute the minimum of two values- Parameters:
a- first valueb- second value- Returns:
- a if a is lesser or equal to b, b otherwise
-
min
public static float min(float a, float b) Compute the minimum of two values- Parameters:
a- first valueb- second value- Returns:
- a if a is lesser or equal to b, b otherwise
-
min
public static double min(double a, double b) Compute the minimum of two values- Parameters:
a- first valueb- second value- Returns:
- a if a is lesser or equal to b, b otherwise
-
max
public static int max(int a, int b) Compute the maximum of two values- Parameters:
a- first valueb- second value- Returns:
- b if a is lesser or equal to b, a otherwise
-
max
public static long max(long a, long b) Compute the maximum of two values- Parameters:
a- first valueb- second value- Returns:
- b if a is lesser or equal to b, a otherwise
-
max
public static float max(float a, float b) Compute the maximum of two values- Parameters:
a- first valueb- second value- Returns:
- b if a is lesser or equal to b, a otherwise
-
max
public static double max(double a, double b) Compute the maximum of two values- Parameters:
a- first valueb- second value- Returns:
- b if a is lesser or equal to b, a otherwise
-
hypot
public static double hypot(double x, double y) Returns the hypotenuse of a triangle with sidesxandy- 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:
x- a valuey- a value- Returns:
- sqrt(x2 +y2)
-
IEEEremainder
public static double IEEEremainder(double dividend, double divisor) Computes the remainder as prescribed by the IEEE 754 standard. The remainder value is mathematically equal tox - y*nwherenis the mathematical integer closest to the exact mathematical value of the quotientx/y. If two mathematical integers are equally close tox/ythennis 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(double, double)- Parameters:
dividend- the number to be divideddivisor- the number by which to divide- Returns:
- the remainder, rounded
-
toIntExact
Convert a long to interger, detecting overflows- Parameters:
n- number to convert to int- Returns:
- integer with same valie as n if no overflows occur
- Throws:
MathArithmeticException- if n cannot fit into an int- Since:
- 3.4
-
incrementExact
Increment a number, detecting overflows.- Parameters:
n- number to increment- Returns:
- n+1 if no overflows occur
- Throws:
MathArithmeticException- if an overflow occurs- Since:
- 3.4
-
incrementExact
Increment a number, detecting overflows.- Parameters:
n- number to increment- Returns:
- n+1 if no overflows occur
- Throws:
MathArithmeticException- if an overflow occurs- Since:
- 3.4
-
decrementExact
Decrement a number, detecting overflows.- Parameters:
n- number to decrement- Returns:
- n-1 if no overflows occur
- Throws:
MathArithmeticException- if an overflow occurs- Since:
- 3.4
-
decrementExact
Decrement a number, detecting overflows.- Parameters:
n- number to decrement- Returns:
- n-1 if no overflows occur
- Throws:
MathArithmeticException- if an overflow occurs- Since:
- 3.4
-
addExact
Add two numbers, detecting overflows.- Parameters:
a- first number to addb- second number to add- Returns:
- a+b if no overflows occur
- Throws:
MathArithmeticException- if an overflow occurs- Since:
- 3.4
-
addExact
Add two numbers, detecting overflows.- Parameters:
a- first number to addb- second number to add- Returns:
- a+b if no overflows occur
- Throws:
MathArithmeticException- if an overflow occurs- Since:
- 3.4
-
subtractExact
public static int subtractExact(int a, int b) Subtract two numbers, detecting overflows.- Parameters:
a- first numberb- second number to subtract from a- Returns:
- a-b if no overflows occur
- Throws:
MathArithmeticException- if an overflow occurs- Since:
- 3.4
-
subtractExact
public static long subtractExact(long a, long b) Subtract two numbers, detecting overflows.- Parameters:
a- first numberb- second number to subtract from a- Returns:
- a-b if no overflows occur
- Throws:
MathArithmeticException- if an overflow occurs- Since:
- 3.4
-
multiplyExact
public static int multiplyExact(int a, int b) Multiply two numbers, detecting overflows.- Parameters:
a- first number to multiplyb- second number to multiply- Returns:
- a*b if no overflows occur
- Throws:
MathArithmeticException- if an overflow occurs- Since:
- 3.4
-
multiplyExact
public static long multiplyExact(long a, long b) Multiply two numbers, detecting overflows.- Parameters:
a- first number to multiplyb- second number to multiply- Returns:
- a*b if no overflows occur
- Throws:
MathArithmeticException- if an overflow occurs- Since:
- 3.4
-
floorDiv
Finds q such that a = q b + r with 0 invalid input: '<'= r invalid input: '<' b if b > 0 and b invalid input: '<' r invalid input: '<'= 0 if b invalid input: '<' 0.This methods returns the same value as integer division when a and b are same signs, but returns a different value when they are opposite (i.e. q is negative).
- Parameters:
a- dividendb- divisor- Returns:
- q such that a = q b + r with 0 invalid input: '<'= r invalid input: '<' b if b > 0 and b invalid input: '<' r invalid input: '<'= 0 if b invalid input: '<' 0
- Throws:
MathArithmeticException- if b == 0- Since:
- 3.4
- See Also:
-
floorDiv
Finds q such that a = q b + r with 0 invalid input: '<'= r invalid input: '<' b if b > 0 and b invalid input: '<' r invalid input: '<'= 0 if b invalid input: '<' 0.This methods returns the same value as integer division when a and b are same signs, but returns a different value when they are opposite (i.e. q is negative).
- Parameters:
a- dividendb- divisor- Returns:
- q such that a = q b + r with 0 invalid input: '<'= r invalid input: '<' b if b > 0 and b invalid input: '<' r invalid input: '<'= 0 if b invalid input: '<' 0
- Throws:
MathArithmeticException- if b == 0- Since:
- 3.4
- See Also:
-
floorMod
Finds r such that a = q b + r with 0 invalid input: '<'= r invalid input: '<' b if b > 0 and b invalid input: '<' r invalid input: '<'= 0 if b invalid input: '<' 0.This methods returns the same value as integer modulo when a and b are same signs, but returns a different value when they are opposite (i.e. q is negative).
- Parameters:
a- dividendb- divisor- Returns:
- r such that a = q b + r with 0 invalid input: '<'= r invalid input: '<' b if b > 0 and b invalid input: '<' r invalid input: '<'= 0 if b invalid input: '<' 0
- Throws:
MathArithmeticException- if b == 0- Since:
- 3.4
- See Also:
-
floorMod
public static long floorMod(long a, long b) Finds r such that a = q b + r with 0 invalid input: '<'= r invalid input: '<' b if b > 0 and b invalid input: '<' r invalid input: '<'= 0 if b invalid input: '<' 0.This methods returns the same value as integer modulo when a and b are same signs, but returns a different value when they are opposite (i.e. q is negative).
- Parameters:
a- dividendb- divisor- Returns:
- r such that a = q b + r with 0 invalid input: '<'= r invalid input: '<' b if b > 0 and b invalid input: '<' r invalid input: '<'= 0 if b invalid input: '<' 0
- Throws:
MathArithmeticException- if b == 0- Since:
- 3.4
- See Also:
-
copySign
public static double copySign(double magnitude, double sign) Returns the first argument with the sign of the second argument. A NaNsignargument is treated as positive.- Parameters:
magnitude- the value to returnsign- the sign for the returned value- Returns:
- the magnitude with the same sign as the
signargument
-
copySign
public static float copySign(float magnitude, float sign) Returns the first argument with the sign of the second argument. A NaNsignargument is treated as positive.- Parameters:
magnitude- the value to returnsign- the sign for the returned value- Returns:
- the magnitude with the same sign as the
signargument
-
getExponent
public static int getExponent(double d) Return the exponent of a double number, removing the bias.For double numbers of the form 2x, the unbiased exponent is exactly x.
- Parameters:
d- number from which exponent is requested- Returns:
- exponent for d in IEEE754 representation, without bias
-
getExponent
public static int getExponent(float f) Return the exponent of a float number, removing the bias.For float numbers of the form 2x, the unbiased exponent is exactly x.
- Parameters:
f- number from which exponent is requested- Returns:
- exponent for d in IEEE754 representation, without bias
-
main
Print out contents of arrays, and check the length.used to generate the preset arrays originally.
- Parameters:
a- unused
-