MathUtils

public class MathUtils
extends Object

java.lang.Object
   ↳ androidx.core.math.MathUtils


A utility class providing functions useful for common mathematical operations.

Summary

Public methods

static long addExact(long x, long y)

See Math.addExact(long, long).

static int addExact(int x, int y)

See Math.addExact(int, int).

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 long clamp(long value, long min, long 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 decrementExact(long a)

See Math.decrementExact(long).

static int decrementExact(int a)

See Math.decrementExact(int).

static long incrementExact(long a)

See Math.incrementExact(long).

static int incrementExact(int a)

See Math.incrementExact(int).

static long multiplyExact(long x, long y)

See Math.multiplyExact(long, long).

static int multiplyExact(int x, int y)

See Math.multiplyExact(int, int).

static int negateExact(int a)

See Math.negateExact(int).

static long negateExact(long a)

See Math.negateExact(long).

static long subtractExact(long x, long y)

See Math.subtractExact(long, long).

static int subtractExact(int x, int y)

See Math.subtractExact(int, int).

static int toIntExact(long value)

See Math.toIntExact(long).

Inherited methods

Public methods

addExact

public static long addExact (long x, 
                long y)

See Math.addExact(long, long).

Parameters
x long

y long

Returns
long

addExact

public static int addExact (int x, 
                int y)

See Math.addExact(int, int).

Parameters
x int

y int

Returns
int

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
value double: the value to be clamped.

min double: minimum resulting value.

max double: maximum resulting value.

Returns
double 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
value long: the value to be clamped.

min long: minimum resulting value.

max long: maximum resulting value.

Returns
long 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
value float: the value to be clamped.

min float: minimum resulting value.

max float: 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
value int: the value to be clamped.

min int: minimum resulting value.

max int: maximum resulting value.

Returns
int the clamped value.

decrementExact

public static long decrementExact (long a)

See Math.decrementExact(long).

Parameters
a long

Returns
long

decrementExact

public static int decrementExact (int a)

See Math.decrementExact(int).

Parameters
a int

Returns
int

incrementExact

public static long incrementExact (long a)

See Math.incrementExact(long).

Parameters
a long

Returns
long

incrementExact

public static int incrementExact (int a)

See Math.incrementExact(int).

Parameters
a int

Returns
int

multiplyExact

public static long multiplyExact (long x, 
                long y)

See Math.multiplyExact(long, long).

Parameters
x long

y long

Returns
long

multiplyExact

public static int multiplyExact (int x, 
                int y)

See Math.multiplyExact(int, int).

Parameters
x int

y int

Returns
int

negateExact

public static int negateExact (int a)

See Math.negateExact(int).

Parameters
a int

Returns
int

negateExact

public static long negateExact (long a)

See Math.negateExact(long).

Parameters
a long

Returns
long

subtractExact

public static long subtractExact (long x, 
                long y)

See Math.subtractExact(long, long).

Parameters
x long

y long

Returns
long

subtractExact

public static int subtractExact (int x, 
                int y)

See Math.subtractExact(int, int).

Parameters
x int

y int

Returns
int

toIntExact

public static int toIntExact (long value)

See Math.toIntExact(long).

Parameters
value long

Returns
int