java.lang.Object
com.aoapps.lang.math.SafeMath
Math routines that check for overflow conditions.
- Author:
- AO Industries, Inc.
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
avg
(int... values) Computes the average of multiple values without overflow or underflow.static int
avg
(int value1, int value2) Computes the average of two values without overflow or underflow.static byte
castByte
(int value) Casts int to byte, looking for any underflow or overflow.static byte
castByte
(long value) Casts long to byte, looking for any underflow or overflow.static int
castInt
(long value) Casts long to int, looking for any underflow or overflow.static short
castShort
(int value) Casts int to short, looking for any underflow or overflow.static short
castShort
(long value) Casts long to short, looking for any underflow or overflow.static long
multiply
(long... values) Multiplies any number of longs, looking for any overflow.static long
multiply
(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
1
when 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.