public final class Arithmetic extends Object
| Modifier and Type | Method and Description |
|---|---|
static double |
binomial(double n,
long k)
Efficiently returns the binomial coefficient, often also referred to as
"n over k" or "n choose k".
|
static double |
binomial(long n,
long k)
Efficiently returns the binomial coefficient, often also referred to as
"n over k" or "n choose k".
|
static long |
ceil(double value)
Returns the smallest
long >= value. |
static double |
factoPow(int n)
Returns the value of n!
|
static double |
factorial(int k)
Instantly returns the factorial k!
|
static long |
floor(double value)
Returns the largest
long <= value. |
static boolean |
isBadNum(double value)
Checks whether the passed
value is one of Double.NaN,
Double.NEGATIVE_INFINITY or Double.POSITIVE_INFINITY. |
static boolean |
isProbability(double value)
Checks whether the passed
value qualifies as a probability, i.e.,
whether it fulfills 0.0 <= value <= 1.0. |
static double |
log(double base,
double x)
Returns logbase(x).
|
static double |
log2(double x)
Returns log2(x).
|
static double |
logFactorial(int k)
Returns log(k!)
|
static long |
longFactorial(int k)
Instantly returns the factorial k!
|
static double |
round(double x) |
static double |
round(double x,
int scale) |
static int |
roundToIntStochastically(double x) |
static double |
stirlingCorrection(int k)
Returns the Stirling correction.
|
public static double binomial(double n,
long k)
The binomial coefficient is defined as (n * n-1 * ... * n-k+1 ) / ( 1 * 2 * ... * k ).
: 0
: 1
: n
(n * n-1 * ... * n-k+1 ) / ( 1 * 2 * ... * k )
n - the n in "n choose k"k - the k in "n choose k"public static double binomial(long n,
long k)
The binomial coefficient is defined as
: 0
: 1
: n
(n * n-1 * ... * n-k+1 ) / ( 1 * 2 * ... * k )
n - the n in "n choose k"k - the k in "n choose k"public static long ceil(double value)
long >= value.
Examples: 1.0 -> 1, 1.2 -> 2, 1.9 -> 2
This method is safer than using (long) Math.ceil(value), because of possible rounding error.
value - the valuepublic static double factorial(int k)
k >= 171, Double.POSITIVE_INFINITY is returned.k - an integer >= 0public static long floor(double value)
long <= value.
Examples:
This method is safer than using (long) Math.floor(value), because of possible rounding error.1.0 -> 1, 1.2 -> 1, 1.9 -> 12.0 -> 2, 2.2 -> 2, 2.9 -> 2
value - the valuepublic static double log(double base,
double x)
base - the basex - the valuepublic static double log2(double x)
x - the valuepublic static double logFactorial(int k)
Tries to avoid overflows. For k < 30 simply looks up a table in O(1). For k >= 30 uses Stirlings approximation.
k - must hold k >= 0public static double factoPow(int n)
n - an integer >= 0n!/n^npublic static long longFactorial(int k)
k - must hold k >= 0 && k < 21public static double stirlingCorrection(int k)
Correction term of the Stirling approximation for log(k!) (series in 1/k, or table values for small k) with int parameter k.
log k! = (k + 1/2)log(k + 1) - (k + 1) + (1/2)log(2Pi) +
stirlingCorrection(k + 1)
log k! = (k + 1/2)log(k) - k + (1/2)log(2Pi) +
stirlingCorrection(k)
k - the valuepublic static boolean isBadNum(double value)
value is one of Double.NaN,
Double.NEGATIVE_INFINITY or Double.POSITIVE_INFINITY.
Returns true if this is the case, otherwise returns
false.value - the double to checktrue when value is finite and not
Double.NaN, false in all other casespublic static boolean isProbability(double value)
value qualifies as a probability, i.e.,
whether it fulfills 0.0 <= value <= 1.0.value - the double to checktrue when value lies within [0, 1],
otherwise falsepublic static int roundToIntStochastically(double x)
public static double round(double x,
int scale)
public static double round(double x)
Copyright © 2022. All rights reserved.