java.lang.Object
com.aoapps.lang.math.SafeMath
Math routines that check for overflow conditions.
- Author:
- AO Industries, Inc.
-
Method Summary
Modifier and TypeMethodDescriptionstatic intavg(int... values) Computes the average of multiple values without overflow or underflow.static intavg(int value1, int value2) Computes the average of two values without overflow or underflow.static bytecastByte(int value) Casts int to byte, looking for any underflow or overflow.static bytecastByte(long value) Casts long to byte, looking for any underflow or overflow.static intcastInt(long value) Casts long to int, looking for any underflow or overflow.static shortcastShort(int value) Casts int to short, looking for any underflow or overflow.static shortcastShort(long value) Casts long to short, looking for any underflow or overflow.static longmultiply(long... values) Multiplies any number of longs, looking for any overflow.static longmultiply(long value1, long value2) Deprecated.
-
Method Details
-
castByte
Casts int to byte, looking for any underflow or overflow.- Throws:
ArithmeticException- for underflow or overflow
-
castByte
Casts long to byte, looking for any underflow or overflow.- Throws:
ArithmeticException- for underflow or overflow
-
castShort
Casts int to short, looking for any underflow or overflow.- Throws:
ArithmeticException- for underflow or overflow
-
castShort
Casts long to short, looking for any underflow or overflow.- Throws:
ArithmeticException- for underflow or overflow
-
castInt
Casts long to int, looking for any underflow or overflow.- Throws:
ArithmeticException- for underflow or overflow
-
multiply
Deprecated.Please useMath.multiplyExact(long, long)as of Java 8.Multiplies two longs, looking for any overflow.- Throws:
ArithmeticException- for overflow
-
multiply
public static long multiply(long... values) Multiplies any number of longs, looking for any overflow.- Returns:
- The product or
1when no values - Throws:
ArithmeticException- for overflow- See Also:
-
avg
public static int avg(int value1, int value2) Computes the average of two values without overflow or underflow. -
avg
public static int avg(int... values) Computes the average of multiple values without overflow or underflow.- Throws:
ArithmeticException- When values is empty (due to resulting division by zero)
-

Math.multiplyExact(long, long)as of Java 8.