Package net.jafama
Class FastMath
java.lang.Object
net.jafama.CmnFastMath
net.jafama.FastMath
Faster (hopefully) versions of java.lang.Math methods, plus additional ones.
Cf. README.txt for more info.
-
Nested Class Summary
Nested classes/interfaces inherited from class net.jafama.CmnFastMath
CmnFastMath.MyTAsin, CmnFastMath.MyTAsinPow, CmnFastMath.MyTAtan, CmnFastMath.MyTCbrt, CmnFastMath.MyTExp, CmnFastMath.MyTLog, CmnFastMath.MyTSinCos, CmnFastMath.MyTSqrt, CmnFastMath.MyTTan, CmnFastMath.MyTTwoPow -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final booleanprivate static final booleanprivate static final booleanprivate static final booleanFields inherited from class net.jafama.CmnFastMath
ANTI_JIT_OPTIM_CRASH_ON_NAN, ANTI_SLOW_CASTS, ASIN_DELTA, ASIN_INDEXER, ASIN_MAX_VALUE_FOR_POWTABS, ASIN_MAX_VALUE_FOR_TABS, ASIN_PIO2_HI, ASIN_PIO2_LO, ASIN_POWTABS_ONE_DIV_MAX_VALUE, ASIN_POWTABS_POWER, ASIN_POWTABS_SIZE, ASIN_POWTABS_SIZE_MINUS_ONE, ASIN_PS0, ASIN_PS1, ASIN_PS2, ASIN_PS3, ASIN_PS4, ASIN_PS5, ASIN_QS1, ASIN_QS2, ASIN_QS3, ASIN_QS4, ASIN_TABS_SIZE, ASINH_ACOSH_SQRT_ELISION_THRESHOLD, ASINH_LOG1P_THRESHOLD, ATAN_AT0, ATAN_AT1, ATAN_AT10, ATAN_AT2, ATAN_AT3, ATAN_AT4, ATAN_AT5, ATAN_AT6, ATAN_AT7, ATAN_AT8, ATAN_AT9, ATAN_DELTA, ATAN_HI3, ATAN_INDEXER, ATAN_LO3, ATAN_MAX_VALUE_FOR_TABS, ATAN_TABS_SIZE, CBRT_LO_BITS, CBRT_LO_TAB_SIZE, DOUBLE_BEFORE_60, DOUBLE_MIN_NORMAL, E, EXP_LO_DISTANCE_TO_ZERO, EXP_LO_DISTANCE_TO_ZERO_POT, EXP_LO_INDEXING, EXP_LO_INDEXING_DIV_SHIFT, EXP_LO_TAB_MID_INDEX, EXP_LO_TAB_SIZE, EXP_LO_TAB_SIZE_POT, EXP_OVERFLOW_LIMIT, EXP_UNDERFLOW_LIMIT, FM_USE_JDK_MATH, FM_USE_POWTABS_FOR_ASIN, FM_USE_REDEFINED_LOG, FM_USE_REDEFINED_SQRT, HYPOT_FACTOR, HYPOT_MAX_MAG, INV_LOG_10, LOG_2, LOG_BITS, LOG_DOUBLE_MAX_VALUE, LOG_TAB_SIZE, LOG_TWO_POW_27, MAX_DOUBLE_EXPONENT, MAX_FLOAT_EXPONENT, MIN_DOUBLE_EXPONENT, MIN_DOUBLE_NORMAL_EXPONENT, MIN_FLOAT_NORMAL_EXPONENT, NORMALIZE_ANGLE_MAX_MEDIUM_DOUBLE_PIO2, ONE_DIV_F2, ONE_DIV_F3, ONE_DIV_F4, PI, PI_HI, PI_INV, PI_LO, PI_SUP, PIO2_HI, PIO2_INV, PIO2_LO, SFM_USE_JDK_MATH, SFM_USE_POWTABS_FOR_ASIN, SFM_USE_REDEFINED_LOG, SFM_USE_REDEFINED_SQRT, SIN_COS_DELTA_HI, SIN_COS_DELTA_LO, SIN_COS_INDEXER, SIN_COS_MAX_VALUE_FOR_INT_MODULO, SIN_COS_TABS_SIZE, SQRT_LO_BITS, SQRT_LO_TAB_SIZE, TAN_DELTA_HI, TAN_DELTA_LO, TAN_INDEXER, TAN_MAX_VALUE_FOR_INT_MODULO, TAN_MAX_VALUE_FOR_TABS, TAN_TABS_SIZE, TAN_VIRTUAL_TABS_SIZE, TANH_1_THRESHOLD, TWO_MATH_PI_IN_MINUS_PI_PI, TWO_POW_23_F, TWO_POW_24, TWO_POW_26, TWO_POW_27, TWO_POW_512, TWO_POW_52, TWO_POW_66, TWO_POW_N26, TWO_POW_N27, TWO_POW_N28, TWO_POW_N512, TWO_POW_N55, TWO_POW_TAB_SIZE, TWOPI_HI, TWOPI_INV, TWOPI_LO, USE_TWO_POW_TAB -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic doubleabs(double a) static floatabs(float a) static doubleacos(double value) static doubleacosh(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 doubleacosh1p(double value) Much more accurate than acosh(1+value), for arguments (and results) close to zero.static doubleacosInRange(double value) If value is not NaN and is outside [-1,1] range, closest value in this range is used.static doubleasin(double value) static doubleasinh(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 doubleasinInRange(double value) If value is not NaN and is outside [-1,1] range, closest value in this range is used.static doubleatan(double value) static doubleatan2(double y, double x) For special values for which multiple conventions could be adopted, behaves like Math.atan2(double,double).static doubleatanh(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 doublecbrt(double value) static doubleceil(double value) static floatceil(float value) static intceilToInt(double value) static doublecopySign(double magnitude, double sign) A sign of NaN can be interpreted as positive or negative.static floatcopySign(float magnitude, float sign) A sign of NaN can be interpreted as positive or negative.static doublecos(double angle) static doublecosh(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 doublecoshm1(double value) Much more accurate than cosh(value)-1, for arguments (and results) close to zero.static doublecosQuick(double angle) Quick cos, with accuracy of about 1.6e-3 (PI/invalid input: '<'look-up tabs size>) for |angle| invalid input: '<' 6588397.0 (Integer.MAX_VALUE * (2*PI/invalid input: '<'look-up tabs size>)), and no accuracy at all for larger values.static doubleexp(double value) static doubleexpm1(double value) Much more accurate than exp(value)-1, for arguments (and results) close to zero.static doubleexpQuick(double value) Quick exp, with a max relative error of about 2.94e-2 for |value| invalid input: '<' 700.0 or so, and no accuracy at all outside this range.static doublefloor(double value) static floatfloor(float value) static intfloorToInt(double value) static intgetExponent(double value) static intgetExponent(float value) static doublehypot(double x, double y) static doublehypot(double x, double y, double z) static doubleIEEEremainder(double f1, double f2) static doubleinvSqrtQuick(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 booleanisInClockwiseDomain(double startAngRad, double angSpanRad, double angRad) NB: Since 2*Math.PI invalid input: '<' 2*PI, a span of 2*Math.PI does not mean full angular range.static booleanisNaNOrInfinite(double value) static booleanisNaNOrInfinite(float value) static doublelog(double value) static doublelog10(double value) static doublelog1p(double value) Much more accurate than log(1+value), for arguments (and results) close to zero.static doublelogQuick(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 doublemax(double a, double b) static floatmax(float a, float b) static doublemin(double a, double b) static floatmin(float a, float b) static doublenextAfter(double start, double direction) If both arguments are +-0.0, direction is returned.static floatnextAfter(float start, double direction) If both arguments are +-0.0(f), (float)direction is returned.static doublenextDown(double start) Semantically equivalent to nextAfter(start,Double.NEGATIVE_INFINITY).static floatnextDown(float start) Semantically equivalent to nextAfter(start,Double.NEGATIVE_INFINITY).static doublenextUp(double start) Semantically equivalent to nextAfter(start,Double.POSITIVE_INFINITY).static floatnextUp(float start) Semantically equivalent to nextAfter(start,Double.POSITIVE_INFINITY).static doublenormalizeMinusHalfPiHalfPi(double angle) static doublenormalizeMinusHalfPiHalfPiFast(double angle) Not accurate for large values.static doublenormalizeMinusPiPi(double angle) static doublenormalizeMinusPiPiFast(double angle) Not accurate for large values.static doublenormalizeZeroTwoPi(double angle) static doublenormalizeZeroTwoPiFast(double angle) Not accurate for large values.static doublepow(double value, double power) 1e-13ish accuracy or better on whole double range.static doublepow2(double value) static floatpow2(float value) static doublepow3(double value) static floatpow3(float value) static doublepowFast(double value, int power) This treatment is somehow accurate for low values of |power|, and for |power*getExponent(value)| invalid input: '<' 1023 or so (to stay away from double extreme magnitudes (large and small)).static doublepowQuick(double value, double power) Quick pow, with a max relative error of about 1e-2 for value >= Double.MIN_NORMAL and 1e-10 invalid input: '<' |value^power| invalid input: '<' 1e10, of about 6e-2 for value >= Double.MIN_NORMAL and 1e-40 invalid input: '<' |value^power| invalid input: '<' 1e40, and worse accuracy otherwise.static doublerandom()static doubleremainder(double dividend, double divisor) Returns dividend - divisor * n, where n is the mathematical integer closest to dividend/divisor.private static doubleremainderPi(double angle) private static doubleremainderPiFast(double angle) Not accurate for large values.private static longremainderPiO2(double angle) private static doubleremainderTwoPi(double angle) private static doubleremainderTwoPiFast(double angle) Not accurate for large values.static doublerint(double value) static floatrint(float value) static longround(double value) Might have different semantics than Math.round(double), see bugs 6430675 and 8010430.static intround(float value) Might have different semantics than Math.round(float), see bugs 6430675 and 8010430.static longroundEven(double value) static introundEven(float value) static introundEvenToInt(double value) static introundToInt(double value) static doublescalb(double value, int scaleFactor) Precision may be lost if the result is subnormal.static floatscalb(float value, int scaleFactor) Precision may be lost if the result is subnormal.static longsignFromBit(double value) static intsignFromBit(float value) static doublesignum(double value) static floatsignum(float value) static doublesin(double angle) static doublesinAndCos(double angle, DoubleWrapper cosine) Computes sine and cosine together.static doublesinh(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 doublesinhAndCosh(double value, DoubleWrapper hcosine) Computes hyperbolic sine and hyperbolic cosine together.static doublesinQuick(double angle) Quick sin, with accuracy of about 1.6e-3 (PI/invalid input: '<'look-up tabs size>) for |angle| invalid input: '<' 6588395.0 (Integer.MAX_VALUE * (2*PI/invalid input: '<'look-up tabs size>) - 2) (- 2 due to removing PI/2 before using cosine tab), and no accuracy at all for larger values.static doublesqrt(double value) static doublesqrtQuick(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 doubletan(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 doubletanh(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 doubletoDegrees(boolean sign, int degrees, int minutes, double seconds) static doubletoDegrees(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 booleantoDMS(double angrad, IntWrapper degrees, IntWrapper minutes, DoubleWrapper seconds) static doubletoRadians(boolean sign, int degrees, int minutes, double seconds) static doubletoRadians(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 doubletoRange(double min, double max, double value) static floattoRange(float min, float max, float value) static doubleulp(double value) The ULP (Unit in the Last Place) is the distance to the next value larger in magnitude.static floatulp(float value) The ULP (Unit in the Last Place) is the distance to the next value larger in magnitude.Methods inherited from class net.jafama.CmnFastMath
abs, abs, addBounded, addBounded, addExact, addExact, atan2_ninf_yyy, atan2_pinf_yyy, atan2_yyy_zeroOrNaN, decodeQuadrant, decodeRemainder, decrementBounded, decrementBounded, decrementExact, decrementExact, encodeRemainderAndQuadrant, floorDiv, floorDiv, floorDiv, floorMod, floorMod, floorMod, heavyRemainderPi, heavyRemainderPiO2, heavyRemainderTwoPi, hypot_NaN, hypot_NaN, incrementBounded, incrementBounded, incrementExact, incrementExact, initTables, jdkRemainderPi, jdkRemainderPiO2, jdkRemainderTwoPi, log2, log2, max, max, min, min, multiplyBounded, multiplyBounded, multiplyBounded, multiplyExact, multiplyExact, multiplyExact, multiplyFull, multiplyHigh, negateBounded, negateBounded, negateExact, negateExact, pow2, pow2, pow3, pow3, subtractBounded, subtractBounded, subtractExact, subtractExact, toInt, toIntExact, toRange, toRange, twoPow, twoPowNormal, twoPowNormalOrSubnormal
-
Field Details
-
USE_JDK_MATH
private static final boolean USE_JDK_MATH -
USE_REDEFINED_LOG
private static final boolean USE_REDEFINED_LOG -
USE_REDEFINED_SQRT
private static final boolean USE_REDEFINED_SQRT -
USE_POWTABS_FOR_ASIN
private static final boolean USE_POWTABS_FOR_ASIN- See Also:
-
-
Constructor Details
-
FastMath
private FastMath()Non-instantiable.
-
-
Method Details
-
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/invalid input: '<'look-up tabs size>) for |angle| invalid input: '<' 6588395.0 (Integer.MAX_VALUE * (2*PI/invalid input: '<'look-up tabs size>) - 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/invalid input: '<'look-up tabs size>) for |angle| invalid input: '<' 6588397.0 (Integer.MAX_VALUE * (2*PI/invalid input: '<'look-up tabs size>)), and no accuracy at all for larger values.- Parameters:
angle- Angle in radians.- Returns:
- Angle cosine.
-
sinAndCos
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 invalid input: '<' 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
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| invalid input: '<' 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 invalid input: '<' |value^power| invalid input: '<' 1e10, of about 6e-2 for value >= Double.MIN_NORMAL and 1e-40 invalid input: '<' |value^power| invalid input: '<' 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)| invalid input: '<' 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 invalid input: '<' 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 invalid input: '<' 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 invalid input: '<' 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 invalid input: '<' 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() -
remainderTwoPi
private static double remainderTwoPi(double angle) - Parameters:
angle- Angle in radians.- Returns:
- Remainder of (angle % (2*PI)), in [-PI,PI].
-
remainderPi
private static double remainderPi(double angle) - Parameters:
angle- Angle in radians.- Returns:
- Remainder of (angle % PI), in [-PI/2,PI/2].
-
remainderPiO2
private static long remainderPiO2(double angle) - Parameters:
angle- Angle in radians.- Returns:
- Bits of double corresponding to remainder of (angle % (PI/2)), in [-PI/4,PI/4], with quadrant encoded in exponent bits.
-
remainderTwoPiFast
private static double remainderTwoPiFast(double angle) Not accurate for large values.- Parameters:
angle- Angle in radians.- Returns:
- Remainder of (angle % (2*PI)), in [-PI,PI].
-
remainderPiFast
private static double remainderPiFast(double angle) Not accurate for large values.- Parameters:
angle- Angle in radians.- Returns:
- Remainder of (angle % PI), in [-PI/2,PI/2].
-