Package net.jafama
Class FastMath
- java.lang.Object
-
- net.jafama.FastMath
-
public final class FastMath extends Object
Faster (hopefully) versions of java.lang.Math methods, plus additional ones. Cf. README.txt for more info.
-
-
Field Summary
Fields Modifier and Type Field Description static double
E
Closest double approximation of e.static double
PI
Closest double approximation of pi, which is inferior to mathematical pi: pi ~= 3.14159265358979323846...static double
PI_SUP
High double approximation of pi, which is further from pi than the low approximation PI: pi ~= 3.14159265358979323846...
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static double
abs(double a)
static float
abs(float a)
static int
abs(int value)
static long
abs(long value)
static double
acos(double value)
static double
acosh(double value)
Some properties of acosh(x) = log(x + sqrt(x^2 - 1)): 1) defined on [1,+Infinity[ 2) result in ]0,+Infinity[ (by convention, since cosh(x) = cosh(-x)) 3) acosh(1) = 0 4) acosh(1+epsilon) ~= log(1 + sqrt(2*epsilon)) ~= sqrt(2*epsilon) 5) lim(acosh(x),x->+Infinity) = +Infinity (y increasing logarithmically slower than x)static double
acosh1p(double value)
Much more accurate than acosh(1+value), for arguments (and results) close to zero.static double
acosInRange(double value)
If value is not NaN and is outside [-1,1] range, closest value in this range is used.static int
addBounded(int a, int b)
static long
addBounded(long a, long b)
static int
addExact(int a, int b)
static long
addExact(long a, long b)
static double
asin(double value)
static double
asinh(double value)
Some properties of asinh(x) = log(x + sqrt(x^2 + 1)) 1) defined on ]-Infinity,+Infinity[ 2) result in ]-Infinity,+Infinity[ 3) asinh(x) = -asinh(-x) (implies asinh(0) = 0) 4) asinh(epsilon) ~= epsilon 5) lim(asinh(x),x->+Infinity) = +Infinity (y increasing logarithmically slower than x)static double
asinInRange(double value)
If value is not NaN and is outside [-1,1] range, closest value in this range is used.static double
atan(double value)
static double
atan2(double y, double x)
For special values for which multiple conventions could be adopted, behaves like Math.atan2(double,double).static double
atanh(double value)
Some properties of atanh(x) = log((1+x)/(1-x))/2: 1) defined on ]-1,1[ 2) result in ]-Infinity,+Infinity[ 3) atanh(-1) = -Infinity (by continuity) 4) atanh(1) = +Infinity (by continuity) 5) atanh(epsilon) ~= epsilon 6) lim(atanh(x),x->1) = +Infinitystatic double
cbrt(double value)
static double
ceil(double value)
static float
ceil(float value)
static int
ceilToInt(double value)
static double
copySign(double magnitude, double sign)
A sign of NaN can be interpreted as positive or negative.static float
copySign(float magnitude, float sign)
A sign of NaN can be interpreted as positive or negative.static double
cos(double angle)
static double
cosh(double value)
Some properties of cosh(x) = (exp(x)+exp(-x))/2: 1) defined on ]-Infinity,+Infinity[ 2) result in [1,+Infinity[ 3) cosh(0) = 1 4) cosh(x) = cosh(-x) 5) lim(cosh(x),x->+Infinity) = +Infinity (y increasing exponentially faster than x) 6) reaches +Infinity (double overflow) for x >= 710.475860073944, i.e.static double
coshm1(double value)
Much more accurate than cosh(value)-1, for arguments (and results) close to zero.static double
cosQuick(double angle)
Quick cos, with accuracy of about 1.6e-3 (PI/) for |angle| < 6588397.0 (Integer.MAX_VALUE * (2*PI/ )), and no accuracy at all for larger values. static int
decrementBounded(int value)
static long
decrementBounded(long value)
static int
decrementExact(int value)
static long
decrementExact(long value)
static double
exp(double value)
static double
expm1(double value)
Much more accurate than exp(value)-1, for arguments (and results) close to zero.static double
expQuick(double value)
Quick exp, with a max relative error of about 2.94e-2 for |value| < 700.0 or so, and no accuracy at all outside this range.static double
floor(double value)
static float
floor(float value)
static int
floorDiv(int x, int y)
Returns the largest int <= dividend/divisor.static long
floorDiv(long x, int y)
Returns the largest long <= dividend/divisor.static long
floorDiv(long x, long y)
Returns the largest long <= dividend/divisor.static int
floorMod(int x, int y)
Returns the floor modulus, which is "x - floorDiv(x,y) * y", has the same sign as y, and is in ]-abs(y),abs(y)[.static int
floorMod(long x, int y)
Returns the floor modulus, which is "x - floorDiv(x,y) * y", has the same sign as y, and is in ]-abs(y),abs(y)[.static long
floorMod(long x, long y)
Returns the floor modulus, which is "x - floorDiv(x,y) * y", has the same sign as y, and is in ]-abs(y),abs(y)[.static int
floorToInt(double value)
static int
getExponent(double value)
static int
getExponent(float value)
static double
hypot(double x, double y)
static double
hypot(double x, double y, double z)
static double
IEEEremainder(double f1, double f2)
static int
incrementBounded(int value)
static long
incrementBounded(long value)
static int
incrementExact(int value)
static long
incrementExact(long value)
static void
initTables()
Ensures that all look-up tables are initialized - otherwise they are initialized lazily.static double
invSqrtQuick(double value)
Quick inverse of square root, with a max relative error of about 3.44e-2 for values in [Double.MIN_NORMAL,Double.MAX_VALUE], and worse accuracy outside this range.static boolean
isInClockwiseDomain(double startAngRad, double angSpanRad, double angRad)
NB: Since 2*Math.PI < 2*PI, a span of 2*Math.PI does not mean full angular range.static boolean
isNaNOrInfinite(double value)
static boolean
isNaNOrInfinite(float value)
static double
log(double value)
static double
log10(double value)
static double
log1p(double value)
Much more accurate than log(1+value), for arguments (and results) close to zero.static int
log2(int value)
static int
log2(long value)
static double
logQuick(double value)
Quick log, with a max relative error of about 1.9e-3 for values in ]Double.MIN_NORMAL,+Infinity[, and worse accuracy outside this range.static double
max(double a, double b)
static float
max(float a, float b)
static int
max(int a, int b)
static long
max(long a, long b)
static double
min(double a, double b)
static float
min(float a, float b)
static int
min(int a, int b)
static long
min(long a, long b)
static int
multiplyBounded(int a, int b)
static long
multiplyBounded(long a, int b)
static long
multiplyBounded(long a, long b)
static int
multiplyExact(int a, int b)
static long
multiplyExact(long a, int b)
static long
multiplyExact(long a, long b)
static long
multiplyFull(int x, int y)
static long
multiplyHigh(long x, long y)
static int
negateBounded(int value)
static long
negateBounded(long value)
static int
negateExact(int value)
static long
negateExact(long value)
static double
nextAfter(double start, double direction)
If both arguments are +-0.0, direction is returned.static float
nextAfter(float start, double direction)
If both arguments are +-0.0(f), (float)direction is returned.static double
nextDown(double start)
Semantically equivalent to nextAfter(start,Double.NEGATIVE_INFINITY).static float
nextDown(float start)
Semantically equivalent to nextAfter(start,Double.NEGATIVE_INFINITY).static double
nextUp(double start)
Semantically equivalent to nextAfter(start,Double.POSITIVE_INFINITY).static float
nextUp(float start)
Semantically equivalent to nextAfter(start,Double.POSITIVE_INFINITY).static double
normalizeMinusHalfPiHalfPi(double angle)
static double
normalizeMinusHalfPiHalfPiFast(double angle)
Not accurate for large values.static double
normalizeMinusPiPi(double angle)
static double
normalizeMinusPiPiFast(double angle)
Not accurate for large values.static double
normalizeZeroTwoPi(double angle)
static double
normalizeZeroTwoPiFast(double angle)
Not accurate for large values.static double
pow(double value, double power)
1e-13ish accuracy or better on whole double range.static double
pow2(double value)
static float
pow2(float value)
static int
pow2(int value)
static long
pow2(long value)
static double
pow3(double value)
static float
pow3(float value)
static int
pow3(int value)
static long
pow3(long value)
static double
powFast(double value, int power)
This treatment is somehow accurate for low values of |power|, and for |power*getExponent(value)| < 1023 or so (to stay away from double extreme magnitudes (large and small)).static double
powQuick(double value, double power)
Quick pow, with a max relative error of about 1e-2 for value >= Double.MIN_NORMAL and 1e-10 < |value^power| < 1e10, of about 6e-2 for value >= Double.MIN_NORMAL and 1e-40 < |value^power| < 1e40, and worse accuracy otherwise.static double
random()
static double
remainder(double dividend, double divisor)
Returns dividend - divisor * n, where n is the mathematical integer closest to dividend/divisor.static double
rint(double value)
static float
rint(float value)
static long
round(double value)
Might have different semantics than Math.round(double), see bugs 6430675 and 8010430.static int
round(float value)
Might have different semantics than Math.round(float), see bugs 6430675 and 8010430.static long
roundEven(double value)
static int
roundEven(float value)
static int
roundEvenToInt(double value)
static int
roundToInt(double value)
static double
scalb(double value, int scaleFactor)
Precision may be lost if the result is subnormal.static float
scalb(float value, int scaleFactor)
Precision may be lost if the result is subnormal.static long
signFromBit(double value)
static int
signFromBit(float value)
static double
signum(double value)
static float
signum(float value)
static double
sin(double angle)
static double
sinAndCos(double angle, DoubleWrapper cosine)
Computes sine and cosine together.static double
sinh(double value)
Some properties of sinh(x) = (exp(x)-exp(-x))/2: 1) defined on ]-Infinity,+Infinity[ 2) result in ]-Infinity,+Infinity[ 3) sinh(x) = -sinh(-x) (implies sinh(0) = 0) 4) sinh(epsilon) ~= epsilon 5) lim(sinh(x),x->+Infinity) = +Infinity (y increasing exponentially faster than x) 6) reaches +Infinity (double overflow) for x >= 710.475860073944, i.e.static double
sinhAndCosh(double value, DoubleWrapper hcosine)
Computes hyperbolic sine and hyperbolic cosine together.static double
sinQuick(double angle)
Quick sin, with accuracy of about 1.6e-3 (PI/) for |angle| < 6588395.0 (Integer.MAX_VALUE * (2*PI/ ) - 2) (- 2 due to removing PI/2 before using cosine tab), and no accuracy at all for larger values. static double
sqrt(double value)
static double
sqrtQuick(double value)
Quick sqrt, with with a max relative error of about 3.41e-2 for values in [Double.MIN_NORMAL,Double.MAX_VALUE], and worse accuracy outside this range.static int
subtractBounded(int a, int b)
static long
subtractBounded(long a, long b)
static int
subtractExact(int a, int b)
static long
subtractExact(long a, long b)
static double
tan(double angle)
Can have very bad relative error near +-PI/2, but of the same magnitude than the relative delta between StrictMath.tan(PI/2) and StrictMath.tan(nextDown(PI/2)).static double
tanh(double value)
Some properties of tanh(x) = sinh(x)/cosh(x) = (exp(2*x)-1)/(exp(2*x)+1): 1) defined on ]-Infinity,+Infinity[ 2) result in ]-1,1[ 3) tanh(x) = -tanh(-x) (implies tanh(0) = 0) 4) tanh(epsilon) ~= epsilon 5) lim(tanh(x),x->+Infinity) = 1 6) reaches 1 (double loss of precision) for x = 19.061547465398498static double
toDegrees(boolean sign, int degrees, int minutes, double seconds)
static double
toDegrees(double angrad)
Gives same result as Math.toDegrees for some particular values like Math.PI/2, Math.PI or 2*Math.PI, but is faster (no division).static boolean
toDMS(double angrad, IntWrapper degrees, IntWrapper minutes, DoubleWrapper seconds)
static int
toInt(long value)
static int
toIntExact(long value)
static double
toRadians(boolean sign, int degrees, int minutes, double seconds)
static double
toRadians(double angdeg)
Gives same result as Math.toRadians for some particular values like 90.0, 180.0 or 360.0, but is faster (no division).static double
toRange(double min, double max, double value)
static float
toRange(float min, float max, float value)
static int
toRange(int min, int max, int value)
static long
toRange(long min, long max, long value)
static double
twoPow(int power)
Returns the exact result, provided it's in double range, i.e.static double
ulp(double value)
The ULP (Unit in the Last Place) is the distance to the next value larger in magnitude.static float
ulp(float value)
The ULP (Unit in the Last Place) is the distance to the next value larger in magnitude.
-
-
-
Field Detail
-
E
public static final double E
Closest double approximation of e.- See Also:
- Constant Field Values
-
PI
public static final double PI
Closest double approximation of pi, which is inferior to mathematical pi: pi ~= 3.14159265358979323846... PI ~= 3.141592653589793- See Also:
- Constant Field Values
-
PI_SUP
public static final double PI_SUP
High double approximation of pi, which is further from pi than the low approximation PI: pi ~= 3.14159265358979323846... PI ~= 3.141592653589793 PI_SUP ~= 3.1415926535897936
-
-
Method Detail
-
sin
public static double sin(double angle)
- Parameters:
angle
- Angle in radians.- Returns:
- Angle sine.
-
sinQuick
public static double sinQuick(double angle)
Quick sin, with accuracy of about 1.6e-3 (PI/) for |angle| < 6588395.0 (Integer.MAX_VALUE * (2*PI/ ) - 2) (- 2 due to removing PI/2 before using cosine tab), and no accuracy at all for larger values. - Parameters:
angle
- Angle in radians.- Returns:
- Angle sine.
-
cos
public static double cos(double angle)
- Parameters:
angle
- Angle in radians.- Returns:
- Angle cosine.
-
cosQuick
public static double cosQuick(double angle)
Quick cos, with accuracy of about 1.6e-3 (PI/) for |angle| < 6588397.0 (Integer.MAX_VALUE * (2*PI/ )), and no accuracy at all for larger values. - Parameters:
angle
- Angle in radians.- Returns:
- Angle cosine.
-
sinAndCos
public static double sinAndCos(double angle, DoubleWrapper cosine)
Computes sine and cosine together.- Parameters:
angle
- Angle in radians.cosine
- (out) Angle cosine.- Returns:
- Angle sine.
-
tan
public static double tan(double angle)
Can have very bad relative error near +-PI/2, but of the same magnitude than the relative delta between StrictMath.tan(PI/2) and StrictMath.tan(nextDown(PI/2)).- Parameters:
angle
- Angle in radians.- Returns:
- Angle tangent.
-
asin
public static double asin(double value)
- Parameters:
value
- Value in [-1,1].- Returns:
- Value arcsine, in radians, in [-PI/2,PI/2].
-
asinInRange
public static double asinInRange(double value)
If value is not NaN and is outside [-1,1] range, closest value in this range is used.- Parameters:
value
- Value in [-1,1].- Returns:
- Value arcsine, in radians, in [-PI/2,PI/2].
-
acos
public static double acos(double value)
- Parameters:
value
- Value in [-1,1].- Returns:
- Value arccosine, in radians, in [0,PI].
-
acosInRange
public static double acosInRange(double value)
If value is not NaN and is outside [-1,1] range, closest value in this range is used.- Parameters:
value
- Value in [-1,1].- Returns:
- Value arccosine, in radians, in [0,PI].
-
atan
public static double atan(double value)
- Parameters:
value
- A double value.- Returns:
- Value arctangent, in radians, in [-PI/2,PI/2].
-
atan2
public static double atan2(double y, double x)
For special values for which multiple conventions could be adopted, behaves like Math.atan2(double,double).- Parameters:
y
- Coordinate on y axis.x
- Coordinate on x axis.- Returns:
- Angle from x axis positive side to (x,y) position, in radians, in [-PI,PI]. Angle measure is positive when going from x axis to y axis (positive sides).
-
toRadians
public static double toRadians(double angdeg)
Gives same result as Math.toRadians for some particular values like 90.0, 180.0 or 360.0, but is faster (no division).- Parameters:
angdeg
- Angle value in degrees.- Returns:
- Angle value in radians.
-
toDegrees
public static double toDegrees(double angrad)
Gives same result as Math.toDegrees for some particular values like Math.PI/2, Math.PI or 2*Math.PI, but is faster (no division).- Parameters:
angrad
- Angle value in radians.- Returns:
- Angle value in degrees.
-
toRadians
public static double toRadians(boolean sign, int degrees, int minutes, double seconds)
- Parameters:
sign
- Sign of the angle: true for positive, false for negative.degrees
- Degrees, in [0,180].minutes
- Minutes, in [0,59].seconds
- Seconds, in [0.0,60.0[.- Returns:
- Angle in radians.
-
toDegrees
public static double toDegrees(boolean sign, int degrees, int minutes, double seconds)
- Parameters:
sign
- Sign of the angle: true for positive, false for negative.degrees
- Degrees, in [0,180].minutes
- Minutes, in [0,59].seconds
- Seconds, in [0.0,60.0[.- Returns:
- Angle in degrees.
-
toDMS
public static boolean toDMS(double angrad, IntWrapper degrees, IntWrapper minutes, DoubleWrapper seconds)
- Parameters:
angrad
- Angle in radians.degrees
- (out) Degrees, in [0,180].minutes
- (out) Minutes, in [0,59].seconds
- (out) Seconds, in [0.0,60.0[.- Returns:
- true if the resulting angle in [-180deg,180deg] is positive, false if it is negative.
-
isInClockwiseDomain
public static boolean isInClockwiseDomain(double startAngRad, double angSpanRad, double angRad)
NB: Since 2*Math.PI < 2*PI, a span of 2*Math.PI does not mean full angular range. ex.: isInClockwiseDomain(0.0, 2*Math.PI, -1e-20) returns false. ---> For full angular range, use a span > 2*Math.PI, like 2*PI_SUP constant of this class.- Parameters:
startAngRad
- An angle, in radians.angSpanRad
- An angular span, >= 0.0, in radians.angRad
- An angle, in radians.- Returns:
- true if angRad is in the clockwise angular domain going from startAngRad, over angSpanRad, extremities included, false otherwise.
-
sinh
public static double sinh(double value)
Some properties of sinh(x) = (exp(x)-exp(-x))/2: 1) defined on ]-Infinity,+Infinity[ 2) result in ]-Infinity,+Infinity[ 3) sinh(x) = -sinh(-x) (implies sinh(0) = 0) 4) sinh(epsilon) ~= epsilon 5) lim(sinh(x),x->+Infinity) = +Infinity (y increasing exponentially faster than x) 6) reaches +Infinity (double overflow) for x >= 710.475860073944, i.e. a bit further than exp(x)- Parameters:
value
- A double value.- Returns:
- Value hyperbolic sine.
-
cosh
public static double cosh(double value)
Some properties of cosh(x) = (exp(x)+exp(-x))/2: 1) defined on ]-Infinity,+Infinity[ 2) result in [1,+Infinity[ 3) cosh(0) = 1 4) cosh(x) = cosh(-x) 5) lim(cosh(x),x->+Infinity) = +Infinity (y increasing exponentially faster than x) 6) reaches +Infinity (double overflow) for x >= 710.475860073944, i.e. a bit further than exp(x)- Parameters:
value
- A double value.- Returns:
- Value hyperbolic cosine.
-
coshm1
public static double coshm1(double value)
Much more accurate than cosh(value)-1, for arguments (and results) close to zero. coshm1(-0.0) = -0.0, for homogeneity with acosh1p(-0.0) = -0.0.- Parameters:
value
- A double value.- Returns:
- Value hyperbolic cosine, minus 1.
-
sinhAndCosh
public static double sinhAndCosh(double value, DoubleWrapper hcosine)
Computes hyperbolic sine and hyperbolic cosine together.- Parameters:
value
- A double value.hcosine
- (out) Value hyperbolic cosine.- Returns:
- Value hyperbolic sine.
-
tanh
public static double tanh(double value)
Some properties of tanh(x) = sinh(x)/cosh(x) = (exp(2*x)-1)/(exp(2*x)+1): 1) defined on ]-Infinity,+Infinity[ 2) result in ]-1,1[ 3) tanh(x) = -tanh(-x) (implies tanh(0) = 0) 4) tanh(epsilon) ~= epsilon 5) lim(tanh(x),x->+Infinity) = 1 6) reaches 1 (double loss of precision) for x = 19.061547465398498- Parameters:
value
- A double value.- Returns:
- Value hyperbolic tangent.
-
asinh
public static double asinh(double value)
Some properties of asinh(x) = log(x + sqrt(x^2 + 1)) 1) defined on ]-Infinity,+Infinity[ 2) result in ]-Infinity,+Infinity[ 3) asinh(x) = -asinh(-x) (implies asinh(0) = 0) 4) asinh(epsilon) ~= epsilon 5) lim(asinh(x),x->+Infinity) = +Infinity (y increasing logarithmically slower than x)- Parameters:
value
- A double value.- Returns:
- Value hyperbolic arcsine.
-
acosh
public static double acosh(double value)
Some properties of acosh(x) = log(x + sqrt(x^2 - 1)): 1) defined on [1,+Infinity[ 2) result in ]0,+Infinity[ (by convention, since cosh(x) = cosh(-x)) 3) acosh(1) = 0 4) acosh(1+epsilon) ~= log(1 + sqrt(2*epsilon)) ~= sqrt(2*epsilon) 5) lim(acosh(x),x->+Infinity) = +Infinity (y increasing logarithmically slower than x)- Parameters:
value
- A double value.- Returns:
- Value hyperbolic arccosine.
-
acosh1p
public static double acosh1p(double value)
Much more accurate than acosh(1+value), for arguments (and results) close to zero. acosh1p(-0.0) = -0.0, for homogeneity with sqrt(-0.0) = -0.0, which looks about the same near 0.- Parameters:
value
- A double value.- Returns:
- Hyperbolic arccosine of (1+value).
-
atanh
public static double atanh(double value)
Some properties of atanh(x) = log((1+x)/(1-x))/2: 1) defined on ]-1,1[ 2) result in ]-Infinity,+Infinity[ 3) atanh(-1) = -Infinity (by continuity) 4) atanh(1) = +Infinity (by continuity) 5) atanh(epsilon) ~= epsilon 6) lim(atanh(x),x->1) = +Infinity- Parameters:
value
- A double value.- Returns:
- Value hyperbolic arctangent.
-
exp
public static double exp(double value)
- Parameters:
value
- A double value.- Returns:
- e^value.
-
expQuick
public static double expQuick(double value)
Quick exp, with a max relative error of about 2.94e-2 for |value| < 700.0 or so, and no accuracy at all outside this range. Derived from a note by Nicol N. Schraudolph, IDSIA, 1998.- Parameters:
value
- A double value.- Returns:
- e^value.
-
expm1
public static double expm1(double value)
Much more accurate than exp(value)-1, for arguments (and results) close to zero.- Parameters:
value
- A double value.- Returns:
- e^value-1.
-
log
public static double log(double value)
- Parameters:
value
- A double value.- Returns:
- Value logarithm (base e).
-
logQuick
public static double logQuick(double value)
Quick log, with a max relative error of about 1.9e-3 for values in ]Double.MIN_NORMAL,+Infinity[, and worse accuracy outside this range.- Parameters:
value
- A double value, in ]0,+Infinity[ (strictly positive and finite).- Returns:
- Value logarithm (base e).
-
log10
public static double log10(double value)
- Parameters:
value
- A double value.- Returns:
- Value logarithm (base 10).
-
log1p
public static double log1p(double value)
Much more accurate than log(1+value), for arguments (and results) close to zero.- Parameters:
value
- A double value.- Returns:
- Logarithm (base e) of (1+value).
-
pow
public static double pow(double value, double power)
1e-13ish accuracy or better on whole double range.- Parameters:
value
- A double value.power
- A power.- Returns:
- value^power.
-
powQuick
public static double powQuick(double value, double power)
Quick pow, with a max relative error of about 1e-2 for value >= Double.MIN_NORMAL and 1e-10 < |value^power| < 1e10, of about 6e-2 for value >= Double.MIN_NORMAL and 1e-40 < |value^power| < 1e40, and worse accuracy otherwise.- Parameters:
value
- A double value, in ]0,+Infinity[ (strictly positive and finite).power
- A double value.- Returns:
- value^power.
-
powFast
public static double powFast(double value, int power)
This treatment is somehow accurate for low values of |power|, and for |power*getExponent(value)| < 1023 or so (to stay away from double extreme magnitudes (large and small)).- Parameters:
value
- A double value.power
- A power.- Returns:
- value^power.
-
pow2
public static float pow2(float value)
- Parameters:
value
- A float value.- Returns:
- value*value.
-
pow2
public static double pow2(double value)
- Parameters:
value
- A double value.- Returns:
- value*value.
-
pow3
public static float pow3(float value)
- Parameters:
value
- A float value.- Returns:
- value*value*value.
-
pow3
public static double pow3(double value)
- Parameters:
value
- A double value.- Returns:
- value*value*value.
-
sqrt
public static double sqrt(double value)
- Parameters:
value
- A double value.- Returns:
- Value square root.
-
sqrtQuick
public static double sqrtQuick(double value)
Quick sqrt, with with a max relative error of about 3.41e-2 for values in [Double.MIN_NORMAL,Double.MAX_VALUE], and worse accuracy outside this range.- Parameters:
value
- A double value.- Returns:
- Value square root.
-
invSqrtQuick
public static double invSqrtQuick(double value)
Quick inverse of square root, with a max relative error of about 3.44e-2 for values in [Double.MIN_NORMAL,Double.MAX_VALUE], and worse accuracy outside this range. This implementation uses zero step of Newton's method. Here are the max relative errors on [Double.MIN_NORMAL,Double.MAX_VALUE] depending on number of steps, if you want to copy-paste this code and use your own number: n=0: about 3.44e-2 n=1: about 1.75e-3 n=2: about 4.6e-6 n=3: about 3.17e-11 n=4: about 3.92e-16 n=5: about 3.03e-16- Parameters:
value
- A double value.- Returns:
- Inverse of value square root.
-
cbrt
public static double cbrt(double value)
- Parameters:
value
- A double value.- Returns:
- Value cubic root.
-
hypot
public static double hypot(double x, double y)
- Returns:
- sqrt(x^2+y^2) without intermediate overflow or underflow.
-
hypot
public static double hypot(double x, double y, double z)
- Returns:
- sqrt(x^2+y^2+z^2) without intermediate overflow or underflow.
-
floor
public static float floor(float value)
- Parameters:
value
- A float value.- Returns:
- Floor of value.
-
floor
public static double floor(double value)
- Parameters:
value
- A double value.- Returns:
- Floor of value.
-
ceil
public static float ceil(float value)
- Parameters:
value
- A float value.- Returns:
- Ceiling of value.
-
ceil
public static double ceil(double value)
- Parameters:
value
- A double value.- Returns:
- Ceiling of value.
-
round
public static int round(float value)
Might have different semantics than Math.round(float), see bugs 6430675 and 8010430.- Parameters:
value
- A double value.- Returns:
- Value rounded to nearest int, choosing superior int in case two are equally close (i.e. rounding-up).
-
round
public static long round(double value)
Might have different semantics than Math.round(double), see bugs 6430675 and 8010430.- Parameters:
value
- A double value.- Returns:
- Value rounded to nearest long, choosing superior long in case two are equally close (i.e. rounding-up).
-
roundEven
public static int roundEven(float value)
- Parameters:
value
- A float value.- Returns:
- Value rounded to nearest int, choosing even int in case two are equally close.
-
roundEven
public static long roundEven(double value)
- Parameters:
value
- A double value.- Returns:
- Value rounded to nearest long, choosing even long in case two are equally close.
-
rint
public static float rint(float value)
- Parameters:
value
- A float value.- Returns:
- The float mathematical integer closest to the specified value, choosing even one if two are equally close, or respectively NaN, +-Infinity or +-0.0f if the value is any of these.
-
rint
public static double rint(double value)
- Parameters:
value
- A double value.- Returns:
- The double mathematical integer closest to the specified value, choosing even one if two are equally close, or respectively NaN, +-Infinity or +-0.0 if the value is any of these.
-
floorToInt
public static int floorToInt(double value)
- Parameters:
value
- A double value.- Returns:
- Floor of value as int, or closest int if floor is out of int range, or 0 if value is NaN.
-
ceilToInt
public static int ceilToInt(double value)
- Parameters:
value
- A double value.- Returns:
- Ceiling of value as int, or closest int if ceiling is out of int range, or 0 if value is NaN.
-
roundToInt
public static int roundToInt(double value)
- Parameters:
value
- A double value.- Returns:
- Value rounded to nearest int, choosing superior int in case two are equally close (i.e. rounding-up).
-
roundEvenToInt
public static int roundEvenToInt(double value)
- Parameters:
value
- A double value.- Returns:
- Value rounded to nearest int, choosing even int in case two are equally close.
-
toRange
public static float toRange(float min, float max, float value)
- Parameters:
min
- A float value.max
- A float value.value
- A float value.- Returns:
- min if value < min, max if value > max, value otherwise.
-
toRange
public static double toRange(double min, double max, double value)
- Parameters:
min
- A double value.max
- A double value.value
- A double value.- Returns:
- min if value < min, max if value > max, value otherwise.
-
remainder
public static double remainder(double dividend, double divisor)
Returns dividend - divisor * n, where n is the mathematical integer closest to dividend/divisor. If dividend/divisor is equally close to surrounding integers, we choose n to be the integer of smallest magnitude, which makes this treatment differ from Math.IEEEremainder(double,double), where n is chosen to be the even integer. Note that the choice of n is not done considering the double approximation of dividend/divisor, because it could cause result to be outside [-|divisor|/2,|divisor|/2] range. The practical effect is that if multiple results would be possible, we always choose the result that is the closest to (and has the same sign as) the dividend. Ex. : - for (-3.0,2.0), this method returns -1.0, whereas Math.IEEEremainder returns 1.0. - for (-5.0,2.0), both this method and Math.IEEEremainder return -1.0. If the remainder is zero, its sign is the same as the sign of the first argument. If either argument is NaN, or the first argument is infinite, or the second argument is positive zero or negative zero, then the result is NaN. If the first argument is finite and the second argument is infinite, then the result is the same as the first argument. NB: - Modulo operator (%) returns a value in ]-|divisor|,|divisor|[, which sign is the same as dividend. - As for modulo operator, the sign of the divisor has no effect on the result. - On some architecture, % operator has been observed to return NaN for some subnormal values of divisor, when dividend exponent is 1023, which impacts the correctness of this method.- Parameters:
dividend
- Dividend.divisor
- Divisor.- Returns:
- Remainder of dividend/divisor, i.e. a value in [-|divisor|/2,|divisor|/2].
-
normalizeMinusPiPi
public static double normalizeMinusPiPi(double angle)
- Parameters:
angle
- Angle in radians.- Returns:
- The same angle, in radians, but in [-PI,PI].
-
normalizeMinusPiPiFast
public static double normalizeMinusPiPiFast(double angle)
Not accurate for large values.- Parameters:
angle
- Angle in radians.- Returns:
- The same angle, in radians, but in [-PI,PI].
-
normalizeZeroTwoPi
public static double normalizeZeroTwoPi(double angle)
- Parameters:
angle
- Angle in radians.- Returns:
- The same angle, in radians, but in [0,2*PI].
-
normalizeZeroTwoPiFast
public static double normalizeZeroTwoPiFast(double angle)
Not accurate for large values.- Parameters:
angle
- Angle in radians.- Returns:
- The same angle, in radians, but in [0,2*PI].
-
normalizeMinusHalfPiHalfPi
public static double normalizeMinusHalfPiHalfPi(double angle)
- Parameters:
angle
- Angle in radians.- Returns:
- Angle value modulo PI, in radians, in [-PI/2,PI/2].
-
normalizeMinusHalfPiHalfPiFast
public static double normalizeMinusHalfPiHalfPiFast(double angle)
Not accurate for large values.- Parameters:
angle
- Angle in radians.- Returns:
- Angle value modulo PI, in radians, in [-PI/2,PI/2].
-
isNaNOrInfinite
public static boolean isNaNOrInfinite(float value)
- Parameters:
value
- A float value.- Returns:
- true if the specified value is NaN or +-Infinity, false otherwise.
-
isNaNOrInfinite
public static boolean isNaNOrInfinite(double value)
- Parameters:
value
- A double value.- Returns:
- true if the specified value is NaN or +-Infinity, false otherwise.
-
getExponent
public static int getExponent(float value)
- Parameters:
value
- A float value.- Returns:
- Value unbiased exponent.
-
getExponent
public static int getExponent(double value)
- Parameters:
value
- A double value.- Returns:
- Value unbiased exponent.
-
signum
public static float signum(float value)
- Parameters:
value
- A float value.- Returns:
- -1.0f if the specified value is < 0, 1.0f if it is > 0, and the value itself if it is NaN or +-0.0f.
-
signum
public static double signum(double value)
- Parameters:
value
- A double value.- Returns:
- -1.0 if the specified value is < 0, 1.0 if it is > 0, and the value itself if it is NaN or +-0.0.
-
signFromBit
public static int signFromBit(float value)
- Parameters:
value
- A float value.- Returns:
- -1 if sign bit is 1, 1 if sign bit is 0.
-
signFromBit
public static long signFromBit(double value)
- Parameters:
value
- A double value.- Returns:
- -1 if sign bit is 1, 1 if sign bit is 0.
-
copySign
public static float copySign(float magnitude, float sign)
A sign of NaN can be interpreted as positive or negative.- Parameters:
magnitude
- A float value.sign
- A float value.- Returns:
- A value with the magnitude of the first argument, and the sign of the second argument.
-
copySign
public static double copySign(double magnitude, double sign)
A sign of NaN can be interpreted as positive or negative.- Parameters:
magnitude
- A double value.sign
- A double value.- Returns:
- A value with the magnitude of the first argument, and the sign of the second argument.
-
ulp
public static float ulp(float value)
The ULP (Unit in the Last Place) is the distance to the next value larger in magnitude.- Parameters:
value
- A float value.- Returns:
- The size of an ulp of the specified value, or Float.MIN_VALUE if it is +-0.0f, or +Infinity if it is +-Infinity, or NaN if it is NaN.
-
ulp
public static double ulp(double value)
The ULP (Unit in the Last Place) is the distance to the next value larger in magnitude.- Parameters:
value
- A double value.- Returns:
- The size of an ulp of the specified value, or Double.MIN_VALUE if it is +-0.0, or +Infinity if it is +-Infinity, or NaN if it is NaN.
-
nextAfter
public static float nextAfter(float start, double direction)
If both arguments are +-0.0(f), (float)direction is returned. If both arguments are +Infinity or -Infinity, respectively +Infinity or -Infinity is returned.- Parameters:
start
- A float value.direction
- A double value.- Returns:
- The float adjacent to start towards direction, considering that +(-)Float.MIN_VALUE is adjacent to +(-)0.0f, and that +(-)Float.MAX_VALUE is adjacent to +(-)Infinity, or NaN if any argument is NaN.
-
nextAfter
public static double nextAfter(double start, double direction)
If both arguments are +-0.0, direction is returned. If both arguments are +Infinity or -Infinity, respectively +Infinity or -Infinity is returned.- Parameters:
start
- A double value.direction
- A double value.- Returns:
- The double adjacent to start towards direction, considering that +(-)Double.MIN_VALUE is adjacent to +(-)0.0, and that +(-)Double.MAX_VALUE is adjacent to +(-)Infinity, or NaN if any argument is NaN.
-
nextDown
public static float nextDown(float start)
Semantically equivalent to nextAfter(start,Double.NEGATIVE_INFINITY).
-
nextDown
public static double nextDown(double start)
Semantically equivalent to nextAfter(start,Double.NEGATIVE_INFINITY).
-
nextUp
public static float nextUp(float start)
Semantically equivalent to nextAfter(start,Double.POSITIVE_INFINITY).
-
nextUp
public static double nextUp(double start)
Semantically equivalent to nextAfter(start,Double.POSITIVE_INFINITY).
-
scalb
public static float scalb(float value, int scaleFactor)
Precision may be lost if the result is subnormal.- Parameters:
value
- A float value.scaleFactor
- An int value.- Returns:
- value * 2^scaleFactor, or a value equivalent to the specified one if it is NaN, +-Infinity or +-0.0f.
-
scalb
public static double scalb(double value, int scaleFactor)
Precision may be lost if the result is subnormal.- Parameters:
value
- A double value.scaleFactor
- An int value.- Returns:
- value * 2^scaleFactor, or a value equivalent to the specified one if it is NaN, +-Infinity or +-0.0.
-
abs
public static float abs(float a)
-
abs
public static double abs(double a)
-
min
public static float min(float a, float b)
-
min
public static double min(double a, double b)
-
max
public static float max(float a, float b)
-
max
public static double max(double a, double b)
-
IEEEremainder
public static double IEEEremainder(double f1, double f2)
-
random
public static double random()
-
initTables
public static void initTables()
Ensures that all look-up tables are initialized - otherwise they are initialized lazily.
-
log2
public static int log2(int value)
- Parameters:
value
- An integer value in [1,Integer.MAX_VALUE].- Returns:
- The integer part of the logarithm, in base 2, of the specified value, i.e. a result in [0,30]
- Throws:
IllegalArgumentException
- if the specified value is <= 0.
-
log2
public static int log2(long value)
- Parameters:
value
- An integer value in [1,Long.MAX_VALUE].- Returns:
- The integer part of the logarithm, in base 2, of the specified value, i.e. a result in [0,62]
- Throws:
IllegalArgumentException
- if the specified value is <= 0.
-
twoPow
public static double twoPow(int power)
Returns the exact result, provided it's in double range, i.e. if power is in [-1074,1023].- Parameters:
power
- An int power.- Returns:
- 2^power as a double, or +-Infinity in case of overflow.
-
pow2
public static int pow2(int value)
- Parameters:
value
- An int value.- Returns:
- value*value.
-
pow2
public static long pow2(long value)
- Parameters:
value
- A long value.- Returns:
- value*value.
-
pow3
public static int pow3(int value)
- Parameters:
value
- An int value.- Returns:
- value*value*value.
-
pow3
public static long pow3(long value)
- Parameters:
value
- A long value.- Returns:
- value*value*value.
-
abs
public static int abs(int value)
- Parameters:
value
- An int value.- Returns:
- The absolute value, except if value is Integer.MIN_VALUE, for which it returns Integer.MIN_VALUE.
-
abs
public static long abs(long value)
- Parameters:
value
- A long value.- Returns:
- The absolute value, except if value is Long.MIN_VALUE, for which it returns Long.MIN_VALUE.
-
toIntExact
public static int toIntExact(long value)
- Parameters:
value
- A long value.- Returns:
- The specified value as int.
- Throws:
ArithmeticException
- if the specified value is not in [Integer.MIN_VALUE,Integer.MAX_VALUE] range.
-
toInt
public static int toInt(long value)
- Parameters:
value
- A long value.- Returns:
- The closest int value in [Integer.MIN_VALUE,Integer.MAX_VALUE] range.
-
toRange
public static int toRange(int min, int max, int value)
- Parameters:
min
- An int value.max
- An int value.value
- An int value.- Returns:
- minValue if value < minValue, maxValue if value > maxValue, value otherwise.
-
toRange
public static long toRange(long min, long max, long value)
- Parameters:
min
- A long value.max
- A long value.value
- A long value.- Returns:
- min if value < min, max if value > max, value otherwise.
-
incrementExact
public static int incrementExact(int value)
- Parameters:
value
- An int value.- Returns:
- The argument incremented by one.
- Throws:
ArithmeticException
- if the mathematical result is not in int range.
-
incrementExact
public static long incrementExact(long value)
- Parameters:
value
- A long value.- Returns:
- The argument incremented by one.
- Throws:
ArithmeticException
- if the mathematical result is not in long range.
-
incrementBounded
public static int incrementBounded(int value)
- Parameters:
value
- An int value.- Returns:
- The argument incremented by one, or the argument if the mathematical result is not in int range.
-
incrementBounded
public static long incrementBounded(long value)
- Parameters:
value
- A long value.- Returns:
- The argument incremented by one, or the argument if the mathematical result is not in long range.
-
decrementExact
public static int decrementExact(int value)
- Parameters:
value
- An int value.- Returns:
- The argument decremented by one.
- Throws:
ArithmeticException
- if the mathematical result is not in int range.
-
decrementExact
public static long decrementExact(long value)
- Parameters:
value
- A long value.- Returns:
- The argument decremented by one.
- Throws:
ArithmeticException
- if the mathematical result is not in long range.
-
decrementBounded
public static int decrementBounded(int value)
- Parameters:
value
- An int value.- Returns:
- The argument decremented by one, or the argument if the mathematical result is not in int range.
-
decrementBounded
public static long decrementBounded(long value)
- Parameters:
value
- A long value.- Returns:
- The argument decremented by one, or the argument if the mathematical result is not in long range.
-
negateExact
public static int negateExact(int value)
- Parameters:
value
- An int value.- Returns:
- The argument negated.
- Throws:
ArithmeticException
- if the mathematical result is not in int range.
-
negateExact
public static long negateExact(long value)
- Parameters:
value
- A long value.- Returns:
- The argument negated.
- Throws:
ArithmeticException
- if the mathematical result is not in long range.
-
negateBounded
public static int negateBounded(int value)
- Parameters:
value
- An int value.- Returns:
- The argument negated, or Integer.MAX_VALUE if the argument is Integer.MIN_VALUE.
-
negateBounded
public static long negateBounded(long value)
- Parameters:
value
- A long value.- Returns:
- The argument negated, or Long.MAX_VALUE if the argument is Long.MIN_VALUE.
-
addExact
public static int addExact(int a, int b)
- Parameters:
a
- An int value.b
- An int value.- Returns:
- The mathematical result of a+b.
- Throws:
ArithmeticException
- if the mathematical result of a+b is not in [Integer.MIN_VALUE,Integer.MAX_VALUE] range.
-
addExact
public static long addExact(long a, long b)
- Parameters:
a
- A long value.b
- A long value.- Returns:
- The mathematical result of a+b.
- Throws:
ArithmeticException
- if the mathematical result of a+b is not in [Long.MIN_VALUE,Long.MAX_VALUE] range.
-
addBounded
public static int addBounded(int a, int b)
- Parameters:
a
- An int value.b
- An int value.- Returns:
- The int value of [Integer.MIN_VALUE,Integer.MAX_VALUE] range which is the closest to mathematical result of a+b.
-
addBounded
public static long addBounded(long a, long b)
- Parameters:
a
- A long value.b
- A long value.- Returns:
- The long value of [Long.MIN_VALUE,Long.MAX_VALUE] range which is the closest to mathematical result of a+b.
-
subtractExact
public static int subtractExact(int a, int b)
- Parameters:
a
- An int value.b
- An int value.- Returns:
- The mathematical result of a-b.
- Throws:
ArithmeticException
- if the mathematical result of a-b is not in [Integer.MIN_VALUE,Integer.MAX_VALUE] range.
-
subtractExact
public static long subtractExact(long a, long b)
- Parameters:
a
- A long value.b
- A long value.- Returns:
- The mathematical result of a-b.
- Throws:
ArithmeticException
- if the mathematical result of a-b is not in [Long.MIN_VALUE,Long.MAX_VALUE] range.
-
subtractBounded
public static int subtractBounded(int a, int b)
- Parameters:
a
- An int value.b
- An int value.- Returns:
- The int value of [Integer.MIN_VALUE,Integer.MAX_VALUE] range which is the closest to mathematical result of a-b.
-
subtractBounded
public static long subtractBounded(long a, long b)
- Parameters:
a
- A long value.b
- A long value.- Returns:
- The long value of [Long.MIN_VALUE,Long.MAX_VALUE] range which is the closest to mathematical result of a-b.
-
multiplyExact
public static int multiplyExact(int a, int b)
- Parameters:
a
- An int value.b
- An int value.- Returns:
- The mathematical result of a*b.
- Throws:
ArithmeticException
- if the mathematical result of a*b is not in [Integer.MIN_VALUE,Integer.MAX_VALUE] range.
-
multiplyExact
public static long multiplyExact(long a, int b)
- Parameters:
a
- A long value.b
- An int value.- Returns:
- The mathematical result of a*b.
- Throws:
ArithmeticException
- if the mathematical result of a*b is not in [Long.MIN_VALUE,Long.MAX_VALUE] range.
-
multiplyExact
public static long multiplyExact(long a, long b)
- Parameters:
a
- A long value.b
- A long value.- Returns:
- The mathematical result of a*b.
- Throws:
ArithmeticException
- if the mathematical result of a*b is not in [Long.MIN_VALUE,Long.MAX_VALUE] range.
-
multiplyBounded
public static int multiplyBounded(int a, int b)
- Parameters:
a
- An int value.b
- An int value.- Returns:
- The int value of [Integer.MIN_VALUE,Integer.MAX_VALUE] range which is the closest to mathematical result of a*b.
-
multiplyBounded
public static long multiplyBounded(long a, int b)
- Parameters:
a
- A long value.b
- An int value.- Returns:
- The long value of [Long.MIN_VALUE,Long.MAX_VALUE] range which is the closest to mathematical result of a*b.
-
multiplyBounded
public static long multiplyBounded(long a, long b)
- Parameters:
a
- A long value.b
- A long value.- Returns:
- The long value of [Long.MIN_VALUE,Long.MAX_VALUE] range which is the closest to mathematical result of a*b.
-
multiplyFull
public static long multiplyFull(int x, int y)
- Parameters:
x
- An int value.y
- An int value.- Returns:
- The mathematical product as a long.
-
multiplyHigh
public static long multiplyHigh(long x, long y)
- Parameters:
x
- A long value.y
- A long value.- Returns:
- The most significant 64 bits of the 128-bit product of two 64-bit factors.
-
floorDiv
public static int floorDiv(int x, int y)
Returns the largest int <= dividend/divisor. Unlike "/" operator, which rounds towards 0, this division rounds towards -Infinity (which give different result when the exact result is negative).- Parameters:
x
- The dividend.y
- The divisor.- Returns:
- The largest int <= dividend/divisor, unless dividend is Integer.MIN_VALUE and divisor is -1, in which case Integer.MIN_VALUE is returned.
- Throws:
ArithmeticException
- if the divisor is zero.
-
floorDiv
public static long floorDiv(long x, int y)
Returns the largest long <= dividend/divisor. Unlike "/" operator, which rounds towards 0, this division rounds towards -Infinity (which give different result when the exact result is negative).- Parameters:
x
- The dividend.y
- The divisor.- Returns:
- The largest long <= dividend/divisor, unless dividend is Long.MIN_VALUE and divisor is -1, in which case Long.MIN_VALUE is returned.
- Throws:
ArithmeticException
- if the divisor is zero.
-
floorDiv
public static long floorDiv(long x, long y)
Returns the largest long <= dividend/divisor. Unlike "/" operator, which rounds towards 0, this division rounds towards -Infinity (which give different result when the exact result is negative).- Parameters:
x
- The dividend.y
- The divisor.- Returns:
- The largest long <= dividend/divisor, unless dividend is Long.MIN_VALUE and divisor is -1, in which case Long.MIN_VALUE is returned.
- Throws:
ArithmeticException
- if the divisor is zero.
-
floorMod
public static int floorMod(int x, int y)
Returns the floor modulus, which is "x - floorDiv(x,y) * y", has the same sign as y, and is in ]-abs(y),abs(y)[. The relationship between floorMod and floorDiv is the same than between "%" and "/".- Parameters:
x
- The dividend.y
- The divisor.- Returns:
- The floor modulus, i.e. "x - (floorDiv(x, y) * y)".
- Throws:
ArithmeticException
- if the divisor is zero.
-
floorMod
public static int floorMod(long x, int y)
Returns the floor modulus, which is "x - floorDiv(x,y) * y", has the same sign as y, and is in ]-abs(y),abs(y)[. The relationship between floorMod and floorDiv is the same than between "%" and "/".- Parameters:
x
- The dividend.y
- The divisor.- Returns:
- The floor modulus, i.e. "x - (floorDiv(x, y) * y)".
- Throws:
ArithmeticException
- if the divisor is zero.
-
floorMod
public static long floorMod(long x, long y)
Returns the floor modulus, which is "x - floorDiv(x,y) * y", has the same sign as y, and is in ]-abs(y),abs(y)[. The relationship between floorMod and floorDiv is the same than between "%" and "/".- Parameters:
x
- The dividend.y
- The divisor.- Returns:
- The floor modulus, i.e. "x - (floorDiv(x, y) * y)".
- Throws:
ArithmeticException
- if the divisor is zero.
-
min
public static int min(int a, int b)
-
min
public static long min(long a, long b)
-
max
public static int max(int a, int b)
-
max
public static long max(long a, long b)
-
-