Package io.deephaven.base
Class MathUtil
java.lang.Object
io.deephaven.base.MathUtil
A handful of simple mathematical utilities.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
base10digits
(int n) Compute the number of base 10 digits in n's representation, for n >= 0.static int
ceilLog2
(int x) Compute ceil(log2(x)).static int
ceilLog2
(long x) Compute ceil(log2(x)).static int
floorLog2
(int x) Compute floor(log2(x)).static int
floorLog2
(long x) Compute floor(log2(x)).static int
gcd
(int a, int b) Compute the greatest common divisor of two integers using the Euclidean algorithm.static int
pow10
(int n) Compute 10^n as a int for 0 <= n <= 9.static int
roundUpArraySize
(int size) Rounds up to the next power of 2 forsize <= MAX_POWER_OF_2
, otherwise returnsArrayUtil.MAX_ARRAY_SIZE
.static int
roundUpPowerOf2
(int x) Rounds up to the next power of 2 forx
; ifx
is already a power of 2,x
will be returned.static long
roundUpPowerOf2
(long x) Rounds up to the next power of 2 forx
; ifx
is already a power of 2,x
will be returned.
-
Field Details
-
MAX_POWER_OF_2
public static final int MAX_POWER_OF_2The maximum power of 2.- See Also:
-
-
Constructor Details
-
MathUtil
public MathUtil()
-
-
Method Details
-
ceilLog2
public static int ceilLog2(int x) Compute ceil(log2(x)). SeeInteger.numberOfLeadingZeros(int)
.- Parameters:
x
- Input- Returns:
- ceil(log2(x))
-
floorLog2
public static int floorLog2(int x) Compute floor(log2(x)). SeeInteger.numberOfLeadingZeros(int)
.- Parameters:
x
- Input- Returns:
- floor(log2(x))
-
ceilLog2
public static int ceilLog2(long x) Compute ceil(log2(x)). SeeLong.numberOfLeadingZeros(long)
.- Parameters:
x
- Input- Returns:
- ceil(log2(x))
-
floorLog2
public static int floorLog2(long x) Compute floor(log2(x)). SeeLong.numberOfLeadingZeros(long)
.- Parameters:
x
- Input- Returns:
- floor(log2(x))
-
gcd
public static int gcd(int a, int b) Compute the greatest common divisor of two integers using the Euclidean algorithm.- Parameters:
a
- The first inputb
- The second input- Returns:
- The GCD
- ImplNote:
- Always gives a non-negative result.
-
pow10
public static int pow10(int n) Compute 10^n as a int for 0 <= n <= 9.- Parameters:
n
- the exponent- Returns:
- 10^n
-
base10digits
public static int base10digits(int n) Compute the number of base 10 digits in n's representation, for n >= 0.- Parameters:
n
- an integer >= 0- Returns:
- how many digits in n's base 10 representation.
-
roundUpPowerOf2
public static int roundUpPowerOf2(int x) Rounds up to the next power of 2 forx
; ifx
is already a power of 2,x
will be returned. Values outside the range1 <= x <= MAX_POWER_OF_2
will return1
.Equivalent to
Math.max(Integer.highestOneBit(x - 1) << 1, 1)
.- Parameters:
x
- the value- Returns:
- the next power of 2 for
x
- See Also:
-
roundUpPowerOf2
public static long roundUpPowerOf2(long x) Rounds up to the next power of 2 forx
; ifx
is already a power of 2,x
will be returned. Values outside the range1 <= x <= Long.MAX_VALUE
will return1
.Equivalent to
Math.max(Long.highestOneBit(x - 1) << 1, 1)
.- Parameters:
x
- the value- Returns:
- the next power of 2 for
x
-
roundUpArraySize
public static int roundUpArraySize(int size) Rounds up to the next power of 2 forsize <= MAX_POWER_OF_2
, otherwise returnsArrayUtil.MAX_ARRAY_SIZE
.Equivalent to
size <= MAX_POWER_OF_2 ? roundUpPowerOf2(size) : ArrayUtil.MAX_ARRAY_SIZE
.- Parameters:
size
- the size- Returns:
- the
- See Also:
-