Package com.storedobject.common
Class MathUtility
java.lang.Object
com.storedobject.common.MathUtility
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedA utility class providing a collection of mathematical helper methods. -
Method Summary
Modifier and TypeMethodDescriptionstatic intcompare(double one, double two) Compares two double values for order using a default error margin for approximate equality.static intcompare(double one, double two, double error) Compares two double values for order using a specified error margin to account for approximate equality.static intcompare(double one, double two, int decimals) Compares two double values for order using a precision determined by the specified number of decimal places.static intcountBits(long value) Counts the number of set bits (1s) in the binary representation of the given long value.static booleanequals(double one, double two) Compares two double values for approximate equality within a default error margin.static booleanequals(double one, double two, double error) Compares two double values for approximate equality based on a specified error margin.static booleanequals(double one, double two, int decimals) Compares two double values for approximate equality based on a specified number of decimal places.static booleanisZero(double value) Checks if a given double value is approximately zero within a default allowable error margin.static booleanisZero(double value, int decimals) Checks if a given double value is approximately zero within the error margin determined by the specified number of decimal places.static BigDecimaltoBigDecimal(Object value) Converts the given object into aBigDecimal.static BigDecimaltoBigDecimal(Object value, int decimals) Converts the given object to aBigDecimalwith the specified number of decimals.static BigDecimaltoBigDecimal(Object value, int decimals, boolean checkDecimals) Converts the given object into aBigDecimalwith the specified number of decimal places and an optional check for exact scaling.
-
Constructor Details
-
MathUtility
protected MathUtility()A utility class providing a collection of mathematical helper methods. This class is not designed to be instantiated and serves as a container for static methods related to mathematical operations. The constructor is protected to prevent direct instantiation by external code, while still allowing subclassing if needed.
-
-
Method Details
-
toBigDecimal
Converts the given object into aBigDecimal. The method handles various types such asBigDecimal,BigInteger, primitive numeric types, and several Java concurrency utilities. If the input is not a numeric type, the method attempts to parse it as aString.- Parameters:
value- the object to convert toBigDecimal. It can be null, a numeric type, aBigValueinstance, or an object with a validStringrepresentation of a number.- Returns:
- the resulting
BigDecimalor null if the input value is null, empty, or cannot be converted to aBigDecimal.
-
toBigDecimal
Converts the given object into aBigDecimalwith the specified number of decimal places and an optional check for exact scaling. The method handles various types such asBigDecimal,BigInteger, primitive numeric types, and several Java concurrency utilities. If the input is not a numeric type, the method attempts to parse it as aString.- Parameters:
value- the object to convert toBigDecimal. It can be null, a numeric type, aBigValueinstance, or an object with a validStringrepresentation of a number.decimals- the number of decimal places to scale the resultingBigDecimalto. Must be a non-negative integer.checkDecimals- a flag indicating whether scaling should fail if it introduces rounding. If true, usesRoundingMode.UNNECESSARYto throw an exception when rounding is required. Otherwise, usesRoundingMode.DOWN.- Returns:
- the resulting
BigDecimalscaled to the specified number of decimal places, or null if the input value is null, empty, invalid, or if scaling fails unexpectedly.
-
toBigDecimal
Converts the given object to aBigDecimalwith the specified number of decimals.- Parameters:
value- the object to be converted, which can be aString,Number, or other supported typesdecimals- the number of decimal places to round the resultingBigDecimalto- Returns:
- a
BigDecimalrepresentation of the given value, rounded to the specified number of decimals
-
isZero
public static boolean isZero(double value) Checks if a given double value is approximately zero within a default allowable error margin.- Parameters:
value- the value to be checked for being approximately zero- Returns:
trueif the value is approximately zero within the default error margin, otherwisefalse
-
isZero
public static boolean isZero(double value, int decimals) Checks if a given double value is approximately zero within the error margin determined by the specified number of decimal places.- Parameters:
value- the value to be checked for being approximately zerodecimals- the number of decimal places to determine the error margin- Returns:
trueif the value is approximately zero within the specified error margin, otherwisefalse
-
equals
public static boolean equals(double one, double two) Compares two double values for approximate equality within a default error margin.- Parameters:
one- the first double value to comparetwo- the second double value to compare- Returns:
trueif the two values are approximately equal within the default error margin, otherwisefalse
-
equals
public static boolean equals(double one, double two, int decimals) Compares two double values for approximate equality based on a specified number of decimal places. The method calculates the allowable error margin depending on the given decimal precision and checks if the difference between the two values is within that margin.- Parameters:
one- the first double value to comparetwo- the second double value to comparedecimals- the number of decimal places to determine the error margin; must be non-negative- Returns:
trueif the two values are approximately equal within the specified decimal precision, otherwisefalse
-
equals
public static boolean equals(double one, double two, double error) Compares two double values for approximate equality based on a specified error margin. The method checks whether the absolute difference between the two values is within the given error margin.- Parameters:
one- the first double value to comparetwo- the second double value to compareerror- the allowable error margin for the comparison; must be a non-negative double- Returns:
trueif the two values are approximately equal within the specified error margin, otherwisefalse
-
compare
public static int compare(double one, double two) Compares two double values for order using a default error margin for approximate equality. The comparison accounts for floating-point precision issues by allowing an allowable margin of error, which is set by default to 0.000001.- Parameters:
one- the first double value to comparetwo- the second double value to compare- Returns:
- 0 if the two values are approximately equal within the default error margin,
-1 if
oneis less thantwotaking the error margin into account, or 1 ifoneis greater thantwotaking the error margin into account
-
compare
public static int compare(double one, double two, int decimals) Compares two double values for order using a precision determined by the specified number of decimal places. The comparison accounts for floating-point precision issues by allowing an error margin calculated from the given decimal places.- Parameters:
one- the first double value to comparetwo- the second double value to comparedecimals- the number of decimal places to determine the error margin; must be non-negative- Returns:
- 0 if the two values are approximately equal within the calculated error margin,
-1 if
oneis less thantwoconsidering the error margin, or 1 ifoneis greater thantwoconsidering the error margin
-
compare
public static int compare(double one, double two, double error) Compares two double values for order using a specified error margin to account for approximate equality. If the values are considered approximately equal within the given error margin, the method returns 0. Otherwise, it returns -1 if the first value is less than the second value considering the error margin, or 1 if the first value is greater than the second value considering the error margin.- Parameters:
one- the first double value to comparetwo- the second double value to compareerror- the allowable error margin for the comparison; must be a non-negative double- Returns:
- 0 if the two values are approximately equal within the specified error margin,
-1 if
oneis less thantwoconsidering the error margin, or 1 ifoneis greater thantwoconsidering the error margin
-
countBits
public static int countBits(long value) Counts the number of set bits (1s) in the binary representation of the given long value.- Parameters:
value- the long value whose set bits are to be counted- Returns:
- the number of set bits in the binary representation of the input value
-