com.google.common.math
@Beta public final class DoubleMath extends java.lang.Object
Math.| Constructor and Description |
|---|
DoubleMath() |
| Modifier and Type | Method and Description |
|---|---|
static double |
factorial(int n)
Returns
n!, that is, the product of the first n positive
integers, 1 if n == 0, or e n!}, or
Double.POSITIVE_INFINITY if n! > Double.MAX_VALUE. |
static boolean |
isMathematicalInteger(double x)
Returns
true if x represents a mathematical integer. |
static boolean |
isPowerOfTwo(double x)
Returns
true if x is exactly equal to 2^k for some finite integer
k. |
static double |
log2(double x)
Returns the base 2 logarithm of a double value.
|
static int |
log2(double x,
java.math.RoundingMode mode)
Returns the base 2 logarithm of a double value, rounded with the specified rounding mode to an
int. |
static java.math.BigInteger |
roundToBigInteger(double x,
java.math.RoundingMode mode)
Returns the
BigInteger value that is equal to x rounded with the specified
rounding mode, if possible. |
static int |
roundToInt(double x,
java.math.RoundingMode mode)
Returns the
int value that is equal to x rounded with the specified rounding
mode, if possible. |
static long |
roundToLong(double x,
java.math.RoundingMode mode)
Returns the
long value that is equal to x rounded with the specified rounding
mode, if possible. |
public static int roundToInt(double x,
java.math.RoundingMode mode)
int value that is equal to x rounded with the specified rounding
mode, if possible.java.lang.ArithmeticException - if
x is infinite or NaN
x, after being rounded to a mathematical integer using the specified
rounding mode, is either less than Integer.MIN_VALUE or greater than Integer.MAX_VALUE
x is not a mathematical integer and mode is
RoundingMode.UNNECESSARY
public static long roundToLong(double x,
java.math.RoundingMode mode)
long value that is equal to x rounded with the specified rounding
mode, if possible.java.lang.ArithmeticException - if
x is infinite or NaN
x, after being rounded to a mathematical integer using the specified
rounding mode, is either less than Long.MIN_VALUE or greater than Long.MAX_VALUE
x is not a mathematical integer and mode is
RoundingMode.UNNECESSARY
public static java.math.BigInteger roundToBigInteger(double x,
java.math.RoundingMode mode)
BigInteger value that is equal to x rounded with the specified
rounding mode, if possible.java.lang.ArithmeticException - if
x is infinite or NaN
x is not a mathematical integer and mode is
RoundingMode.UNNECESSARY
public static boolean isPowerOfTwo(double x)
true if x is exactly equal to 2^k for some finite integer
k.public static double log2(double x)
Special cases:
x is NaN or less than zero, the result is NaN.
x is positive infinity, the result is positive infinity.
x is positive or negative zero, the result is negative infinity.
The computed result must be within 1 ulp of the exact result.
If the result of this method will be immediately rounded to an int,
log2(double, RoundingMode) is faster.
public static int log2(double x,
java.math.RoundingMode mode)
int.
Regardless of the rounding mode, this is faster than (int) log2(x).
java.lang.IllegalArgumentException - if x <= 0.0, x is NaN, or x is
infinitepublic static boolean isMathematicalInteger(double x)
true if x represents a mathematical integer.
This is equivalent to, but not necessarily implemented as, the expression !Double.isNaN(x) && !Double.isInfinite(x) && x == Math.rint(x).
public static double factorial(int n)
n!, that is, the product of the first n positive
integers, 1 if n == 0, or e n!}, or
Double.POSITIVE_INFINITY if n! > Double.MAX_VALUE.
The result is within 1 ulp of the true value.
java.lang.IllegalArgumentException - if n < 0