Class MathUtils
java.lang.Math.-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivatePrivate constructor since this is a utility class. -
Method Summary
Modifier and TypeMethodDescriptionstatic intfloorDiv(int a, int b) Returns the floor division.static longfloorDiv(long a, long b) Returns the floor division.static intfloorMod(int a, int b) Returns the floor modulus.static intfloorMod(long a, int b) Returns the floor modulus.static longfloorMod(long a, long b) Returns the floor modulus.static intsafeAdd(int a, int b) Safely adds two int values.static longsafeAdd(long a, long b) Safely adds two long values.static intsafeCompare(int a, int b) Safely compare one int with another.static intsafeCompare(long a, long b) Safely compare one long with another.static intsafeDecrement(int value) Safely decrements an int.static longsafeDecrement(long value) Safely decrements a long.static intsafeIncrement(int value) Safely increments an int.static longsafeIncrement(long value) Safely increments a long.static intsafeMultiply(int a, int b) Safely multiply one int by another.static longsafeMultiply(long a, int b) Safely multiply a long by an int.static longsafeMultiply(long a, long b) Multiply two values throwing an exception if overflow occurs.static intsafeNegate(int value) Negates the input value, throwing an exception if an overflow occurs.static longsafeNegate(long value) Negates the input value, throwing an exception if an overflow occurs.static intsafeSubtract(int a, int b) Safely subtracts one int from another.static longsafeSubtract(long a, long b) Safely subtracts one long from another.static intsafeToInt(long value) Safely convert a long to an int.
-
Constructor Details
-
MathUtils
private MathUtils()Private constructor since this is a utility class.
-
-
Method Details
-
safeNegate
public static int safeNegate(int value) Negates the input value, throwing an exception if an overflow occurs.- Parameters:
value- the value to negate- Returns:
- the negated value
- Throws:
ArithmeticException- if the value is MIN_VALUE and cannot be negated
-
safeNegate
public static long safeNegate(long value) Negates the input value, throwing an exception if an overflow occurs.- Parameters:
value- the value to negate- Returns:
- the negated value
- Throws:
ArithmeticException- if the value is MIN_VALUE and cannot be negated
-
safeAdd
public static int safeAdd(int a, int b) Safely adds two int values.- Parameters:
a- the first valueb- the second value- Returns:
- the result
- Throws:
ArithmeticException- if the result overflows an int
-
safeAdd
public static long safeAdd(long a, long b) Safely adds two long values.- Parameters:
a- the first valueb- the second value- Returns:
- the result
- Throws:
ArithmeticException- if the result overflows a long
-
safeSubtract
public static int safeSubtract(int a, int b) Safely subtracts one int from another.- Parameters:
a- the first valueb- the second value to subtract from the first- Returns:
- the result
- Throws:
ArithmeticException- if the result overflows an int
-
safeSubtract
public static long safeSubtract(long a, long b) Safely subtracts one long from another.- Parameters:
a- the first valueb- the second value to subtract from the first- Returns:
- the result
- Throws:
ArithmeticException- if the result overflows a long
-
safeMultiply
public static int safeMultiply(int a, int b) Safely multiply one int by another.- Parameters:
a- the first valueb- the second value- Returns:
- the result
- Throws:
ArithmeticException- if the result overflows an int
-
safeMultiply
public static long safeMultiply(long a, int b) Safely multiply a long by an int.- Parameters:
a- the first valueb- the second value- Returns:
- the new total
- Throws:
ArithmeticException- if the result overflows a long
-
safeMultiply
public static long safeMultiply(long a, long b) Multiply two values throwing an exception if overflow occurs.- Parameters:
a- the first valueb- the second value- Returns:
- the new total
- Throws:
ArithmeticException- if the result overflows a long
-
safeIncrement
public static int safeIncrement(int value) Safely increments an int.- Parameters:
value- the value to increment- Returns:
- the result
- Throws:
ArithmeticException- if the result overflows a long
-
safeIncrement
public static long safeIncrement(long value) Safely increments a long.- Parameters:
value- the value to increment- Returns:
- the result
- Throws:
ArithmeticException- if the result overflows a long
-
safeDecrement
public static int safeDecrement(int value) Safely decrements an int.- Parameters:
value- the value to decrement- Returns:
- the result
- Throws:
ArithmeticException- if the result overflows a long
-
safeDecrement
public static long safeDecrement(long value) Safely decrements a long.- Parameters:
value- the value to decrement- Returns:
- the result
- Throws:
ArithmeticException- if the result overflows a long
-
safeToInt
public static int safeToInt(long value) Safely convert a long to an int.- Parameters:
value- the value to convert- Returns:
- the int value
- Throws:
ArithmeticException- if the result overflows an int
-
safeCompare
public static int safeCompare(int a, int b) Safely compare one int with another.- Parameters:
a- the first valueb- the second value- Returns:
- negative if a is less than b, positive if a is greater than b, zero if equal
-
safeCompare
public static int safeCompare(long a, long b) Safely compare one long with another.- Parameters:
a- the first valueb- the second value- Returns:
- negative if a is less than b, positive if a is greater than b, zero if equal
-
floorDiv
public static long floorDiv(long a, long b) Returns the floor division.This returns
0forfloorDiv(0, 4).
This returns-1forfloorDiv(-1, 4).
This returns-1forfloorDiv(-2, 4).
This returns-1forfloorDiv(-3, 4).
This returns-1forfloorDiv(-4, 4).
This returns-2forfloorDiv(-5, 4).- Parameters:
a- the dividendb- the divisor- Returns:
- the floor division
-
floorMod
public static long floorMod(long a, long b) Returns the floor modulus.This returns
0forfloorMod(0, 4).
This returns1forfloorMod(-1, 4).
This returns2forfloorMod(-2, 4).
This returns3forfloorMod(-3, 4).
This returns0forfloorMod(-4, 4).- Parameters:
a- the dividendb- the divisor- Returns:
- the floor modulus (positive)
-
floorMod
public static int floorMod(long a, int b) Returns the floor modulus.This returns
0forfloorMod(0, 4).
This returns3forfloorMod(-1, 4).
This returns2forfloorMod(-2, 4).
This returns1forfloorMod(-3, 4).
This returns0forfloorMod(-4, 4).
This returns3forfloorMod(-5, 4).- Parameters:
a- the dividendb- the divisor- Returns:
- the floor modulus (positive)
-
floorDiv
public static int floorDiv(int a, int b) Returns the floor division.This returns
0forfloorDiv(0, 4).
This returns-1forfloorDiv(-1, 4).
This returns-1forfloorDiv(-2, 4).
This returns-1forfloorDiv(-3, 4).
This returns-1forfloorDiv(-4, 4).
This returns-2forfloorDiv(-5, 4).- Parameters:
a- the dividendb- the divisor- Returns:
- the floor division
-
floorMod
public static int floorMod(int a, int b) Returns the floor modulus.This returns
0forfloorMod(0, 4).
This returns1forfloorMod(-1, 4).
This returns2forfloorMod(-2, 4).
This returns3forfloorMod(-3, 4).
This returns0forfloorMod(-4, 4).- Parameters:
a- the dividendb- the divisor- Returns:
- the floor modulus (positive)
-