Class FastMath
- java.lang.Object
-
- org.apache.commons.math.util.FastMath
-
public class FastMath extends java.lang.ObjectFaster, more accurate, portable alternative toStrictMath.Additionally implements the following methods not found in StrictMath:
The following methods are found in StrictMath since 1.6 only- Since:
- 2.2
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static 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 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 functionstatic 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 functionstatic doublecosh(double x)Compute the hyperbolic cosine of a number.static doubleexp(double x)Exponential function.static doubleexpm1(double x)Compute exp(x) - 1static doublefloor(double x)Get the largest whole number smaller than x.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 doublelog(double x)Natural logarithm.static doublelog10(double x)Compute the base 10 logarithm.static doublelog1p(double x)Compute log(1 + x).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 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 doublenextUp(double a)Compute next number towards positive infinity.static floatnextUp(float a)Compute next number towards positive infinity.static doublepow(double x, double y)Power function.static doublerandom()Returns a pseudo-random number between 0.0 and 1.0.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.static doublesqrt(double a)Compute the square root of a number.static doubletan(double x)Tangent functionstatic doubletanh(double x)Compute the hyperbolic tangent of a number.static doubletoDegrees(double x)Convert radians to degrees, with error of less than 0.5 ULPstatic 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 Detail
-
PI
public static final double PI
Archimede's constant PI, ratio of circle circumference to diameter.- See Also:
- Constant Field Values
-
E
public static final double E
Napier's constant e, base of the natural logarithm.- See Also:
- Constant Field Values
-
-
Method Detail
-
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
-
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 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:
x- a double- Returns:
- double ex
-
expm1
public static double expm1(double x)
Compute exp(x) - 1- Parameters:
x- number to compute shifted exponential- Returns:
- exp(x) - 1
-
log
public static double log(double x)
Natural logarithm.- Parameters:
x- a double- Returns:
- log(x)
-
log1p
public static double log1p(double x)
Compute log(1 + x).- Parameters:
x- a number- Returns:
- log(1 + x)
-
log10
public static double log10(double x)
Compute the base 10 logarithm.- Parameters:
x- a number- Returns:
- log10(x)
-
pow
public static double pow(double x, double y)Power function. Compute x^y.- Parameters:
x- a doubley- a double- Returns:
- double
-
sin
public static double sin(double x)
Sine function.- Parameters:
x- a number- Returns:
- sin(x)
-
cos
public static double cos(double x)
Cosine function- Parameters:
x- a number- Returns:
- cos(x)
-
tan
public static double tan(double x)
Tangent function- Parameters:
x- a number- Returns:
- tan(x)
-
atan
public static double atan(double x)
Arctangent function- Parameters:
x- a number- Returns:
- atan(x)
-
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 <= x < 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 < x <= 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 <= x <= 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
-
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
-
-