Package org.projog.clp.math
Class MathUtils
java.lang.Object
org.projog.clp.math.MathUtils
Contains methods for performing basic numeric operations which handle overflow errors by rounding down to
java.lang.Long.MAX_VALUE or up to java.lang.Long.MIN_VALUE.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic 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.
-
Constructor Details
-
MathUtils
private MathUtils()
-
-
Method Details
-
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:
-
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:
-
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:
-
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:
-
safeMinus
public static long safeMinus(long v)
-