Package net.jafama
Class CmnFastMath
java.lang.Object
net.jafama.CmnFastMath
- Direct Known Subclasses:
FastMath
,StrictFastMath
Stuffs for FastMath and StrictFastMath.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final class
(package private) static final class
(package private) static final class
(package private) static final class
(package private) static final class
(package private) static final class
(package private) static final class
(package private) static final class
(package private) static final class
(package private) static final class
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final boolean
If some methods get JIT-optimized, they might crash if they contain "(var == xxx)" with var being NaN (can happen with Java 6u29).(package private) static final boolean
Because on some architectures, some casts can be slow, especially for large values.(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
Supposed to be >= sin(77.2deg), as fdlibm code is supposed to work with values > 0.975, but seems to work well enough as long as value >= sin(25deg).(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final int
(package private) static final double
sqrt(x*x+-1) should yield higher threshold, but it's enough due to subsequent log.(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
Supposed to be >= tan(67.7deg), as fdlibm code is supposed to work with values > 2.4375.(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final double
(package private) static final double
Double.MIN_NORMAL since Java 6.static final double
Closest double approximation of e.(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final double
(package private) static final double
(package private) static final boolean
(package private) static final boolean
Set it to true if FastMath.sqrt(double) is slow (more tables, but less calls to FastMath.sqrt(double)).(package private) static final boolean
Used for both FastMath.log(double) and FastMath.log10(double).(package private) static final boolean
(package private) static final double
Large enough to get a value's magnitude back into [2^-511,2^511] from Double.MIN_VALUE or Double.MAX_VALUE, and small enough not to get it across that range (considering a 2*53 tolerance due to only checking magnitude of min/max value, and scaling all values together).(package private) static final double
For using sqrt, to avoid overflow/underflow, we want values magnitude in [1/sqrt(Double.MAX_VALUE/n),sqrt(Double.MAX_VALUE/n)], n being the number of arguments.(package private) static final double
(package private) static final double
(package private) static final int
(package private) static final double
(package private) static final int
(package private) static final double
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final int
(package private) static final double
fdlibm uses 2^19*PI/2 here.(package private) static final double
(package private) static final double
(package private) static final double
static final double
Closest double approximation of pi, which is inferior to mathematical pi: pi ~= 3.14159265358979323846...(package private) static final double
(package private) static final double
(package private) static final double
static final double
High double approximation of pi, which is further from pi than the low approximation PI: pi ~= 3.14159265358979323846...(package private) static final double
(package private) static final double
(package private) static final double
private static final double
private static final double
private static final double
private static final double
private static final double
private static final double
private static final long
Bit = 0 where quadrant is encoded in remainder bits.private static final long
Remainder bits where quadrant is encoded, 0 elsewhere.(package private) static final boolean
(package private) static final boolean
Set it to true if StrictFastMath.sqrt(double) is slow (more tables, but less calls to StrictFastMath.sqrt(double)).(package private) static final boolean
Used for both StrictFastMath.log(double) and StrictFastMath.log10(double).(package private) static final boolean
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
Max abs value for index-based reduction, above which we use regular angle normalization.(package private) static final int
private static final double
(package private) static final int
(package private) static final int
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
Max abs value for fast modulo, above which we use regular angle normalization.(package private) static final double
Must be >= 45deg, and supposed to be >= 51.4deg, as fdlibm code is not supposed to work with values inferior to that (51.4deg is about (PI/2-Double.longBitsToDouble(0x3FE5942800000000L))).(package private) static final int
(package private) static final int
We use indexing past look-up tables, so that indexing information allows for fast recomputation of angle in [0,PI/2] range.(package private) static final double
Constant found experimentally: StrictMath.tanh(TANH_1_THRESHOLD) = 1, StrictMath.tanh(nextDown(TANH_1_THRESHOLD)) = FastMath.tanh(nextDown(TANH_1_THRESHOLD)) < 1.(package private) static final double
2*Math.PI, normalized into [-PI,PI], as returned by StrictMath.asin(StrictMath.sin(2*Math.PI)) (asin behaves as identity for this).private static final double[]
Table of constants for 1/(PI/2), 282 Hex digits (enough for normalizing doubles).(package private) static final float
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
private static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final int
(package private) static final double
(package private) static final double
(package private) static final double
(package private) static final boolean
Using two pow tab can just make things barely faster, and could relatively hurt in case of cache-misses, especially for methods that otherwise wouldn't rely on any tab, so we don't use it. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic int
abs
(int value) static long
abs
(long value) 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) (package private) static double
atan2_ninf_yyy
(double y) (package private) static double
atan2_pinf_yyy
(double y) (package private) static double
atan2_yyy_zeroOrNaN
(double y, double x) (package private) static int
decodeQuadrant
(long bits) (package private) static double
decodeRemainder
(long bits) static int
decrementBounded
(int value) static long
decrementBounded
(long value) static int
decrementExact
(int value) static long
decrementExact
(long value) (package private) static long
encodeRemainderAndQuadrant
(double remainder, int quadrant) 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)[.private static boolean
getBooleanProperty
(String key, boolean defaultValue) private static int
getTabSizePower
(int tabSizePower) Use look-up tables size power through this method, to make sure is it small in case java.lang.Math is directly used.(package private) static double
heavyRemainderPi
(double angle) (package private) static long
heavyRemainderPiO2
(double angle, boolean negateRem) Remainder using an accurate definition of PI.(package private) static double
heavyRemainderTwoPi
(double angle) (package private) static double
hypot_NaN
(double xAbs, double yAbs) At least one of the arguments must be NaN.(package private) static double
hypot_NaN
(double xAbs, double yAbs, double zAbs) At least one of the arguments must be NaN.static int
incrementBounded
(int value) static long
incrementBounded
(long value) static int
incrementExact
(int value) static long
incrementExact
(long value) static void
Ensures that all look-up tables are initialized - otherwise they are initialized lazily.(package private) static double
jdkRemainderPi
(double angle) (package private) static long
jdkRemainderPiO2
(double angle, boolean negateRem) (package private) static double
jdkRemainderTwoPi
(double angle) static int
log2
(int value) static int
log2
(long value) static int
max
(int a, int b) static long
max
(long a, long 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 int
pow2
(int value) static long
pow2
(long value) static int
pow3
(int value) static long
pow3
(long value) private static long
signFromBit_antiCyclic
(double value) Redefined here, to avoid cyclic dependency with (Strict)FastMath.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 int
toInt
(long value) static int
toIntExact
(long 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.(package private) static double
twoPowNormal
(int power) (package private) static double
twoPowNormalOrSubnormal
(int power)
-
Field Details
-
FM_USE_JDK_MATH
static final boolean FM_USE_JDK_MATH -
FM_USE_REDEFINED_LOG
static final boolean FM_USE_REDEFINED_LOGUsed for both FastMath.log(double) and FastMath.log10(double). -
FM_USE_REDEFINED_SQRT
static final boolean FM_USE_REDEFINED_SQRT -
FM_USE_POWTABS_FOR_ASIN
static final boolean FM_USE_POWTABS_FOR_ASINSet it to true if FastMath.sqrt(double) is slow (more tables, but less calls to FastMath.sqrt(double)).- See Also:
-
SFM_USE_JDK_MATH
static final boolean SFM_USE_JDK_MATH -
SFM_USE_REDEFINED_LOG
static final boolean SFM_USE_REDEFINED_LOGUsed for both StrictFastMath.log(double) and StrictFastMath.log10(double). True by default because the StrictMath implementations can be slow. -
SFM_USE_REDEFINED_SQRT
static final boolean SFM_USE_REDEFINED_SQRT -
SFM_USE_POWTABS_FOR_ASIN
static final boolean SFM_USE_POWTABS_FOR_ASINSet it to true if StrictFastMath.sqrt(double) is slow (more tables, but less calls to StrictFastMath.sqrt(double)).- See Also:
-
USE_TWO_POW_TAB
static final boolean USE_TWO_POW_TABUsing two pow tab can just make things barely faster, and could relatively hurt in case of cache-misses, especially for methods that otherwise wouldn't rely on any tab, so we don't use it.- See Also:
-
ANTI_SLOW_CASTS
static final boolean ANTI_SLOW_CASTSBecause on some architectures, some casts can be slow, especially for large values. Might make things a bit slower for latest architectures, but not as much as it makes them faster for older ones.- See Also:
-
ANTI_JIT_OPTIM_CRASH_ON_NAN
static final boolean ANTI_JIT_OPTIM_CRASH_ON_NANIf some methods get JIT-optimized, they might crash if they contain "(var == xxx)" with var being NaN (can happen with Java 6u29). The crash does not happen if we replace "==" with "<" or ">". Only the code that has been observed to trigger the bug has been modified.- See Also:
-
E
public static final double EClosest double approximation of e.- See Also:
-
PI
public static final double PIClosest double approximation of pi, which is inferior to mathematical pi: pi ~= 3.14159265358979323846... PI ~= 3.141592653589793- See Also:
-
PI_SUP
public static final double PI_SUPHigh double approximation of pi, which is further from pi than the low approximation PI: pi ~= 3.14159265358979323846... PI ~= 3.141592653589793 PI_SUP ~= 3.1415926535897936 -
ONE_DIV_F2
static final double ONE_DIV_F2- See Also:
-
ONE_DIV_F3
static final double ONE_DIV_F3- See Also:
-
ONE_DIV_F4
static final double ONE_DIV_F4- See Also:
-
TWO_POW_23_F
static final float TWO_POW_23_F -
TWO_POW_24
static final double TWO_POW_24 -
TWO_POW_N24
private static final double TWO_POW_N24 -
TWO_POW_26
static final double TWO_POW_26 -
TWO_POW_N26
static final double TWO_POW_N26 -
TWO_POW_27
static final double TWO_POW_27 -
TWO_POW_N27
static final double TWO_POW_N27 -
TWO_POW_N28
static final double TWO_POW_N28 -
TWO_POW_52
static final double TWO_POW_52 -
TWO_POW_N55
static final double TWO_POW_N55 -
TWO_POW_66
static final double TWO_POW_66 -
TWO_POW_512
static final double TWO_POW_512 -
TWO_POW_N512
static final double TWO_POW_N512 -
DOUBLE_MIN_NORMAL
static final double DOUBLE_MIN_NORMALDouble.MIN_NORMAL since Java 6. -
MIN_DOUBLE_EXPONENT
static final int MIN_DOUBLE_EXPONENT- See Also:
-
MIN_DOUBLE_NORMAL_EXPONENT
static final int MIN_DOUBLE_NORMAL_EXPONENT- See Also:
-
MAX_DOUBLE_EXPONENT
static final int MAX_DOUBLE_EXPONENT- See Also:
-
MIN_FLOAT_NORMAL_EXPONENT
static final int MIN_FLOAT_NORMAL_EXPONENT- See Also:
-
MAX_FLOAT_EXPONENT
static final int MAX_FLOAT_EXPONENT- See Also:
-
SQRT_2
private static final double SQRT_2 -
LOG_2
static final double LOG_2 -
LOG_TWO_POW_27
static final double LOG_TWO_POW_27 -
LOG_DOUBLE_MAX_VALUE
static final double LOG_DOUBLE_MAX_VALUE -
INV_LOG_10
static final double INV_LOG_10 -
DOUBLE_BEFORE_60
static final double DOUBLE_BEFORE_60 -
TWO_OVER_PI_TAB
private static final double[] TWO_OVER_PI_TABTable of constants for 1/(PI/2), 282 Hex digits (enough for normalizing doubles). 1/(PI/2) approximation = sum of TWO_OVER_PI_TAB[i]*2^(-24*(i+1)). double and not int, to avoid int-to-double cast during computations. -
PIO2_TAB0
private static final double PIO2_TAB0 -
PIO2_TAB1
private static final double PIO2_TAB1 -
PIO2_TAB2
private static final double PIO2_TAB2 -
PIO2_TAB3
private static final double PIO2_TAB3 -
PIO2_TAB4
private static final double PIO2_TAB4 -
PIO2_TAB5
private static final double PIO2_TAB5 -
PIO2_INV
static final double PIO2_INV -
PIO2_HI
static final double PIO2_HI -
PIO2_LO
static final double PIO2_LO -
PI_INV
static final double PI_INV -
PI_HI
static final double PI_HI -
PI_LO
static final double PI_LO -
TWOPI_INV
static final double TWOPI_INV -
TWOPI_HI
static final double TWOPI_HI -
TWOPI_LO
static final double TWOPI_LO -
QUADRANT_BITS_0_MASK
private static final long QUADRANT_BITS_0_MASKBit = 0 where quadrant is encoded in remainder bits.- See Also:
-
QUADRANT_PLACE_BITS
private static final long QUADRANT_PLACE_BITSRemainder bits where quadrant is encoded, 0 elsewhere.- See Also:
-
NORMALIZE_ANGLE_MAX_MEDIUM_DOUBLE_PIO2
static final double NORMALIZE_ANGLE_MAX_MEDIUM_DOUBLE_PIO2fdlibm uses 2^19*PI/2 here. With 2^18*PI/2 we would be more accurate, for example when normalizing 822245.903631403, which is close to 2^19*PI/2, but we are still in our accuracy tolerance with fdlibm's value (but not 2^20*PI/2) so we stick to it, to help being faster than (Strict)Math for values in [2^18*PI/2,2^19*PI/2]. For tests, can use a smaller value, for heavy remainder not to only be used with huge values. -
TWO_MATH_PI_IN_MINUS_PI_PI
static final double TWO_MATH_PI_IN_MINUS_PI_PI2*Math.PI, normalized into [-PI,PI], as returned by StrictMath.asin(StrictMath.sin(2*Math.PI)) (asin behaves as identity for this). NB: NumbersUtils.minus2PI(2*Math.PI) returns -2.449293598153844E-16, which is different due to not using an accurate enough definition of PI.- See Also:
-
SIN_COS_TABS_SIZE
static final int SIN_COS_TABS_SIZE -
SIN_COS_DELTA_HI
static final double SIN_COS_DELTA_HI -
SIN_COS_DELTA_LO
static final double SIN_COS_DELTA_LO -
SIN_COS_INDEXER
static final double SIN_COS_INDEXER -
SIN_COS_MAX_VALUE_FOR_INT_MODULO
static final double SIN_COS_MAX_VALUE_FOR_INT_MODULOMax abs value for index-based reduction, above which we use regular angle normalization. This value must be < (Integer.MAX_VALUE / SIN_COS_INDEXER), to stay in range of int type. If too high, error gets larger because index-based reduction doesn't use an accurate enough definition of PI. If too low, and if we would be using remainder into [-PI,PI] instead of into [-PI/4,PI/4], error would get larger as well, because remainder would just provide a double, while index-based reduction is more accurate, using delta from index values and HI/LO values. -
TAN_VIRTUAL_TABS_SIZE
static final int TAN_VIRTUAL_TABS_SIZEWe use indexing past look-up tables, so that indexing information allows for fast recomputation of angle in [0,PI/2] range. -
TAN_MAX_VALUE_FOR_TABS
static final double TAN_MAX_VALUE_FOR_TABSMust be >= 45deg, and supposed to be >= 51.4deg, as fdlibm code is not supposed to work with values inferior to that (51.4deg is about (PI/2-Double.longBitsToDouble(0x3FE5942800000000L))). -
TAN_TABS_SIZE
static final int TAN_TABS_SIZE -
TAN_DELTA_HI
static final double TAN_DELTA_HI -
TAN_DELTA_LO
static final double TAN_DELTA_LO -
TAN_INDEXER
static final double TAN_INDEXER -
TAN_MAX_VALUE_FOR_INT_MODULO
static final double TAN_MAX_VALUE_FOR_INT_MODULOMax abs value for fast modulo, above which we use regular angle normalization. This value must be < (Integer.MAX_VALUE / TAN_INDEXER), to stay in range of int type. If too high, error gets larger because index-based reduction doesn't use an accurate enough definition of PI. If too low, error gets larger as well, because we use remainder into [-PI/2,PI/2], just provides a double, while index-based reduction is more accurate, using delta from index values and HI/LO values. -
ASIN_MAX_VALUE_FOR_TABS
static final double ASIN_MAX_VALUE_FOR_TABSSupposed to be >= sin(77.2deg), as fdlibm code is supposed to work with values > 0.975, but seems to work well enough as long as value >= sin(25deg). -
ASIN_TABS_SIZE
static final int ASIN_TABS_SIZE -
ASIN_DELTA
static final double ASIN_DELTA -
ASIN_INDEXER
static final double ASIN_INDEXER -
ASIN_MAX_VALUE_FOR_POWTABS
static final double ASIN_MAX_VALUE_FOR_POWTABS -
ASIN_POWTABS_POWER
static final int ASIN_POWTABS_POWER- See Also:
-
ASIN_POWTABS_ONE_DIV_MAX_VALUE
static final double ASIN_POWTABS_ONE_DIV_MAX_VALUE -
ASIN_POWTABS_SIZE
static final int ASIN_POWTABS_SIZE -
ASIN_POWTABS_SIZE_MINUS_ONE
static final int ASIN_POWTABS_SIZE_MINUS_ONE -
ASIN_PIO2_HI
static final double ASIN_PIO2_HI -
ASIN_PIO2_LO
static final double ASIN_PIO2_LO -
ASIN_PS0
static final double ASIN_PS0 -
ASIN_PS1
static final double ASIN_PS1 -
ASIN_PS2
static final double ASIN_PS2 -
ASIN_PS3
static final double ASIN_PS3 -
ASIN_PS4
static final double ASIN_PS4 -
ASIN_PS5
static final double ASIN_PS5 -
ASIN_QS1
static final double ASIN_QS1 -
ASIN_QS2
static final double ASIN_QS2 -
ASIN_QS3
static final double ASIN_QS3 -
ASIN_QS4
static final double ASIN_QS4 -
ATAN_MAX_VALUE_FOR_TABS
static final double ATAN_MAX_VALUE_FOR_TABSSupposed to be >= tan(67.7deg), as fdlibm code is supposed to work with values > 2.4375. -
ATAN_TABS_SIZE
static final int ATAN_TABS_SIZE -
ATAN_DELTA
static final double ATAN_DELTA -
ATAN_INDEXER
static final double ATAN_INDEXER -
ATAN_HI3
static final double ATAN_HI3 -
ATAN_LO3
static final double ATAN_LO3 -
ATAN_AT0
static final double ATAN_AT0 -
ATAN_AT1
static final double ATAN_AT1 -
ATAN_AT2
static final double ATAN_AT2 -
ATAN_AT3
static final double ATAN_AT3 -
ATAN_AT4
static final double ATAN_AT4 -
ATAN_AT5
static final double ATAN_AT5 -
ATAN_AT6
static final double ATAN_AT6 -
ATAN_AT7
static final double ATAN_AT7 -
ATAN_AT8
static final double ATAN_AT8 -
ATAN_AT9
static final double ATAN_AT9 -
ATAN_AT10
static final double ATAN_AT10 -
TANH_1_THRESHOLD
static final double TANH_1_THRESHOLDConstant found experimentally: StrictMath.tanh(TANH_1_THRESHOLD) = 1, StrictMath.tanh(nextDown(TANH_1_THRESHOLD)) = FastMath.tanh(nextDown(TANH_1_THRESHOLD)) < 1.- See Also:
-
ASINH_LOG1P_THRESHOLD
static final double ASINH_LOG1P_THRESHOLD- See Also:
-
ASINH_ACOSH_SQRT_ELISION_THRESHOLD
static final double ASINH_ACOSH_SQRT_ELISION_THRESHOLDsqrt(x*x+-1) should yield higher threshold, but it's enough due to subsequent log.- See Also:
-
EXP_OVERFLOW_LIMIT
static final double EXP_OVERFLOW_LIMIT -
EXP_UNDERFLOW_LIMIT
static final double EXP_UNDERFLOW_LIMIT -
EXP_LO_DISTANCE_TO_ZERO_POT
static final int EXP_LO_DISTANCE_TO_ZERO_POT- See Also:
-
EXP_LO_DISTANCE_TO_ZERO
static final int EXP_LO_DISTANCE_TO_ZERO- See Also:
-
EXP_LO_TAB_SIZE_POT
static final int EXP_LO_TAB_SIZE_POT -
EXP_LO_TAB_SIZE
static final int EXP_LO_TAB_SIZE -
EXP_LO_TAB_MID_INDEX
static final int EXP_LO_TAB_MID_INDEX -
EXP_LO_INDEXING
static final int EXP_LO_INDEXING -
EXP_LO_INDEXING_DIV_SHIFT
static final int EXP_LO_INDEXING_DIV_SHIFT -
LOG_BITS
static final int LOG_BITS -
LOG_TAB_SIZE
static final int LOG_TAB_SIZE -
TWO_POW_TAB_SIZE
static final int TWO_POW_TAB_SIZE- See Also:
-
SQRT_LO_BITS
static final int SQRT_LO_BITS -
SQRT_LO_TAB_SIZE
static final int SQRT_LO_TAB_SIZE -
CBRT_LO_BITS
static final int CBRT_LO_BITS -
CBRT_LO_TAB_SIZE
static final int CBRT_LO_TAB_SIZE -
HYPOT_MAX_MAG
static final double HYPOT_MAX_MAGFor using sqrt, to avoid overflow/underflow, we want values magnitude in [1/sqrt(Double.MAX_VALUE/n),sqrt(Double.MAX_VALUE/n)], n being the number of arguments. sqrt(Double.MAX_VALUE/2) = 9.480751908109176E153 and sqrt(Double.MAX_VALUE/3) = 7.741001517595157E153 so 2^511 = 6.7039039649712985E153 works for both. -
HYPOT_FACTOR
static final double HYPOT_FACTORLarge enough to get a value's magnitude back into [2^-511,2^511] from Double.MIN_VALUE or Double.MAX_VALUE, and small enough not to get it across that range (considering a 2*53 tolerance due to only checking magnitude of min/max value, and scaling all values together).
-
-
Constructor Details
-
CmnFastMath
CmnFastMath()
-
-
Method Details
-
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) -
twoPowNormal
static double twoPowNormal(int power) - Parameters:
power
- Must be in normal values range.
-
twoPowNormalOrSubnormal
static double twoPowNormalOrSubnormal(int power) - Parameters:
power
- Must be in normal or subnormal values range.
-
atan2_pinf_yyy
static double atan2_pinf_yyy(double y) -
atan2_ninf_yyy
static double atan2_ninf_yyy(double y) -
atan2_yyy_zeroOrNaN
static double atan2_yyy_zeroOrNaN(double y, double x) -
hypot_NaN
static double hypot_NaN(double xAbs, double yAbs) At least one of the arguments must be NaN. -
hypot_NaN
static double hypot_NaN(double xAbs, double yAbs, double zAbs) At least one of the arguments must be NaN. -
encodeRemainderAndQuadrant
static long encodeRemainderAndQuadrant(double remainder, int quadrant) - Parameters:
remainder
- Must have 1 for 2nd and 3rd exponent bits, which is the case for heavyRemPiO2 remainders (their absolute values are >= Double.longBitsToDouble(0x3000000000000000L) = 1.727233711018889E-77, and even if they were not, turning these bits from 0 to 1 on decoding would not change the absolute error much), and also works for +-Infinity or NaN encoding.quadrant
- Must be in [0,3].- Returns:
- Bits holding remainder, and quadrant instead of reamainder's 2nd and 3rd exponent bits.
-
decodeRemainder
static double decodeRemainder(long bits) -
decodeQuadrant
static int decodeQuadrant(long bits) -
jdkRemainderTwoPi
static double jdkRemainderTwoPi(double angle) - Parameters:
angle
- Angle, in radians.- Returns:
- Remainder of (angle % (2*PI)), in [-PI,PI].
-
jdkRemainderPi
static double jdkRemainderPi(double angle) - Parameters:
angle
- Angle, in radians.- Returns:
- Remainder of (angle % PI), in [-PI/2,PI/2].
-
jdkRemainderPiO2
static long jdkRemainderPiO2(double angle, boolean negateRem) - 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.
-
heavyRemainderTwoPi
static double heavyRemainderTwoPi(double angle) - Parameters:
angle
- Angle, in radians. Must not be NaN nor +-Infinity.- Returns:
- Remainder of (angle % (2*PI)), in [-PI,PI].
-
heavyRemainderPi
static double heavyRemainderPi(double angle) - Parameters:
angle
- Angle, in radians. Must not be NaN nor +-Infinity.- Returns:
- Remainder of (angle % PI), in [-PI/2,PI/2].
-
heavyRemainderPiO2
static long heavyRemainderPiO2(double angle, boolean negateRem) Remainder using an accurate definition of PI. Derived from a fdlibm treatment called __kernel_rem_pio2. Not defining a non-strictfp version for FastMath, to avoid duplicating its long and messy code, and because it's slow anyway, and should be rarely used when speed matters.- Parameters:
angle
- Angle, in radians. Must not be NaN nor +-Infinity.negateRem
- True if remainder must be negated before encoded into returned long.- Returns:
- Bits of double corresponding to remainder of (angle % (PI/2)), in [-PI/4,PI/4], with quadrant encoded in exponent bits.
-
signFromBit_antiCyclic
private static long signFromBit_antiCyclic(double value) Redefined here, to avoid cyclic dependency with (Strict)FastMath.- Parameters:
value
- A double value.- Returns:
- -1 if sign bit is 1, 1 if sign bit is 0.
-
getBooleanProperty
-
getTabSizePower
private static int getTabSizePower(int tabSizePower) Use look-up tables size power through this method, to make sure is it small in case java.lang.Math is directly used.
-