Class MathUtility
-
Method Summary
Modifier and TypeMethodDescriptionstatic doubleclamp(double value, double min, double max) Deprecated.Use the builtin Math.clamp() method instead.static floatclamp(float value, float min, float max) Deprecated.Use the builtin Math.clamp() method instead.static intclamp(int value, int min, int max) Deprecated, for removal: This API element is subject to removal in a future version.Use the builtin Math.clamp() method instead.static longclamp(long value, long min, long max) Deprecated.Use the builtin Math.clamp() method instead.static doubleclamp01(double value) static doubleclampUlp1(double value) Clamps a double value between the limits of Math.ulp(1.0) and 1.0.static intgetGaussianAverage(List<Integer> values) This is a convenience method for callinggetGaussianAverage(List, double)with a strictness of 1.0 (neutral).static intgetGaussianAverage(List<Integer> values, double strictness) Calculates a Gaussian-weighted average from a list of integer values.static doublelerp(double min, double max, double factor) static floatlerp(float min, float max, float factor) static intlerp(int min, int max, double factor) static longlerp(long min, long max, double factor) static booleanparseBoolean(String value) Parses a string into a boolean, returningfalsewhen the input isnullor empty.static booleanparseBoolean(String value, boolean defaultValue) Utility function to handle parsing strings into boolean and to handle the possible NumberFormatException with logging and to return defaultValue.static doubleparseDouble(String value) Parses the provided string into a double.static DoubleparseDouble(String value, Double defaultValue) Utility function to handle parsing strings into Doubles and to handle the possible NumberFormatException with logging and to return defaultValue.static floatparseFloat(String value, float defaultValue) Utility function to handle parsing strings into floats and to handle the possible NumberFormatException with logging and to return defaultValue.static intAttempts to parse the provided string into an integer.static intUtility function to handle parsing strings into Integers and to handle the possible NumberFormatException with logging and to return defaultValue.static longUtility function to handle parsing strings into Integers and to handle the possible NumberFormatException with logging and to return defaultValue.static introundAwayFromZero(double value) Rounds a double value away from zero ("up" for both positive and negative values).static introundTowardsZero(double value) Rounds a double value toward zero ("down" for both positive and negative values).
-
Method Details
-
roundAwayFromZero
public static int roundAwayFromZero(double value) Rounds a double value away from zero ("up" for both positive and negative values).For positive values, this method returns the least integer greater than or equal to the value. For negative values, it returns the greatest integer less than or equal to the value (i.e., rounds further away from zero).
Special cases:
- If the input is
NaN, returns0and logs a warning. - If the rounded value is out of
intrange, returnsInteger.MAX_VALUEorInteger.MIN_VALUEas appropriate and logs a warning. - If the input is positive or negative infinity, returns
Integer.MAX_VALUEorInteger.MIN_VALUEand logs a warning.
- Parameters:
value- The double value to round.- Returns:
- The rounded integer value, away from zero.
- Since:
- 0.50.10
- If the input is
-
roundTowardsZero
public static int roundTowardsZero(double value) Rounds a double value toward zero ("down" for both positive and negative values).For positive values, this method returns the greatest integer less than or equal to the value. For negative values, it returns the least integer greater than or equal to the value (i.e., rounds closer to zero).
Special cases:
- If the input is
NaN, returns0and logs a warning. - If the rounded value is out of
intrange, returnsInteger.MAX_VALUEorInteger.MIN_VALUEas appropriate and logs a warning. - If the input is positive or negative infinity, returns
Integer.MAX_VALUEorInteger.MIN_VALUEand logs a warning.
- Parameters:
value- The double value to round.- Returns:
- The rounded integer value, toward zero.
- Since:
- 0.50.10
- If the input is
-
getGaussianAverage
This is a convenience method for callinggetGaussianAverage(List, double)with a strictness of 1.0 (neutral).- Parameters:
values- the list of integer values to average; must not benull- Returns:
- the Gaussian-weighted average as an
int, or0if the list is empty - Since:
- 0.50.10
-
getGaussianAverage
Calculates a Gaussian-weighted average from a list of integer values.This method computes a "soft" average that down-weights statistical outliers using a Gaussian (normal-distribution) weighting function. This is useful when a handful of extreme values should not influence the final result as strongly as values clustered near the center of the distribution.
How It Works
- Compute the arithmetic mean of all values.
- Compute the standard deviation. This measures how far values typically lie from the mean.
- If the standard deviation is zero (all values identical), simply return the mean. This avoids a divide-by-zero error when standardizing distances.
- Apply a configurable strictness factor to the standard deviation. Values less than
1.0increase strictness by shrinking the effective deviation (causing outliers to be down-weighted more aggressively). Values greater than1.0reduce strictness. - For each value, compute its standardized distance from the mean and apply a Gaussian weighting function:
weight = exp( -0.5 * ((value - mean) / adjustedDeviation)^2). Values closer to the mean receive weights near1.0, while more distant values rapidly approach zero weight. - Return the ratio of the weighted sum of values to the total weight.
The result behaves like a robust average: representative values dominate the calculation, while extreme outliers exert proportionally less influence. This is especially useful when working with mixed-force Battle Value (BV) or unit count arrays where unusually large or unusually small BVs/counts should not skew budgeting logic.
Strictness Guidelines
strictness < 1.0: more strict; outliers are strongly suppressed.strictness = 1.0: neutral; standard Gaussian weighting (default).strictness > 1.0: less strict; outliers retain more influence.
The strictness value should rarely be set below
0.5or above2.0. The default of1.0should work well for most distributions.To protect against overflow during conversion to
int, the final result is clamped withinInteger.MIN_VALUEandInteger.MIN_VALUEusingclamp(int, int, int).- Parameters:
values- the list of integer values to average; must not benullstrictness- how strict the calculations should be. Used to increase or decrease the influence of outliers.- Returns:
- the Gaussian-weighted average as an
int, or0if the list is empty - Since:
- 0.50.10
-
lerp
public static int lerp(int min, int max, double factor) - Parameters:
min- the minimum valuemax- the maximum valuefactor- location factor between the two points- Returns:
- integer rounded graphical linear interpolation value between min and max. A factor of 0d will return the minimum, a factor of 1d will return the maximum. Otherwise, this will return the rounded integer between the two points
-
lerp
public static double lerp(double min, double max, double factor) - Parameters:
min- the minimum valuemax- the maximum valuefactor- location factor between the two points- Returns:
- double graphical linear interpolation value between min and max. A factor of 0d will return the minimum, a factor of 1d will return the maximum. Otherwise, this will return the double value between the two points
-
lerp
public static float lerp(float min, float max, float factor) - Parameters:
min- the minimum valuemax- the maximum valuefactor- location factor between the two points- Returns:
- float graphical linear interpolation value between min and max. A factor of 0f will return the minimum, a factor of 1f will return the maximum. Otherwise, this will return the float value between the two points
-
lerp
public static long lerp(long min, long max, double factor) - Parameters:
min- the minimum valuemax- the maximum valuefactor- location factor between the two points- Returns:
- long rounded graphical linear interpolation value between min and max. A factor of 0d will return the minimum, a factor of 1d will return the maximum. Otherwise, this will return the rounded long between the two points
-
clamp
Deprecated, for removal: This API element is subject to removal in a future version.Use the builtin Math.clamp() method instead.- Parameters:
value- the int value to clampmin- the minimum limitmax- the maximum limit- Returns:
- The value if it is inside the range given by the limits (inclusive); the min value if value is below that
range and the max value if value is above that range.
- clamp(2, 6, 8) returns 6
- clamp(7, 6, 8) returns 7
- clamp(12, 3, 5) returns 5
-
clamp
public static double clamp(double value, double min, double max) Deprecated.Use the builtin Math.clamp() method instead.- Parameters:
value- the double value to clampmin- the minimum limitmax- the maximum limit- Returns:
- The value if it is inside the range given by the limits (inclusive); the min value if value is below that
range and the max value if value is above that range.
- clamp(2, 6, 8) returns 6
- clamp(7, 6, 8) returns 7
- clamp(12, 3, 5) returns 5
-
clamp
public static float clamp(float value, float min, float max) Deprecated.Use the builtin Math.clamp() method instead.- Parameters:
value- the float value to clampmin- the minimum limitmax- the maximum limit- Returns:
- The value if it is inside the range given by the limits (inclusive); the min value if value is below that
range and the max value if value is above that range.
- clamp(2, 6, 8) returns 6
- clamp(7, 6, 8) returns 7
- clamp(12, 3, 5) returns 5
-
clamp
public static long clamp(long value, long min, long max) Deprecated.Use the builtin Math.clamp() method instead.- Parameters:
value- the long value to clampmin- the minimum limitmax- the maximum limit- Returns:
- The value if it is inside the range given by the limits (inclusive); the min value if value is below that
range and the max value if value is above that range.
- clamp(2, 6, 8) returns 6
- clamp(7, 6, 8) returns 7
- clamp(12, 3, 5) returns 5
-
clamp01
public static double clamp01(double value) - Parameters:
value- the long value to clamp- Returns:
- The value if it is inside the range given by the limits 0.0-1.0 (inclusive); the min value if value is
below that range and the max value if value is above that range.
- clamp01(1.3) returns 1.0
- clamp01(0.3) returns 0.3
- clamp01(-5) returns 0
-
clampUlp1
public static double clampUlp1(double value) Clamps a double value between the limits of Math.ulp(1.0) and 1.0. Math.ulp is the smallest positive double value that is greater than 0.0.- Parameters:
value- the double value to clamp between Math.ulp(1.0) and 1.0- Returns:
- The value if it is inside the range given by the limits (inclusive); the min value if value is below that
range and the max value if value is above that range.
- clamp(0.3) returns 0.3
- clamp(7) returns 1
- clamp(-4) returns 2.220446049250313E-16
-
parseInt
Utility function to handle parsing strings into Integers and to handle the possible NumberFormatException with logging and to return defaultValue.- Parameters:
value- String value to parse.defaultValue- Default value to set if failed to parse.- Returns:
- The
intvalue or defaultValue.
-
parseInt
Attempts to parse the provided string into an integer.If parsing fails, the method defaults to returning
0. To specify a custom default value in case of failure, use the overloadedparseInt(String, int)method.- Parameters:
value- The string to parse. Can be a numeric string or null.- Returns:
- The integer value parsed from the string, or
0if parsing fails. - See Also:
-
parseLong
Utility function to handle parsing strings into Integers and to handle the possible NumberFormatException with logging and to return defaultValue.- Parameters:
value- String value to parse.defaultValue- Default value to set if failed to parse.- Returns:
- The
intvalue or defaultValue.
-
parseDouble
Utility function to handle parsing strings into Doubles and to handle the possible NumberFormatException with logging and to return defaultValue.- Parameters:
value- String value to parse.defaultValue- Default value to set if failed to parse.- Returns:
- The
doublevalue or defaultValue.
-
parseDouble
Parses the provided string into a double. If parsing fails, a default value of 0.0 is returned. This method delegates toparseDouble(String, Double)with a default value.- Parameters:
value- the string to parse. Can be a numeric string ornull.- Returns:
- the parsed double value, or 0.0 if parsing fails.
- Since:
- 0.50.07
-
parseFloat
Utility function to handle parsing strings into floats and to handle the possible NumberFormatException with logging and to return defaultValue.- Parameters:
value- String value to parse.defaultValue- Default value to set if failed to parse.- Returns:
- The
floatvalue or defaultValue.
-
parseBoolean
Utility function to handle parsing strings into boolean and to handle the possible NumberFormatException with logging and to return defaultValue.- Parameters:
value- String value to parse.defaultValue- Default value to set if failed to parse.- Returns:
- The
booleanvalue or defaultValue.
-
parseBoolean
Parses a string into a boolean, returningfalsewhen the input isnullor empty.Note:
Boolean.parseBoolean(String)returnstrueonly for (case-insensitive)"true".- Parameters:
value- String value to parse.- Returns:
- The
booleanvalue, orfalseifvalueisnullor empty.
-