Class MathUtils

java.lang.Object
net.time4j.base.MathUtils

public final class MathUtils extends Object

Defines some mathematical routines which are needed in calendrical calculations.

Author:
Meno Hochschild
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    floorDivide​(int value, int divisor)
    Returns the largest lower limit of quotient.
    static long
    floorDivide​(long value, int divisor)
    static int
    floorModulo​(int value, int divisor)
    Calculates the remainder based on floorDivide(int, int).
    static int
    floorModulo​(long value, int divisor)
    static int
    safeAdd​(int op1, int op2)
    Sums up the numbers with range check.
    static long
    safeAdd​(long op1, long op2)
    Sums up the numbers with range check.
    static int
    safeCast​(long num)
    Performs a safe type-cast to an int-primitive.
    static int
    safeMultiply​(int op1, int op2)
    Multiplies the numbers with range check.
    static long
    safeMultiply​(long op1, long op2)
    Multiplies the numbers with range check.
    static int
    safeNegate​(int value)
    Inverts the number with range check.
    static long
    safeNegate​(long value)
    Inverts the number with range check.
    static int
    safeSubtract​(int op1, int op2)
    Subtracts the numbers from each other with range check.
    static long
    safeSubtract​(long op1, long op2)
    Subtracts the numbers from each other with range check.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • safeCast

      public static int safeCast(long num)

      Performs a safe type-cast to an int-primitive.

      Parameters:
      num - long-primitive
      Returns:
      int as type-cast
      Throws:
      ArithmeticException - if int-range overflows
    • safeAdd

      public static int safeAdd(int op1, int op2)

      Sums up the numbers with range check.

      Parameters:
      op1 - first operand
      op2 - second operand
      Returns:
      sum
      Throws:
      ArithmeticException - if int-range overflows
    • safeAdd

      public static long safeAdd(long op1, long op2)

      Sums up the numbers with range check.

      Parameters:
      op1 - first operand
      op2 - second operand
      Returns:
      sum
      Throws:
      ArithmeticException - if long-range overflows
    • safeSubtract

      public static int safeSubtract(int op1, int op2)

      Subtracts the numbers from each other with range check.

      Parameters:
      op1 - first operand
      op2 - second operand
      Returns:
      difference
      Throws:
      ArithmeticException - if int-range overflows
    • safeSubtract

      public static long safeSubtract(long op1, long op2)

      Subtracts the numbers from each other with range check.

      Parameters:
      op1 - first operand
      op2 - second operand
      Returns:
      difference
      Throws:
      ArithmeticException - if long-range overflows
    • safeMultiply

      public static int safeMultiply(int op1, int op2)

      Multiplies the numbers with range check.

      Parameters:
      op1 - first operand
      op2 - second operand
      Returns:
      product
      Throws:
      ArithmeticException - if int-range overflows
    • safeMultiply

      public static long safeMultiply(long op1, long op2)

      Multiplies the numbers with range check.

      Parameters:
      op1 - first operand
      op2 - second operand
      Returns:
      product
      Throws:
      ArithmeticException - if long-range overflows
    • safeNegate

      public static int safeNegate(int value)

      Inverts the number with range check.

      Parameters:
      value - value to be negated
      Returns:
      the expression -value
      Throws:
      ArithmeticException - if int-range overflows
    • safeNegate

      public static long safeNegate(long value)

      Inverts the number with range check.

      Parameters:
      value - value to be negated
      Returns:
      the expression -value
      Throws:
      ArithmeticException - if long-range overflows
    • floorDivide

      public static int floorDivide(int value, int divisor)

      Returns the largest lower limit of quotient.

      Examples:

      • floorDivide(2, 2) == 1
      • floorDivide(1, 2) == 0
      • floorDivide(0, 2) == 0
      • floorDivide(-1, 2) == -1
      • floorDivide(-2, 2) == -1
      • floorDivide(-3, 2) == -2
      Parameters:
      value - numerator
      divisor - divisor
      Returns:
      quotient as result of division
    • floorDivide

      public static long floorDivide(long value, int divisor)
      Parameters:
      value - numerator
      divisor - divisor
      Returns:
      quotient as result of division
    • floorModulo

      public static int floorModulo(int value, int divisor)

      Calculates the remainder based on floorDivide(int, int).

      Examples:

      • floorModulo(2, 2) == 0
      • floorModulo(1, 2) == 1
      • floorModulo(0, 2) == 0
      • floorModulo(-1, 2) == 1
      • floorModulo(-2, 2) == 0
      • floorModulo(-3, 2) == 1
      Parameters:
      value - numerator
      divisor - divisor
      Returns:
      remainder of division (never negative if divisor is positive)
    • floorModulo

      public static int floorModulo(long value, int divisor)
      Parameters:
      value - numerator
      divisor - divisor
      Returns:
      remainder of division (never negative if divisor is positive)