public final class FastMath extends Object
Modifier and Type | Field and 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...
|
Modifier and Type | Method and 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) = +Infinity
|
static double |
cbrt(double value) |
static double |
ceil(double value) |
static float |
ceil(float 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/
|
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 |
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 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/
|
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.061547465398498
|
static 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.
|
public static final double E
public static final double PI
public static final double PI_SUP
public static double sin(double angle)
angle
- Angle in radians.public static double sinQuick(double angle)
angle
- Angle in radians.public static double cos(double angle)
angle
- Angle in radians.public static double cosQuick(double angle)
angle
- Angle in radians.public static double sinAndCos(double angle, DoubleWrapper cosine)
angle
- Angle in radians.cosine
- (out) Angle cosine.public static double tan(double angle)
angle
- Angle in radians.public static double asin(double value)
value
- Value in [-1,1].public static double asinInRange(double value)
value
- Value in [-1,1].public static double acos(double value)
value
- Value in [-1,1].public static double acosInRange(double value)
value
- Value in [-1,1].public static double atan(double value)
value
- A double value.public static double atan2(double y, double x)
y
- Coordinate on y axis.x
- Coordinate on x axis.public static double toRadians(double angdeg)
angdeg
- Angle value in degrees.public static double toDegrees(double angrad)
angrad
- Angle value in radians.public static double toRadians(boolean sign, int degrees, int minutes, double seconds)
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[.public static double toDegrees(boolean sign, int degrees, int minutes, double seconds)
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[.public static boolean toDMS(double angrad, IntWrapper degrees, IntWrapper minutes, DoubleWrapper seconds)
angrad
- Angle in radians.degrees
- (out) Degrees, in [0,180].minutes
- (out) Minutes, in [0,59].seconds
- (out) Seconds, in [0.0,60.0[.public static boolean isInClockwiseDomain(double startAngRad, double angSpanRad, double angRad)
startAngRad
- An angle, in radians.angSpanRad
- An angular span, >= 0.0, in radians.angRad
- An angle, in radians.public static double sinh(double value)
value
- A double value.public static double cosh(double value)
value
- A double value.public static double coshm1(double value)
value
- A double value.public static double sinhAndCosh(double value, DoubleWrapper hcosine)
value
- A double value.hcosine
- (out) Value hyperbolic cosine.public static double tanh(double value)
value
- A double value.public static double asinh(double value)
value
- A double value.public static double acosh(double value)
value
- A double value.public static double acosh1p(double value)
value
- A double value.public static double atanh(double value)
value
- A double value.public static double exp(double value)
value
- A double value.public static double expQuick(double value)
value
- A double value.public static double expm1(double value)
value
- A double value.public static double log(double value)
value
- A double value.public static double logQuick(double value)
value
- A double value, in ]0,+Infinity[ (strictly positive and finite).public static double log10(double value)
value
- A double value.public static double log1p(double value)
value
- A double value.public static double pow(double value, double power)
value
- A double value.power
- A power.public static double powQuick(double value, double power)
value
- A double value, in ]0,+Infinity[ (strictly positive and finite).power
- A double value.public static double powFast(double value, int power)
value
- A double value.power
- A power.public static float pow2(float value)
value
- A float value.public static double pow2(double value)
value
- A double value.public static float pow3(float value)
value
- A float value.public static double pow3(double value)
value
- A double value.public static double sqrt(double value)
value
- A double value.public static double sqrtQuick(double value)
value
- A double value.public static double invSqrtQuick(double value)
value
- A double value.public static double cbrt(double value)
value
- A double value.public static double hypot(double x, double y)
public static double hypot(double x, double y, double z)
public static float floor(float value)
value
- A float value.public static double floor(double value)
value
- A double value.public static float ceil(float value)
value
- A float value.public static double ceil(double value)
value
- A double value.public static int round(float value)
value
- A double value.public static long round(double value)
value
- A double value.public static int roundEven(float value)
value
- A float value.public static long roundEven(double value)
value
- A double value.public static float rint(float value)
value
- A float value.public static double rint(double value)
value
- A double value.public static float toRange(float min, float max, float value)
min
- A float value.max
- A float value.value
- A float value.public static double toRange(double min, double max, double value)
min
- A double value.max
- A double value.value
- A double value.public static double remainder(double dividend, double divisor)
dividend
- Dividend.divisor
- Divisor.public static double normalizeMinusPiPi(double angle)
angle
- Angle in radians.public static double normalizeMinusPiPiFast(double angle)
angle
- Angle in radians.public static double normalizeZeroTwoPi(double angle)
angle
- Angle in radians.public static double normalizeZeroTwoPiFast(double angle)
angle
- Angle in radians.public static double normalizeMinusHalfPiHalfPi(double angle)
angle
- Angle in radians.public static double normalizeMinusHalfPiHalfPiFast(double angle)
angle
- Angle in radians.public static boolean isNaNOrInfinite(float value)
value
- A float value.public static boolean isNaNOrInfinite(double value)
value
- A double value.public static int getExponent(float value)
value
- A float value.public static int getExponent(double value)
value
- A double value.public static float signum(float value)
value
- A float value.public static double signum(double value)
value
- A double value.public static int signFromBit(float value)
value
- A float value.public static long signFromBit(double value)
value
- A double value.public static float copySign(float magnitude, float sign)
magnitude
- A float value.sign
- A float value.public static double copySign(double magnitude, double sign)
magnitude
- A double value.sign
- A double value.public static float ulp(float value)
value
- A float value.public static double ulp(double value)
value
- A double value.public static float nextAfter(float start, double direction)
start
- A float value.direction
- A double value.public static double nextAfter(double start, double direction)
start
- A double value.direction
- A double value.public static float nextDown(float start)
public static double nextDown(double start)
public static float nextUp(float start)
public static double nextUp(double start)
public static float scalb(float value, int scaleFactor)
value
- A float value.scaleFactor
- An int value.public static double scalb(double value, int scaleFactor)
value
- A double value.scaleFactor
- An int value.public static float abs(float a)
public static double abs(double a)
public static float min(float a, float b)
public static double min(double a, double b)
public static float max(float a, float b)
public static double max(double a, double b)
public static double IEEEremainder(double f1, double f2)
public static double random()
public static void initTables()
public static int log2(int value)
value
- An integer value in [1,Integer.MAX_VALUE].IllegalArgumentException
- if the specified value is <= 0.public static int log2(long value)
value
- An integer value in [1,Long.MAX_VALUE].IllegalArgumentException
- if the specified value is <= 0.public static double twoPow(int power)
power
- An int power.public static int pow2(int value)
value
- An int value.public static long pow2(long value)
value
- A long value.public static int pow3(int value)
value
- An int value.public static long pow3(long value)
value
- A long value.public static int abs(int value)
value
- An int value.public static long abs(long value)
value
- A long value.public static int toIntExact(long value)
value
- A long value.ArithmeticException
- if the specified value is not in [Integer.MIN_VALUE,Integer.MAX_VALUE] range.public static int toInt(long value)
value
- A long value.public static int toRange(int min, int max, int value)
min
- An int value.max
- An int value.value
- An int value.public static long toRange(long min, long max, long value)
min
- A long value.max
- A long value.value
- A long value.public static int incrementExact(int value)
value
- An int value.ArithmeticException
- if the mathematical result
is not in int range.public static long incrementExact(long value)
value
- A long value.ArithmeticException
- if the mathematical result
is not in long range.public static int incrementBounded(int value)
value
- An int value.public static long incrementBounded(long value)
value
- A long value.public static int decrementExact(int value)
value
- An int value.ArithmeticException
- if the mathematical result
is not in int range.public static long decrementExact(long value)
value
- A long value.ArithmeticException
- if the mathematical result
is not in long range.public static int decrementBounded(int value)
value
- An int value.public static long decrementBounded(long value)
value
- A long value.public static int negateExact(int value)
value
- An int value.ArithmeticException
- if the mathematical result
is not in int range.public static long negateExact(long value)
value
- A long value.ArithmeticException
- if the mathematical result
is not in long range.public static int negateBounded(int value)
value
- An int value.public static long negateBounded(long value)
value
- A long value.public static int addExact(int a, int b)
a
- An int value.b
- An int value.ArithmeticException
- if the mathematical result of a+b is not in [Integer.MIN_VALUE,Integer.MAX_VALUE] range.public static long addExact(long a, long b)
a
- A long value.b
- A long value.ArithmeticException
- if the mathematical result of a+b is not in [Long.MIN_VALUE,Long.MAX_VALUE] range.public static int addBounded(int a, int b)
a
- An int value.b
- An int value.public static long addBounded(long a, long b)
a
- A long value.b
- A long value.public static int subtractExact(int a, int b)
a
- An int value.b
- An int value.ArithmeticException
- if the mathematical result of a-b is not in [Integer.MIN_VALUE,Integer.MAX_VALUE] range.public static long subtractExact(long a, long b)
a
- A long value.b
- A long value.ArithmeticException
- if the mathematical result of a-b is not in [Long.MIN_VALUE,Long.MAX_VALUE] range.public static int subtractBounded(int a, int b)
a
- An int value.b
- An int value.public static long subtractBounded(long a, long b)
a
- A long value.b
- A long value.public static int multiplyExact(int a, int b)
a
- An int value.b
- An int value.ArithmeticException
- if the mathematical result of a*b is not in [Integer.MIN_VALUE,Integer.MAX_VALUE] range.public static long multiplyExact(long a, int b)
a
- A long value.b
- An int value.ArithmeticException
- if the mathematical result of a*b is not in [Long.MIN_VALUE,Long.MAX_VALUE] range.public static long multiplyExact(long a, long b)
a
- A long value.b
- A long value.ArithmeticException
- if the mathematical result of a*b is not in [Long.MIN_VALUE,Long.MAX_VALUE] range.public static int multiplyBounded(int a, int b)
a
- An int value.b
- An int value.public static long multiplyBounded(long a, int b)
a
- A long value.b
- An int value.public static long multiplyBounded(long a, long b)
a
- A long value.b
- A long value.public static long multiplyFull(int x, int y)
x
- An int value.y
- An int value.public static long multiplyHigh(long x, long y)
x
- A long value.y
- A long value.public static int floorDiv(int x, int y)
x
- The dividend.y
- The divisor.ArithmeticException
- if the divisor is zero.public static long floorDiv(long x, int y)
x
- The dividend.y
- The divisor.ArithmeticException
- if the divisor is zero.public static long floorDiv(long x, long y)
x
- The dividend.y
- The divisor.ArithmeticException
- if the divisor is zero.public static int floorMod(int x, int y)
x
- The dividend.y
- The divisor.ArithmeticException
- if the divisor is zero.public static int floorMod(long x, int y)
x
- The dividend.y
- The divisor.ArithmeticException
- if the divisor is zero.public static long floorMod(long x, long y)
x
- The dividend.y
- The divisor.ArithmeticException
- if the divisor is zero.public static int min(int a, int b)
public static long min(long a, long b)
public static int max(int a, int b)
public static long max(long a, long b)
Jas4pp 1.5 © Java Analysis Studio for Particle Physics