java.lang.Object
ushiosan.jvm_utilities.lang.Maths
Class that contains extra utilities for mathematical operations
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final doubleDefault decimal operations tolerance -
Method Summary
Modifier and TypeMethodDescriptionstatic doubleclamp(double value, double min, double max) Keeps the value within a specified range.static doubledistance(double v1, double v2) Returns the distance between two values.static booleanequals(double v1, double v2) Check if two decimal numbers are equal.static booleanequals(double v1, double v2, double tolerance) Check if two decimal numbers are equal.static booleanisZero(double value) Checks if a decimal number is zerostatic booleanisZero(double value, double tolerance) Checks if a decimal number is zerostatic doublelerp(double base, double objective, double amount) Linear interpolation between two numbers.static doublelerpPrecise(double base, double objective, double amount) Linear interpolation between two numbers.static doublenormalize(double start, double end, double current) Linearly normalizes two numbers.static doublepercentage(double value, double percentage) Calculates the percentage of an amount.static doublepercentageValue(double percentage) Converts a percent based on hundreds to a decimal percent.static doubletoDegrees(double radians) Performs a conversion from radians to degreesstatic doubletoRadians(double degrees) Performs a conversion from degrees to radians
-
Field Details
-
DECIMAL_TOLERANCE
public static final double DECIMAL_TOLERANCEDefault decimal operations tolerance32-bit number tolerance
- See Also:
-
-
Method Details
-
lerp
public static double lerp(double base, double objective, double amount) Linear interpolation between two numbers.- Parameters:
base- the base numberobjective- the destination numberamount- the amount of distance in each iteration- Returns:
- the result of the iteration
-
lerpPrecise
public static double lerpPrecise(double base, double objective, double amount) Linear interpolation between two numbers.Unlike
lerp(double, double, double), this method is more precise and performs an intermediate comparison (but has a slightly higher performance cost).- Parameters:
base- the base numberobjective- the destination numberamount- the amount of distance in each iteration- Returns:
- the result of the iteration
-
distance
public static double distance(double v1, double v2) Returns the distance between two values.- Parameters:
v1- value 1v2- value 2- Returns:
- the distance between two values
-
normalize
public static double normalize(double start, double end, double current) Linearly normalizes two numbers. This range must not be empty.- Parameters:
start- the start rangeend- the end of rangecurrent- the value to normalize- Returns:
- the normalized value.
-
clamp
public static double clamp(double value, double min, double max) Keeps the value within a specified range.- Parameters:
value- the number to clampmin- the minimum value of the rangemax- the maximum value of the range- Returns:
- the constrained value within the given range
-
equals
public static boolean equals(double v1, double v2, double tolerance) Check if two decimal numbers are equal. Decimal number operations can lead to many errors because they take into account decimals and operations that may seem the same, in reality they are not:
that is why this method uses tolerance, to avoid this type of problem.// JVM Operations float x = 0.2f; float y = x - 0.1f; if(y == 0.1){ // can be "false" because "y" is 0.99999999999999... // ... }- Parameters:
v1- first valuev2- second valuetolerance- operation tolerance- Returns:
trueif the numbers are equal orfalseotherwise
-
equals
public static boolean equals(double v1, double v2) Check if two decimal numbers are equal.- Parameters:
v1- first valuev2- second value- Returns:
trueif the numbers are equal orfalseotherwise- See Also:
-
isZero
public static boolean isZero(double value, double tolerance) Checks if a decimal number is zero- Parameters:
value- the value to checktolerance- inspection tolerance- Returns:
trueif the number is0orfalseotherwise
-
isZero
public static boolean isZero(double value) Checks if a decimal number is zero- Parameters:
value- the value to check- Returns:
trueif the number is0orfalseotherwise- See Also:
-
toDegrees
public static double toDegrees(double radians) Performs a conversion from radians to degrees- Parameters:
radians- the radians to convert- Returns:
- the conversion from radians to degrees
- See Also:
-
RADIANS_DEGREE
-
toRadians
public static double toRadians(double degrees) Performs a conversion from degrees to radians- Parameters:
degrees- the degrees to convert- Returns:
- the conversion from degrees to radians
- See Also:
-
DEGREE_RADIANS
-
percentage
public static double percentage(double value, double percentage) Calculates the percentage of an amount.Example:
double fiftyPercent = Maths.percentage(500, 50); // 50% -> 250 double doubleValue = Maths.percentage(120, 200); // 200% -> 240- Parameters:
value- the value to convertpercentage- the percentage to calculate- Returns:
- the percentage calculated based on the given amount
- See Also:
-
percentageValue
public static double percentageValue(double percentage) Converts a percent based on hundreds to a decimal percent.Example:
double fifty = Maths.percentageValue(50); // 50% -> 0.5 double ten = Maths.percentageValue(1); // 10% -> 0.1 double one = Maths.percentageValue(1); // 01% -> 0.01- Parameters:
percentage- the percentage to calculate- Returns:
- the decimal percentage value
- See Also:
-