Package de.gurkenlabs.litiengine.util
Class MathUtilities
java.lang.Object
de.gurkenlabs.litiengine.util.MathUtilities
Utility class for mathematical operations.
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanequals(double d1, double d2, double epsilon) Checks if two double values are equal within a given epsilon.static doublegetAverage(double[] numbers) Calculates the average of an array of double values.static floatgetAverage(float[] numbers) Calculates the average of an array of float values.static doublegetAverage(int[] numbers) Calculates the average of an array of int values.static intgetFullPercent(double value, double fraction) Calculates the percentage of a fraction relative to a value.static intgetMax(int... numbers) Finds the maximum value in an array of int values.static doublegetPercent(double value, double fraction) Calculates the percentage of a fraction relative to a value.static booleanisInt(double value) Checks if a double value is an integer.static booleanisOddNumber(int num) Checks if an int value is an odd number.static doubleround(double value, int places) Rounds a double value to the specified number of decimal places.static floatround(float value, int places) Rounds a float value to the specified number of decimal places.
-
Method Details
-
equals
public static boolean equals(double d1, double d2, double epsilon) Checks if two double values are equal within a given epsilon.- Parameters:
d1- the first double valued2- the second double valueepsilon- the tolerance within which the two values are considered equal- Returns:
- true if the absolute difference between d1 and d2 is less than or equal to epsilon, false otherwise
-
round
public static float round(float value, int places) Rounds a float value to the specified number of decimal places.- Parameters:
value- the float value to be roundedplaces- the number of decimal places to round to- Returns:
- the rounded float value
-
round
public static double round(double value, int places) Rounds a double value to the specified number of decimal places.- Parameters:
value- the double value to be roundedplaces- the number of decimal places to round to- Returns:
- the rounded double value
- Throws:
IllegalArgumentException- if the number of decimal places is negative
-
getAverage
public static double getAverage(double[] numbers) Calculates the average of an array of double values.- Parameters:
numbers- the array of double values- Returns:
- the average of the values in the array
-
getAverage
public static float getAverage(float[] numbers) Calculates the average of an array of float values.- Parameters:
numbers- the array of float values- Returns:
- the average of the values in the array
-
getAverage
public static double getAverage(int[] numbers) Calculates the average of an array of int values.- Parameters:
numbers- the array of int values- Returns:
- the average of the values in the array
-
getMax
public static int getMax(int... numbers) Finds the maximum value in an array of int values.- Parameters:
numbers- the array of int values- Returns:
- the maximum value in the array
-
isInt
public static boolean isInt(double value) Checks if a double value is an integer.- Parameters:
value- the double value to check- Returns:
- true if the value is an integer, false otherwise
-
isOddNumber
public static boolean isOddNumber(int num) Checks if an int value is an odd number.- Parameters:
num- the int value to check- Returns:
- true if the value is odd, false otherwise
-
getFullPercent
public static int getFullPercent(double value, double fraction) Calculates the percentage of a fraction relative to a value.- Parameters:
value- the total valuefraction- the fraction of the total value- Returns:
- the percentage of the fraction relative to the total value
-
getPercent
public static double getPercent(double value, double fraction) Calculates the percentage of a fraction relative to a value.- Parameters:
value- the total valuefraction- the fraction of the total value- Returns:
- the percentage of the fraction relative to the total value
-