Class MathUtils

java.lang.Object
org.projog.clp.math.MathUtils

public final class MathUtils extends Object
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
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static long
    safeAbs(long v)
    Returns the absolute value of a long value.
    static long
    safeAdd(long x, long y)
    Returns the sum of the arguments, rounding up or down if the result overflows a long.
    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.
    static long
    safeMinus(long v)
     
    static long
    safeMultiply(long x, long y)
    Returns the product of the arguments, rounding up or down if the result overflows a long.
    static long
    safeSubtract(long x, long y)
    Returns the difference of the arguments, rounding up or down if the result overflows a long.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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) returns Long.MAX_VALUE.
      safeAdd(Long.MIN_Value, -1) returns Long.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) returns Long.MAX_VALUE.
      safeSubtract(Long.MIN_Value, 1) returns Long.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) returns Long.MAX_VALUE.
      safeMultiply(Long.MIN_Value, 1) returns Long.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 a long value.

      safeAbs(Long.MIN_VALUE) returns Long.MAX_VALUE whereas Math.abs(Long.MIN_Value) returns Long.MIN_VALUE.

      See Also:
    • safeMinus

      public static long safeMinus(long v)