Class IntMath
- java.lang.Object
-
- org.apache.commons.statistics.descriptive.IntMath
-
final class IntMath extends java.lang.Object
Support class for integer math.- Since:
- 1.1
-
-
Field Summary
Fields Modifier and Type Field Description private static int
EXP_BIAS
Bias offset for the exponent of a double.private static int
EXP_SHIFT
Shift for the exponent of a double.private static double
HALF
0.5.private static long
MASK32
Mask for the lower 32-bits of a long.private static long
MASK52
Mask for the lower 52-bits of a long.private static long
TWO_POW_53
2^53.
-
Constructor Summary
Constructors Modifier Constructor Description private
IntMath()
No instances.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description (package private) static double
divide(Int128 x, long n)
Divide valuex
by the countn
.private static double
roundToInteger(double x)
Get the whole number that is the nearest to x, with ties rounding towards positive infinity.(package private) static long
squareHigh(long x)
Square the values as if an unsigned 64-bit long to produce the high 64-bits of the 128-bit unsigned result.(package private) static java.math.BigInteger
toBigIntegerExact(double x)
Return the whole number that is nearest to thedouble
argumentx
as anint
, with ties rounding towards positive infinity.(package private) static int
toIntExact(double x)
Return the whole number that is nearest to thedouble
argumentx
as anint
, with ties rounding towards positive infinity.(package private) static long
toLongExact(double x)
Return the whole number that is nearest to thedouble
argumentx
as anlong
, with ties rounding towards positive infinity.(package private) static double
uint128ToDouble(long hi, long lo)
Convert an unsigned 128-bit integer to adouble
.(package private) static long
unsignedMultiplyHigh(long value1, long value2)
Multiply the two values as if unsigned 64-bit longs to produce the high 64-bits of the 128-bit unsigned result.(package private) static double
unsignedMultiplyToDouble(long x, long y)
Multiply the arguments as if unsigned integers to adouble
result.
-
-
-
Field Detail
-
MASK32
private static final long MASK32
Mask for the lower 32-bits of a long.- See Also:
- Constant Field Values
-
MASK52
private static final long MASK52
Mask for the lower 52-bits of a long.- See Also:
- Constant Field Values
-
EXP_BIAS
private static final int EXP_BIAS
Bias offset for the exponent of a double.- See Also:
- Constant Field Values
-
EXP_SHIFT
private static final int EXP_SHIFT
Shift for the exponent of a double.- See Also:
- Constant Field Values
-
HALF
private static final double HALF
0.5.- See Also:
- Constant Field Values
-
TWO_POW_53
private static final long TWO_POW_53
2^53.- See Also:
- Constant Field Values
-
-
Method Detail
-
squareHigh
static long squareHigh(long x)
Square the values as if an unsigned 64-bit long to produce the high 64-bits of the 128-bit unsigned result. aThis method computes the equivalent of:
Math.multiplyHigh(x, x) Math.unsignedMultiplyHigh(x, x) - (((x >> 63) & x) << 1)
Note: The method
Math.multiplyHigh
was added in JDK 9 and should be used as above when the source code targets Java 11 to exploit the intrinsic method.Note: The method uses the unsigned multiplication. When the input is negative it can be adjusted to the signed result by subtracting the argument twice from the result.
- Parameters:
x
- Value- Returns:
- the high 64-bits of the 128-bit result
-
unsignedMultiplyHigh
static long unsignedMultiplyHigh(long value1, long value2)
Multiply the two values as if unsigned 64-bit longs to produce the high 64-bits of the 128-bit unsigned result.This method computes the equivalent of:
Math.multiplyHigh(a, b) + ((a >> 63) & b) + ((b >> 63) & a)
Note: The method
Math.multiplyHigh
was added in JDK 9 and should be used as above when the source code targets Java 11 to exploit the intrinsic method.Note: The method
Math.unsignedMultiplyHigh
was added in JDK 18 and should be used when the source code target allows.Taken from
o.a.c.rng.core.source64.LXMSupport
.- Parameters:
value1
- the first valuevalue2
- the second value- Returns:
- the high 64-bits of the 128-bit result
-
unsignedMultiplyToDouble
static double unsignedMultiplyToDouble(long x, long y)
Multiply the arguments as if unsigned integers to adouble
result.- Parameters:
x
- Value.y
- Value.- Returns:
- the double
-
uint128ToDouble
static double uint128ToDouble(long hi, long lo)
Convert an unsigned 128-bit integer to adouble
.- Parameters:
hi
- High 64-bits.lo
- Low 64-bits.- Returns:
- the double
-
toIntExact
static int toIntExact(double x)
Return the whole number that is nearest to thedouble
argumentx
as anint
, with ties rounding towards positive infinity.This will raise an
ArithmeticException
if the closest integer result is not within the range[-2^31, 2^31)
, i.e. it overflows anint
; or the argumentx
is not finite.Note: This method is equivalent to:
Math.toIntExact(Math.round(x))
The behaviour has been re-implemented for consistent error handling for
int
,long
andBigInteger
types.- Parameters:
x
- Value.- Returns:
- rounded value
- Throws:
java.lang.ArithmeticException
- if theresult
overflows anint
, orx
is not finite- See Also:
Math.round(double)
,Math.toIntExact(long)
-
toLongExact
static long toLongExact(double x)
Return the whole number that is nearest to thedouble
argumentx
as anlong
, with ties rounding towards positive infinity.This will raise an
ArithmeticException
if the closest integer result is not within the range[-2^63, 2^63)
, i.e. it overflows along
; or the argumentx
is not finite.- Parameters:
x
- Value.- Returns:
- rounded value
- Throws:
java.lang.ArithmeticException
- if theresult
overflows along
, orx
is not finite
-
toBigIntegerExact
static java.math.BigInteger toBigIntegerExact(double x)
Return the whole number that is nearest to thedouble
argumentx
as anint
, with ties rounding towards positive infinity.This will raise an
ArithmeticException
if the argumentx
is not finite.- Parameters:
x
- Value.- Returns:
- rounded value
- Throws:
java.lang.ArithmeticException
- ifx
is not finite
-
roundToInteger
private static double roundToInteger(double x)
Get the whole number that is the nearest to x, with ties rounding towards positive infinity.This method is intended to perform the equivalent of
Math.round(double)
without converting to along
primitive type. This allows the domain of the result to be checked against the range[-2^63, 2^63)
.Note: Adapted from
o.a.c.math4.AccurateMath.rint
and modified to perform rounding towards positive infinity.- Parameters:
x
- Number from which nearest whole number is requested.- Returns:
- a double number r such that r is an integer
r - 0.5 <= x < r + 0.5
-
divide
static double divide(Int128 x, long n)
Divide valuex
by the countn
.- Parameters:
x
- Value.n
- Count.- Returns:
- the quotient
-
-