Package org.projog.clp.math
Class MathUtils
- java.lang.Object
-
- org.projog.clp.math.MathUtils
-
public final class MathUtils extends java.lang.ObjectContains methods for performing basic numeric operations which handle overflow errors by rounding down tojava.lang.Long.MAX_VALUEor up tojava.lang.Long.MIN_VALUE.
-
-
Constructor Summary
Constructors Modifier Constructor Description privateMathUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static longsafeAbs(long v)Returns the absolute value of alongvalue.static longsafeAdd(long x, long y)Returns the sum of the arguments, rounding up or down if the result overflows a long.static longsafeDivide(long x, long y)Returns the result of dividing the first argument by the second, rounding down if the result overflows a long.static longsafeMinus(long v)static longsafeMultiply(long x, long y)Returns the product of the arguments, rounding up or down if the result overflows a long.static longsafeSubtract(long x, long y)Returns the difference of the arguments, rounding up or down if the result overflows a long.
-
-
-
Method Detail
-
safeAdd
public static long safeAdd(long x, long y)Returns the sum of the arguments, rounding up or down if the result overflows a long.e.g.:
safeAdd(Long.MAX_Value, 1)returnsLong.MAX_VALUE.
safeAdd(Long.MIN_Value, -1)returnsLong.MIN_VALUE.- See Also:
Math.addExact(long, long)
-
safeSubtract
public static long safeSubtract(long x, long y)Returns the difference of the arguments, rounding up or down if the result overflows a long.e.g.:
safeSubtract(Long.MAX_Value, -1)returnsLong.MAX_VALUE.
safeSubtract(Long.MIN_Value, 1)returnsLong.MIN_VALUE.- See Also:
Math.subtractExact(long, long)
-
safeMultiply
public static long safeMultiply(long x, long y)Returns the product of the arguments, rounding up or down if the result overflows a long.e.g.:
safeMultiply(Long.MAX_Value, 1)returnsLong.MAX_VALUE.
safeMultiply(Long.MIN_Value, 1)returnsLong.MIN_VALUE.- See Also:
Math.multiplyExact(long, long)
-
safeDivide
public static long safeDivide(long x, long y)Returns the result of dividing the first argument by the second, rounding down if the result overflows a long.
-
safeAbs
public static long safeAbs(long v)
Returns the absolute value of alongvalue.safeAbs(Long.MIN_VALUE)returnsLong.MAX_VALUEwhereasMath.abs(Long.MIN_Value)returnsLong.MIN_VALUE.- See Also:
Math.abs(long)
-
safeMinus
public static long safeMinus(long v)
-
-