MathUtils
public class MathUtils
A utility class providing functions useful for common mathematical operations.
Summary
Public methods |
|
|---|---|
static int |
addExact(int x, int y)Returns the sum of its arguments, throwing an exception if the result overflows an |
static long |
addExact(long x, long y)Returns the sum of its arguments, throwing an exception if the result overflows a |
static double |
clamp(double value, double min, double max)This method takes a numerical value and ensures it fits in a given numerical range. |
static float |
clamp(float value, float min, float max)This method takes a numerical value and ensures it fits in a given numerical range. |
static int |
clamp(int value, int min, int max)This method takes a numerical value and ensures it fits in a given numerical range. |
static long |
clamp(long value, long min, long max)This method takes a numerical value and ensures it fits in a given numerical range. |
static int |
decrementExact(int a)Returns the argument decremented by one, throwing an exception if the result overflows an |
static long |
decrementExact(long a)Returns the argument decremented by one, throwing an exception if the result overflows a |
static int |
incrementExact(int a)Returns the argument incremented by one, throwing an exception if the result overflows an |
static long |
incrementExact(long a)Returns the argument incremented by one, throwing an exception if the result overflows a |
static int |
multiplyExact(int x, int y)Returns the product of the arguments, throwing an exception if the result overflows an |
static long |
multiplyExact(long x, long y)Returns the product of the arguments, throwing an exception if the result overflows a |
static int |
negateExact(int a)Returns the negation of the argument, throwing an exception if the result overflows an |
static long |
negateExact(long a)Returns the negation of the argument, throwing an exception if the result overflows a |
static int |
subtractExact(int x, int y)Returns the difference of the arguments, throwing an exception if the result overflows an |
static long |
subtractExact(long x, long y)Returns the difference of the arguments, throwing an exception if the result overflows a |
static int |
toIntExact(long value)Returns the value of the |
Public methods
addExact
public static int addExact(int x, int y)
Returns the sum of its arguments, throwing an exception if the result overflows an int.
| Parameters | |
|---|---|
int x |
the first value |
int y |
the second value |
| Returns | |
|---|---|
int |
the result |
| Throws | |
|---|---|
java.lang.ArithmeticException |
if the result overflows an int |
addExact
public static long addExact(long x, long y)
Returns the sum of its arguments, throwing an exception if the result overflows a long.
| Parameters | |
|---|---|
long x |
the first value |
long y |
the second value |
| Returns | |
|---|---|
long |
the result |
| Throws | |
|---|---|
java.lang.ArithmeticException |
if the result overflows a long |
clamp
public static double clamp(double value, double min, double max)
This method takes a numerical value and ensures it fits in a given numerical range. If the number is smaller than the minimum required by the range, then the minimum of the range will be returned. If the number is higher than the maximum allowed by the range then the maximum of the range will be returned.
| Parameters | |
|---|---|
double value |
the value to be clamped. |
double min |
minimum resulting value. |
double max |
maximum resulting value. |
| Returns | |
|---|---|
double |
the clamped value. |
clamp
public static float clamp(float value, float min, float max)
This method takes a numerical value and ensures it fits in a given numerical range. If the number is smaller than the minimum required by the range, then the minimum of the range will be returned. If the number is higher than the maximum allowed by the range then the maximum of the range will be returned.
| Parameters | |
|---|---|
float value |
the value to be clamped. |
float min |
minimum resulting value. |
float max |
maximum resulting value. |
| Returns | |
|---|---|
float |
the clamped value. |
clamp
public static int clamp(int value, int min, int max)
This method takes a numerical value and ensures it fits in a given numerical range. If the number is smaller than the minimum required by the range, then the minimum of the range will be returned. If the number is higher than the maximum allowed by the range then the maximum of the range will be returned.
| Parameters | |
|---|---|
int value |
the value to be clamped. |
int min |
minimum resulting value. |
int max |
maximum resulting value. |
| Returns | |
|---|---|
int |
the clamped value. |
clamp
public static long clamp(long value, long min, long max)
This method takes a numerical value and ensures it fits in a given numerical range. If the number is smaller than the minimum required by the range, then the minimum of the range will be returned. If the number is higher than the maximum allowed by the range then the maximum of the range will be returned.
| Parameters | |
|---|---|
long value |
the value to be clamped. |
long min |
minimum resulting value. |
long max |
maximum resulting value. |
| Returns | |
|---|---|
long |
the clamped value. |
decrementExact
public static int decrementExact(int a)
Returns the argument decremented by one, throwing an exception if the result overflows an int. The overflow only occurs for the minimum value.
| Parameters | |
|---|---|
int a |
the value to decrement |
| Returns | |
|---|---|
int |
the result |
| Throws | |
|---|---|
java.lang.ArithmeticException |
if the result overflows an int |
decrementExact
public static long decrementExact(long a)
Returns the argument decremented by one, throwing an exception if the result overflows a long. The overflow only occurs for the minimum value.
| Parameters | |
|---|---|
long a |
the value to decrement |
| Returns | |
|---|---|
long |
the result |
| Throws | |
|---|---|
java.lang.ArithmeticException |
if the result overflows a long |
incrementExact
public static int incrementExact(int a)
Returns the argument incremented by one, throwing an exception if the result overflows an int. The overflow only occurs for the maximum value.
| Parameters | |
|---|---|
int a |
the value to increment |
| Returns | |
|---|---|
int |
the result |
| Throws | |
|---|---|
java.lang.ArithmeticException |
if the result overflows an int |
incrementExact
public static long incrementExact(long a)
Returns the argument incremented by one, throwing an exception if the result overflows a long. The overflow only occurs for the maximum value.
| Parameters | |
|---|---|
long a |
the value to increment |
| Returns | |
|---|---|
long |
the result |
| Throws | |
|---|---|
java.lang.ArithmeticException |
if the result overflows a long |
multiplyExact
public static int multiplyExact(int x, int y)
Returns the product of the arguments, throwing an exception if the result overflows an int.
| Parameters | |
|---|---|
int x |
the first value |
int y |
the second value |
| Returns | |
|---|---|
int |
the result |
| Throws | |
|---|---|
java.lang.ArithmeticException |
if the result overflows an int |
multiplyExact
public static long multiplyExact(long x, long y)
Returns the product of the arguments, throwing an exception if the result overflows a long.
| Parameters | |
|---|---|
long x |
the first value |
long y |
the second value |
| Returns | |
|---|---|
long |
the result |
| Throws | |
|---|---|
java.lang.ArithmeticException |
if the result overflows a long |
negateExact
public static int negateExact(int a)
Returns the negation of the argument, throwing an exception if the result overflows an int. The overflow only occurs for the minimum value.
| Parameters | |
|---|---|
int a |
the value to negate |
| Returns | |
|---|---|
int |
the result |
| Throws | |
|---|---|
java.lang.ArithmeticException |
if the result overflows an int |
negateExact
public static long negateExact(long a)
Returns the negation of the argument, throwing an exception if the result overflows a long. The overflow only occurs for the minimum value.
| Parameters | |
|---|---|
long a |
the value to negate |
| Returns | |
|---|---|
long |
the result |
| Throws | |
|---|---|
java.lang.ArithmeticException |
if the result overflows a long |
subtractExact
public static int subtractExact(int x, int y)
Returns the difference of the arguments, throwing an exception if the result overflows an int.
| Parameters | |
|---|---|
int x |
the first value |
int y |
the second value to subtract from the first |
| Returns | |
|---|---|
int |
the result |
| Throws | |
|---|---|
java.lang.ArithmeticException |
if the result overflows an int |
subtractExact
public static long subtractExact(long x, long y)
Returns the difference of the arguments, throwing an exception if the result overflows a long.
| Parameters | |
|---|---|
long x |
the first value |
long y |
the second value to subtract from the first |
| Returns | |
|---|---|
long |
the result |
| Throws | |
|---|---|
java.lang.ArithmeticException |
if the result overflows a long |
toIntExact
public static int toIntExact(long value)
Returns the value of the long argument, throwing an exception if the value overflows an int.
| Parameters | |
|---|---|
long value |
the long value |
| Returns | |
|---|---|
int |
the argument as an int |
| Throws | |
|---|---|
java.lang.ArithmeticException |
if the |