-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
Mth()
Private constructor to prevent instantiation.
This is a static helper class. -
Method Summary
Modifier and TypeMethodDescriptionstatic double
average
(double @Nullable ... values) Returns the average of the given double values.
If the given array is null or empty,Double.NaN
will be returned.static double
average
(int @Nullable ... values) Returns the average of the given integer values.
If the given array is null or empty,Double.NaN
will be returned.static double
average
(long @Nullable ... values) Returns the average of the given long values.
If the given array is null or empty,Double.NaN
will be returned.static double
clamp
(double value, double min, double max) Clamps the given double value between the given min and max value.static int
clamp
(int value, int min, int max) Clamps the given integer value between the given min and max value.static long
clamp
(long value, long min, long max) Clamps the given long value between the given min and max value.static double
frac
(double f) Returns the fractional (decimal) part of the given value.private static @NotNull BigDecimal
getBigDecimal
(@NotNull String hex, int p) Parses the actual hexadecimal string to a big decimal.static boolean
isInBounds
(double value, double min, double max) Checks if the given value is in the given bounds.
The bounds are inclusive (min ≤ value ≤ max).static boolean
isPowerOfTwo
(int value) Checks if the given value is a power of two.static double
max
(double @Nullable ... values) Returns the maximum of the given double values.
If the given array is null or empty,Double.MIN_VALUE
will be returned.static int
max
(int @Nullable ... values) Returns the maximum of the given integer values.
If the given array is null or empty,Integer.MIN_VALUE
will be returned.static long
max
(long @Nullable ... values) Returns the maximum of the given long values.
If the given array is null or empty,Long.MIN_VALUE
will be returned.static double
min
(double @Nullable ... values) Returns the minimum of the given double values.
If the given array is null or empty,Double.MAX_VALUE
will be returned.static int
min
(int @Nullable ... values) Returns the minimum of the given integer values.
If the given array is null or empty,Integer.MAX_VALUE
will be returned.static long
min
(long @Nullable ... values) Returns the minimum of the given long values.
If the given array is null or empty,Long.MAX_VALUE
will be returned.static @NotNull BigDecimal
parseHexToBigDecimal
(@NotNull String hex) Parses the given hexadecimal string to a big decimal.static int
randomExclusiveInt
(@NotNull Random rng, int min, int max) Returns a random integer between min (exclusive) and max (exclusive).
The value will be greater than min and less than max (min < value < max).static int
randomInclusiveInt
(@NotNull Random rng, int min, int max) Returns a random integer between min (inclusive) and max (inclusive).
The value will be greater than or equal to min and less than or equal to max (min ≤ value ≤ max).static int
Returns a random integer between min (inclusive) and max (exclusive).
The value will be greater than or equal to min and less than max (min ≤ value < max).static double
roundTo
(double value, int digits) Rounds the given value to the given number of digits after the decimal point.
Example:static boolean
sameValues
(Number @Nullable ... numbers) Checks if the given values have the same value.static int
sum
(int value) Sums all digits of the given integer.
If the given integer is negative, the absolute value will be used.static int
sum
(long value) Sums all digits of the given long.
If the given long is negative, the absolute value will be used.
-
Constructor Details
-
Mth
private Mth()Private constructor to prevent instantiation.
This is a static helper class.
-
-
Method Details
-
sum
public static int sum(int value) Sums all digits of the given integer.
If the given integer is negative, the absolute value will be used.- Parameters:
value
- The number to sum.- Returns:
- The sum of all digits
-
sum
public static int sum(long value) Sums all digits of the given long.
If the given long is negative, the absolute value will be used.- Parameters:
value
- The number to sum- Returns:
- The sum of all digits
-
randomInt
Returns a random integer between min (inclusive) and max (exclusive).
The value will be greater than or equal to min and less than max (min ≤ value < max).- Parameters:
rng
- The random number generatormin
- The minimum valuemax
- The maximum value- Returns:
- A random integer between min (inclusive) and max (exclusive)
- Throws:
NullPointerException
- If the given random number generator is nullIllegalArgumentException
- If the given minimum value is greater than or equal to the given maximum value
-
randomExclusiveInt
Returns a random integer between min (exclusive) and max (exclusive).
The value will be greater than min and less than max (min < value < max).- Parameters:
rng
- The random number generatormin
- The minimum valuemax
- The maximum value- Returns:
- A random integer between min (exclusive) and max (exclusive)
- Throws:
NullPointerException
- If the given random number generator is nullIllegalArgumentException
- If the given minimum value is greater than or equal to the given maximum value
-
randomInclusiveInt
Returns a random integer between min (inclusive) and max (inclusive).
The value will be greater than or equal to min and less than or equal to max (min ≤ value ≤ max).- Parameters:
rng
- The random number generatormin
- The minimum valuemax
- The maximum value- Returns:
- A random integer between min (inclusive) and max (inclusive)
- Throws:
NullPointerException
- If the given random number generator is nullIllegalArgumentException
- If the given minimum value is greater than or equal to the given maximum value
-
roundTo
public static double roundTo(double value, int digits) Rounds the given value to the given number of digits after the decimal point.
Example:
roundTo(1234.5678, 3) = 1234.568 roundTo(1234.5678, 2) = 1234.57 roundTo(1234.5678, 1) = 1234.6 roundTo(1234.5678, 0) = 1235.0 roundTo(1234.5678, -1) = 1230.0 roundTo(1234.5678, -2) = 1200.0 roundTo(1234.5678, -3) = 1000.0
- Parameters:
value
- The value to rounddigits
- The number of digits after the decimal point- Returns:
- The rounded value
-
isInBounds
public static boolean isInBounds(double value, double min, double max) Checks if the given value is in the given bounds.
The bounds are inclusive (min ≤ value ≤ max).- Parameters:
value
- The value to checkmin
- The minimum valuemax
- The maximum value- Returns:
- True if the value is in the given bounds, otherwise false
-
sameValues
Checks if the given values have the same value.
If the given array is null or empty, false will be returned.
If the given array has only one element, true will be returned.
If there are more than one element, the integer value of the first element will be compared
to the integer value of the other elements.
- Parameters:
numbers
- The numbers to compare- Returns:
- True if all numbers have the same value, otherwise false
-
frac
public static double frac(double f) Returns the fractional (decimal) part of the given value.- Parameters:
f
- The value- Returns:
- The fractional part of the given value
-
clamp
public static int clamp(int value, int min, int max) Clamps the given integer value between the given min and max value.
If the given value is less than the given min value, the min value will be returned.
If the given value is greater than the given max value, the max value will be returned.
(min ≤ value ≤ max)
- Parameters:
value
- The value to clampmin
- The minimum value (inclusive)max
- The maximum value (inclusive)- Returns:
- The clamped value
-
clamp
public static long clamp(long value, long min, long max) Clamps the given long value between the given min and max value.
If the given value is less than the given min value, the min value will be returned.
If the given value is greater than the given max value, the max value will be returned.
(min ≤ value ≤ max)
- Parameters:
value
- The value to clampmin
- The minimum value (inclusive)max
- The maximum value (inclusive)- Returns:
- The clamped value
-
clamp
public static double clamp(double value, double min, double max) Clamps the given double value between the given min and max value.
If the given value is less than the given min value, the min value will be returned.
If the given value is greater than the given max value, the max value will be returned.
(min ≤ value ≤ max)
- Parameters:
value
- The value to clampmin
- The minimum value (inclusive)max
- The maximum value (inclusive)- Returns:
- The clamped value
-
min
public static int min(int @Nullable ... values) Returns the minimum of the given integer values.
If the given array is null or empty,Integer.MAX_VALUE
will be returned.- Parameters:
values
- The values- Returns:
- The minimum of the given values
- Throws:
IllegalStateException
- If the minimum value cannot be determined (should never happen)
-
min
public static long min(long @Nullable ... values) Returns the minimum of the given long values.
If the given array is null or empty,Long.MAX_VALUE
will be returned.- Parameters:
values
- The values- Returns:
- The minimum of the given values
- Throws:
IllegalStateException
- If the minimum value cannot be determined (should never happen)
-
min
public static double min(double @Nullable ... values) Returns the minimum of the given double values.
If the given array is null or empty,Double.MAX_VALUE
will be returned.- Parameters:
values
- The values- Returns:
- The minimum of the given values
- Throws:
IllegalStateException
- If the minimum value cannot be determined (should never happen)
-
max
public static int max(int @Nullable ... values) Returns the maximum of the given integer values.
If the given array is null or empty,Integer.MIN_VALUE
will be returned.- Parameters:
values
- The values- Returns:
- The maximum of the given values
- Throws:
IllegalStateException
- If the maximum value cannot be determined (should never happen)
-
max
public static long max(long @Nullable ... values) Returns the maximum of the given long values.
If the given array is null or empty,Long.MIN_VALUE
will be returned.- Parameters:
values
- The values- Returns:
- The maximum of the given values
- Throws:
IllegalStateException
- If the maximum value cannot be determined (should never happen)
-
max
public static double max(double @Nullable ... values) Returns the maximum of the given double values.
If the given array is null or empty,Double.MIN_VALUE
will be returned.- Parameters:
values
- The values- Returns:
- The maximum of the given values
- Throws:
IllegalStateException
- If the maximum value cannot be determined (should never happen)
-
average
public static double average(int @Nullable ... values) Returns the average of the given integer values.
If the given array is null or empty,Double.NaN
will be returned.- Parameters:
values
- The values- Returns:
- The average of the given values
- Throws:
IllegalStateException
- If the average value cannot be determined (should never happen)
-
average
public static double average(long @Nullable ... values) Returns the average of the given long values.
If the given array is null or empty,Double.NaN
will be returned.- Parameters:
values
- The values- Returns:
- The average of the given values
- Throws:
IllegalStateException
- If the average value cannot be determined (should never happen)
-
average
public static double average(double @Nullable ... values) Returns the average of the given double values.
If the given array is null or empty,Double.NaN
will be returned.- Parameters:
values
- The values- Returns:
- The average of the given values
- Throws:
IllegalStateException
- If the average value cannot be determined (should never happen)
-
isPowerOfTwo
public static boolean isPowerOfTwo(int value) Checks if the given value is a power of two.- Parameters:
value
- The value to check- Returns:
- True if the given value is a power of two, otherwise false
-
parseHexToBigDecimal
Parses the given hexadecimal string to a big decimal.- Parameters:
hex
- The hexadecimal string- Returns:
- The parsed big decimal
- Throws:
NullPointerException
- If the given hexadecimal string is nullNumberFormatException
- If the given hexadecimal string is not a valid hexadecimal floating point constant
-
getBigDecimal
Parses the actual hexadecimal string to a big decimal.- Parameters:
hex
- The hexadecimal stringp
- The index of the exponent- Returns:
- The parsed big decimal
- Throws:
NullPointerException
- If the given hexadecimal string is null
-