Math.
This class contains only static methods.-
Method Summary
Modifier and TypeMethodDescriptionstatic doubleasDouble(double value) Convert a double to a double.static doubleasDouble(int value) Convert an int to a double.static doubleasDouble(long value) Convert a long to a double.static intasInt(double value) Turn a double into an int.static longasLong(double value) Turn a double into a long.static floatfceil(double value) Round a double-precision floating point number to the nearest single-precision floating point number larger than or equal to its value.static floatffloor(double value) Round a double-precision floating point number to the nearest single-precision floating point number less than or equal to its value.static floatfroundTowardInf(double value) Round a double-precision floating point number to the nearest single-precision floating point number whose absolute value is greater than or equal to the double-precision number's value.static floatfroundTowardZero(double value) Round a double-precision floating point number to the nearest single-precision floating point number whose absolute value is less than or equal to the double-precision number's value.static intgcd(int a, int b) Compute the greatest common divisor of two integersstatic longgcd(long a, long b) Compute the greatest common divisor of two non-negative long integersstatic intintRound(double value) Round a double-precision number to produce an integer.static doublelog2(int n) Compute the base-2 logarithm of an integer n.static doublelog2(int n, double accuracy) Compute the base-2 logarithm of an integer n, specifying its accuracy.static doublelog2(long n) Compute the base-2 logarithm of a long integer n.static doublelog2(long n, double accuracy) Compute the base-2 logarithm of a long integer n, specifying its accuracy.static longlPow(int x, int n) Raise an integer to to a non-negative integer power.static doublepow(double x, int n) Raise a number to an integer power.static doublepow(double z, int xn, int xd) Raise a number x to a rational power.static doublepow(double x, long n) Raise a number to a long integer power.static doubleroot(int n, double x) Compute the nth root of a real number.
-
Method Details
-
asInt
Turn a double into an int. This method is provided for convenience when scripting languages such as the ones supported byExpressionParserare used:ExpressionParserhas no explicit iteration constructs and instead relies on Java streams. In this case one may have to force an expression to provide a specific numberical type.- Parameters:
value- the double-precision value- Returns:
- the corresponding integer
- Throws:
IllegalArgumentException- if the double value is not the value one would obtain by converting the closest integer into a double-precision value or if the integer value is too large or too small to be prepresented as an int
-
asLong
Turn a double into a long. This method is provided for convenience when scripting languages such as the ones supported byExpressionParserare used:ExpressionParserhas no explicit iteration constructs and instead relies on Java streams. In this case one may have to force an expression to provide a specific numberical type.- Parameters:
value- the double-precision value- Returns:
- the corresponding integer
- Throws:
IllegalArgumentException- if the double value is not the value one would obtain by converting the closest integer into a double-precision value
-
asDouble
public static double asDouble(int value) Convert an int to a double. This method is provided for convenience when scripting languages such as the ones supported byExpressionParserare used:ExpressionParserhas no explicit iteration constructs and instead relies on Java streams. In this case one may have to force an expression to provide a specific numberical type.- Parameters:
value- the value to convert- Returns:
- the corresponding double-precision value
-
asDouble
public static double asDouble(long value) Convert a long to a double. This method is provided for convenience when scripting languages such as the ones supported byExpressionParserare used:ExpressionParserhas no explicit iteration constructs and instead relies on Java streams. In this case one may have to force an expression to provide a specific numberical type.- Parameters:
value- the value to convert- Returns:
- the corresponding double-precision value
-
asDouble
public static double asDouble(double value) Convert a double to a double. This method simply returns its value, and is provided for convenience when scripting languages such as the ones supported byExpressionParserare used:ExpressionParserhas no explicit iteration constructs and instead relies on Java streams. In this case one may have to force an expression to provide a specific numberical type.- Parameters:
value- the value to convert- Returns:
- the corresponding double-precision value
-
fceil
public static float fceil(double value) Round a double-precision floating point number to the nearest single-precision floating point number larger than or equal to its value.- Parameters:
value- the value to round- Returns:
- the closest single-precision floating point number greater than or equal to the argument
-
ffloor
public static float ffloor(double value) Round a double-precision floating point number to the nearest single-precision floating point number less than or equal to its value.- Parameters:
value- the value to round- Returns:
- the closest single-precision floating point number less than or equal to the argument
-
froundTowardZero
public static float froundTowardZero(double value) Round a double-precision floating point number to the nearest single-precision floating point number whose absolute value is less than or equal to the double-precision number's value.- Parameters:
value- the value to round- Returns:
- the closest single-precision floating point number less than or equal to the argument
-
froundTowardInf
public static float froundTowardInf(double value) Round a double-precision floating point number to the nearest single-precision floating point number whose absolute value is greater than or equal to the double-precision number's value.- Parameters:
value- the value to round- Returns:
- the closest single-precision floating point number greater than or equal to the argument
-
gcd
Compute the greatest common divisor of two integers- Parameters:
a- the first integerb- the second integer- Returns:
- the greatest common divisor of a and b
- Throws:
IllegalArgumentException- both arguments were zero
-
gcd
Compute the greatest common divisor of two non-negative long integers- Parameters:
a- the first integerb- the second integer- Returns:
- the greatest common divisor of a and b
- Throws:
IllegalArgumentException- both arguments were zero
-
intRound
Round a double-precision number to produce an integer.This method was added specifically to support the class
ExpressionParser: Java does not let one cast a double-precision method to an integer andMath.round(double)returns a long integer.- Parameters:
value- the value to round- Returns:
- the value rounded to the nearest integer
- Throws:
IllegalArgumentException- the value is out of range and cannot be converted to an integer
-
pow
public static double pow(double x, int n) Raise a number to an integer power. This method is provided to supplement the methodMath.pow(double, double)by providing a method whose second argument is an integer. This is useful in some infinite series expansions of special functions. For example, for small z, the spherical Bessel function jn(z) has a first term proportional to zn in its Taylor series expansion (for larger values of |z|, the implementation inFunctionsdoes not use this Taylor series).The time complexity is O(log2 n). Timing measurements indicate that it is faster than
java.lang.Math.pow(which handles more cases).- Parameters:
x- a real numbern- the power- Returns:
- the value of xn
-
pow
public static double pow(double x, long n) Raise a number to a long integer power. This method is provided to supplement the methodMath.pow(double, double)by providing a method whose second argument is a long integer. This is useful in some infinite series expansions of special functions. For example, for small z, the spherical Bessel function jn(z) has a first term proportional to zn in its Taylor series expansion (for larger values of |z|, the implementation inFunctionsdoes not use this Taylor series).The time complexity is O(log2 n). Timing measurements indicate that it is faster than
java.lang.Math.pow(which handles more cases).- Parameters:
x- a real numbern- the power- Returns:
- the value of xn
-
root
public static double root(int n, double x) Compute the nth root of a real number. The second argument must be non-negative when the first argument is an even integer.- Parameters:
n- the root index (2 for the square root, 3 for the cube root, etc.)x- the number whose root is to be computed.- Returns:
- the nth root
- Throws:
IllegalArgumentException- an argument was out of range
-
pow
public static double pow(double z, int xn, int xd) Raise a number x to a rational power. This method is provided to supplement the methodMath.pow(double, double)by providing a method whose second argument is the numerator of the power and whose third argument is the denominator.- Parameters:
z- a real numberxn- the numerator of fraction representing a rational number for the powerxd- the denominator of a fraction representing a rational number for the power- Returns:
- the value of xxn/xd
-
lPow
Raise an integer to to a non-negative integer power. This method is provided to supplement the methodMath.pow(double, double)by providing a method whose second argument is an integer. This is useful in some infinite series expansions of special functions. For example, for small z, the spherical Bessel function jn(z) has a first term proportional to zn in its Taylor series expansion.The time complexity is O(log2 n). Timing measurements indicate that it is faster than
java.lang.Math.pow(which handles more cases).- Parameters:
x- an integern- the power- Returns:
- the value of xn
- Throws:
IllegalArgumentException- an argument was out of boundsArithmeticException- the combination of arguments would overflow the integer representation used
-
log2
Compute the base-2 logarithm of an integer n.- Parameters:
n- an integer that is larger than 0- Returns:
- the base-2 logarithm of n
- Throws:
IllegalArgumentException- an argument was out of range
-
log2
Compute the base-2 logarithm of an integer n, specifying its accuracy. One use of this method is in estimating the time or space needed by an algorithm, where precise values are not needed.Note: when the accuracy is set to 1.0, the value returned is floor(log2(n)). The accuracy is an upper bound on the difference between the returned value and the actual value.
- Parameters:
n- an integer that is larger than 0accuracy- the accuracy of the fractional part of the returned value (values should be in the range (0.0, 1.0])- Returns:
- the base-2 logarithm of n
- Throws:
IllegalArgumentException- an argument was out of range
-
log2
Compute the base-2 logarithm of a long integer n.- Parameters:
n- an integer that is larger than 0- Returns:
- the base-2 logarithm of n
- Throws:
IllegalArgumentException- an argument was out of range
-
log2
Compute the base-2 logarithm of a long integer n, specifying its accuracy. One use of this method is in estimating the time or space needed by an algorithm, where precise values are not needed.- Parameters:
n- a long integer that is larger than 0accuracy- the accuracy of the fractional part of the returned value (values should be in the range (0.0, 1.0])- Returns:
- the base-2 logarithm of n
- Throws:
IllegalArgumentException- an argument was out of range
-