public final class MathExtras
extends java.lang.Object
java.lang.Math
.
Modifier and Type | Method and Description |
---|---|
static boolean |
approxEquals(double value1,
double value2,
double tolerance)
Checks that two values are approximately equal (plus or minus a specified absolute tolerance).
|
static double |
barronSpline(double x,
double shape,
double turning)
A generalization on bias and gain functions that can represent both; this version is branch-less.
|
static float |
barronSpline(float x,
float shape,
float turning)
A generalization on bias and gain functions that can represent both; this version is branch-less.
|
static java.math.BigInteger |
bigFactorial(int n)
Calculates the factorial of n where n is a positive integer.
|
static float |
cbrt(float x)
An approximation of the cube-root function for float inputs and outputs.
|
static double |
clamp(double value,
double min,
double max)
If the specified value is not greater than or equal to the specified minimum and
less than or equal to the specified maximum, adjust it so that it is.
|
static float |
clamp(float value,
float min,
float max)
If the specified value is not greater than or equal to the specified minimum and
less than or equal to the specified maximum, adjust it so that it is.
|
static int |
clamp(int value,
int min,
int max)
If the specified value is not greater than or equal to the specified minimum and
less than or equal to the specified maximum, adjust it so that it is.
|
static long |
clamp(long value,
long min,
long max)
If the specified value is not greater than or equal to the specified minimum and
less than or equal to the specified maximum, adjust it so that it is.
|
static double |
factorial(double x)
A close approximation to the factorial function for real numbers, using an algorithm by T.
|
static long |
factorial(int n)
Calculates the factorial of n where n is a number in the
range 0 - 20.
|
static double |
gamma(double x)
A close approximation to the gamma function for positive doubles, using an algorithm by T.
|
static long |
greatestCommonDivisor(long a,
long b)
Determines the greatest common divisor of a pair of natural numbers
using the Euclidean algorithm.
|
static long |
isqrt(long n)
Integer square root (using floor), maintaining correct results even for very large
long values. |
static double |
log(double base,
double arg)
Calculate logarithms for arbitrary bases.
|
static int |
modularMultiplicativeInverse(int a)
Given any odd int
a , this finds another odd int b such that a * b == 1 . |
static long |
modularMultiplicativeInverse(long a)
Given any odd long
a , this finds another odd long b such that a * b == 1L . |
static double |
probit(double d)
A way of taking a double in the (0.0, 1.0) range and mapping it to a Gaussian or normal distribution, so high
inputs correspond to high outputs, and similarly for the low range.
|
static long |
raiseToPower(int value,
int power)
Calculate the first argument raised to the power of the second.
|
static double |
remainder(double op,
double d)
Like the modulo operator
% , but the result will always match the sign of d instead of op . |
public static long factorial(int n)
bigFactorial(int)
.n
- The factorial to calculate.bigFactorial(int)
public static java.math.BigInteger bigFactorial(int n)
factorial(int)
instead since it uses a faster
implementation.n
- The factorial to calculate.factorial(int)
public static long raiseToPower(int value, int power)
value
- The number to be raised.power
- The exponent (must be positive).value
raised to power
.public static double log(double base, double arg)
base
- The base for the logarithm.arg
- The value to calculate the logarithm for.arg
in the specified base
.public static boolean approxEquals(double value1, double value2, double tolerance)
value1
- The first value to compare.value2
- The second value to compare.tolerance
- How much the values are allowed to differ and still be considered equal, as an absolute amount.public static int clamp(int value, int min, int max)
value
- The value to check.min
- The minimum permitted value.max
- The maximum permitted value.value
if it is between the specified limits, min
if the value
is too low, or max
if the value is too high.public static long clamp(long value, long min, long max)
value
- The value to check.min
- The minimum permitted value.max
- The maximum permitted value.value
if it is between the specified limits, min
if the value
is too low, or max
if the value is too high.public static double clamp(double value, double min, double max)
value
- The value to check.min
- The minimum permitted value.max
- The maximum permitted value.value
if it is between the specified limits, min
if the value
is too low, or max
if the value is too high.public static float clamp(float value, float min, float max)
value
- The value to check.min
- The minimum permitted value.max
- The maximum permitted value.value
if it is between the specified limits, min
if the value
is too low, or max
if the value is too high.public static double remainder(double op, double d)
%
, but the result will always match the sign of d
instead of op
.op
- the dividend; negative values are permitted and wrap instead of producing negative resultsd
- the divisor; if this is negative then the result will be negative, otherwise it will be positivepublic static long greatestCommonDivisor(long a, long b)
a
- The first value.b
- The second value.public static int modularMultiplicativeInverse(int a)
a
, this finds another odd int b
such that a * b == 1
.
a
- any odd int; note that even numbers do not have inverses modulo 2 to the 32a
modulo 4294967296 (or, 2 to the 32)public static long modularMultiplicativeInverse(long a)
a
, this finds another odd long b
such that a * b == 1L
.a
- any odd long; note that even numbers do not have inverses modulo 2 to the 64a
modulo 18446744073709551616 (or, 2 to the 64)public static double probit(double d)
Random.nextGaussian()
). If this is given an input of 0.0
or less, it returns -38.5, which is slightly less than the result when given Double.MIN_VALUE
. If it is
given an input of 1.0 or more, it returns 38.5, which is significantly larger than the result when given the
largest double less than 1.0 (this value is further from 1.0 than Double.MIN_VALUE
is from 0.0). If
given Double.NaN
, it returns whatever Math.copySign(double, double)
returns for the arguments
38.5, Double.NaN
, which is implementation-dependent. It uses an algorithm by Peter John Acklam, as
implemented by Sherali Karimov.
Original source.
Information on the algorithm.
Wikipedia's page on the probit function may help, but
is more likely to just be confusing.
double
that's exclusive on 1.0, it won't produce a result higher than
8.209536145151493
, and will only produce results of at least -8.209536145151493
if 0.0 is
excluded from the inputs (if 0.0 is an input, the result is 38.5
). A chief advantage of using this with
a random number generator is that it only requires one random double to obtain one Gaussian value;
Random.nextGaussian()
generates at least two random doubles for each two Gaussian values, but may rarely
require much more random generation.
VanDerCorputQRNG.determine(int, int)
or the R2 sequence).d
- should be between 0 and 1, exclusive, but other values are toleratedpublic static long isqrt(long n)
long
values. This
version treats negative inputs as unsigned and returns positive square roots for them (these are usually large).
n
- a long
value that will be treated as if unsignedlong
if the result isn't already a long
public static float cbrt(float x)
Math.cbrt(double)
. It
correctly returns negative results when given negative inputs.
x
- any finite float to find the cube root ofpublic static float barronSpline(float x, float shape, float turning)
x
- progress through the spline, from 0 to 1, inclusiveshape
- must be greater than or equal to 0; values greater than 1 are "normal interpolations"turning
- a value between 0.0 and 1.0, inclusive, where the shape changespublic static double barronSpline(double x, double shape, double turning)
x
- progress through the spline, from 0 to 1, inclusiveshape
- must be greater than or equal to 0; values greater than 1 are "normal interpolations"turning
- a value between 0.0 and 1.0, inclusive, where the shape changespublic static double gamma(double x)
MathExtras.factorial(x - 1.0)
.x
- a real number; should usually be positivepublic static double factorial(double x)
x
- a real number; should not be both large and negativeCopyright © Eben Howard 2012–2022. All rights reserved.