- java.lang.Object
-
- com.aoapps.lang.math.SafeMath
-
public final class SafeMath extends Object
Math routines that check for overflow conditions.- Author:
- AO Industries, Inc.
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static 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.Please useMath.multiplyExact(long, long)
-
-
-
Method Detail
-
castByte
public static byte castByte(int value) throws ArithmeticExceptionCasts int to byte, looking for any underflow or overflow.- Throws:
ArithmeticException- for underflow or overflow
-
castByte
public static byte castByte(long value) throws ArithmeticExceptionCasts long to byte, looking for any underflow or overflow.- Throws:
ArithmeticException- for underflow or overflow
-
castShort
public static short castShort(int value) throws ArithmeticExceptionCasts int to short, looking for any underflow or overflow.- Throws:
ArithmeticException- for underflow or overflow
-
castShort
public static short castShort(long value) throws ArithmeticExceptionCasts long to short, looking for any underflow or overflow.- Throws:
ArithmeticException- for underflow or overflow
-
castInt
public static int castInt(long value) throws ArithmeticExceptionCasts long to int, looking for any underflow or overflow.- Throws:
ArithmeticException- for underflow or overflow
-
multiply
@Deprecated public static long multiply(long value1, long value2)
Deprecated.Please useMath.multiplyExact(long, long)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:
Math.multiplyExact(long, long)
-
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)
-
-