MathUtils

Added in 1.1.0

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 int.

static long
addExact(long x, long y)

Returns the sum of its arguments, throwing an exception if the result overflows a long.

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

Returns the argument decremented by one, throwing an exception if the result overflows an int.

static long

Returns the argument decremented by one, throwing an exception if the result overflows a long.

static int

Returns the argument incremented by one, throwing an exception if the result overflows an int.

static long

Returns the argument incremented by one, throwing an exception if the result overflows a long.

static int
multiplyExact(int x, int y)

Returns the product of the arguments, throwing an exception if the result overflows an int.

static long
multiplyExact(long x, long y)

Returns the product of the arguments, throwing an exception if the result overflows a long.

static int
negateExact(int a)

Returns the negation of the argument, throwing an exception if the result overflows an int.

static long
negateExact(long a)

Returns the negation of the argument, throwing an exception if the result overflows a long.

static int
subtractExact(int x, int y)

Returns the difference of the arguments, throwing an exception if the result overflows an int.

static long
subtractExact(long x, long y)

Returns the difference of the arguments, throwing an exception if the result overflows a long.

static int
toIntExact(long value)

Returns the value of the long argument, throwing an exception if the value overflows an int.

Public methods

addExact

Added in 1.7.0
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

Added in 1.7.0
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

Added in 1.1.0
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

Added in 1.1.0
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

Added in 1.1.0
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

Added in 1.2.0
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

Added in 1.7.0
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

Added in 1.7.0
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

Added in 1.7.0
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

Added in 1.7.0
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

Added in 1.7.0
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

Added in 1.7.0
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

Added in 1.7.0
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

Added in 1.7.0
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

Added in 1.7.0
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

Added in 1.7.0
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

Added in 1.7.0
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 argument overflows an int