Stay organized with collections Save and categorize content based on your preferences.

MathUtils

public class MathUtils


A utility class providing functions useful for common mathematical operations.

Summary

Public methods

static int
addExact(int x, int y)

See addExact.

static long
addExact(long x, long y)

See addExact.

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 double
clamp(double value, double min, double 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

See decrementExact.

static long

See decrementExact.

static int

See incrementExact.

static long

See incrementExact.

static int
multiplyExact(int x, int y)

See multiplyExact.

static long
multiplyExact(long x, long y)

See multiplyExact.

static int
negateExact(int a)

See negateExact.

static long
negateExact(long a)

See negateExact.

static int
subtractExact(int x, int y)

See subtractExact.

static long
subtractExact(long x, long y)

See subtractExact.

static int
toIntExact(long value)

See toIntExact.

Public methods

addExact

public static int addExact(int x, int y)

See addExact.

addExact

public static long addExact(long x, long y)

See addExact.

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 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 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)

See decrementExact.

decrementExact

public static long decrementExact(long a)

See decrementExact.

incrementExact

public static int incrementExact(int a)

See incrementExact.

incrementExact

public static long incrementExact(long a)

See incrementExact.

multiplyExact

public static int multiplyExact(int x, int y)

See multiplyExact.

multiplyExact

public static long multiplyExact(long x, long y)

See multiplyExact.

negateExact

public static int negateExact(int a)

See negateExact.

negateExact

public static long negateExact(long a)

See negateExact.

subtractExact

public static int subtractExact(int x, int y)

See subtractExact.

subtractExact

public static long subtractExact(long x, long y)

See subtractExact.

toIntExact

public static int toIntExact(long value)

See toIntExact.