Package net.time4j.base
Class MathUtils
java.lang.Object
net.time4j.base.MathUtils
Defines some mathematical routines which are needed in calendrical calculations.
- Author:
- Meno Hochschild
-
Method Summary
Modifier and TypeMethodDescriptionstatic intfloorDivide(int value, int divisor)Returns the largest lower limit of quotient.static longfloorDivide(long value, int divisor)static intfloorModulo(int value, int divisor)Calculates the remainder based onfloorDivide(int, int).static intfloorModulo(long value, int divisor)static intsafeAdd(int op1, int op2)Sums up the numbers with range check.static longsafeAdd(long op1, long op2)Sums up the numbers with range check.static intsafeCast(long num)Performs a safe type-cast to an int-primitive.static intsafeMultiply(int op1, int op2)Multiplies the numbers with range check.static longsafeMultiply(long op1, long op2)Multiplies the numbers with range check.static intsafeNegate(int value)Inverts the number with range check.static longsafeNegate(long value)Inverts the number with range check.static intsafeSubtract(int op1, int op2)Subtracts the numbers from each other with range check.static longsafeSubtract(long op1, long op2)Subtracts the numbers from each other with range check.
-
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 operandop2- 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 operandop2- 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 operandop2- 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 operandop2- 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 operandop2- 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 operandop2- 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) == 1floorDivide(1, 2) == 0floorDivide(0, 2) == 0floorDivide(-1, 2) == -1floorDivide(-2, 2) == -1floorDivide(-3, 2) == -2
- Parameters:
value- numeratordivisor- divisor- Returns:
- quotient as result of division
-
floorDivide
public static long floorDivide(long value, int divisor)- Parameters:
value- numeratordivisor- 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) == 0floorModulo(1, 2) == 1floorModulo(0, 2) == 0floorModulo(-1, 2) == 1floorModulo(-2, 2) == 0floorModulo(-3, 2) == 1
- Parameters:
value- numeratordivisor- divisor- Returns:
- remainder of division (never negative if divisor is positive)
-
floorModulo
public static int floorModulo(long value, int divisor)- Parameters:
value- numeratordivisor- divisor- Returns:
- remainder of division (never negative if divisor is positive)
-