# MathUtils

Artifact: [androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/math/MathUtils.java+class:androidx.core.math.MathUtils) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

*** ** * ** ***


```
public class MathUtils
```

<br />

*** ** * ** ***

A utility class providing functions useful for common mathematical operations.

## Summary

| ### Public methods |
|---|---|
| `static int` | `https://developer.android.com/reference/androidx/core/math/MathUtils#addExact(int,int)(int x, int y)` Returns the sum of its arguments, throwing an exception if the result overflows an `int`. |
| `static long` | `https://developer.android.com/reference/androidx/core/math/MathUtils#addExact(long,long)(long x, long y)` Returns the sum of its arguments, throwing an exception if the result overflows a `long`. |
| `static double` | `https://developer.android.com/reference/androidx/core/math/MathUtils#clamp(double,double,double)(double value, double min, double max)` This method takes a numerical value and ensures it fits in a given numerical range. |
| `static float` | `https://developer.android.com/reference/androidx/core/math/MathUtils#clamp(float,float,float)(float value, float min, float max)` This method takes a numerical value and ensures it fits in a given numerical range. |
| `static int` | `https://developer.android.com/reference/androidx/core/math/MathUtils#clamp(int,int,int)(int value, int min, int max)` This method takes a numerical value and ensures it fits in a given numerical range. |
| `static long` | `https://developer.android.com/reference/androidx/core/math/MathUtils#clamp(long,long,long)(long value, long min, long max)` This method takes a numerical value and ensures it fits in a given numerical range. |
| `static int` | `https://developer.android.com/reference/androidx/core/math/MathUtils#decrementExact(int)(int a)` Returns the argument decremented by one, throwing an exception if the result overflows an `int`. |
| `static long` | `https://developer.android.com/reference/androidx/core/math/MathUtils#decrementExact(long)(long a)` Returns the argument decremented by one, throwing an exception if the result overflows a `long`. |
| `static int` | `https://developer.android.com/reference/androidx/core/math/MathUtils#incrementExact(int)(int a)` Returns the argument incremented by one, throwing an exception if the result overflows an `int`. |
| `static long` | `https://developer.android.com/reference/androidx/core/math/MathUtils#incrementExact(long)(long a)` Returns the argument incremented by one, throwing an exception if the result overflows a `long`. |
| `static int` | `https://developer.android.com/reference/androidx/core/math/MathUtils#multiplyExact(int,int)(int x, int y)` Returns the product of the arguments, throwing an exception if the result overflows an `int`. |
| `static long` | `https://developer.android.com/reference/androidx/core/math/MathUtils#multiplyExact(long,long)(long x, long y)` Returns the product of the arguments, throwing an exception if the result overflows a `long`. |
| `static int` | `https://developer.android.com/reference/androidx/core/math/MathUtils#negateExact(int)(int a)` Returns the negation of the argument, throwing an exception if the result overflows an `int`. |
| `static long` | `https://developer.android.com/reference/androidx/core/math/MathUtils#negateExact(long)(long a)` Returns the negation of the argument, throwing an exception if the result overflows a `long`. |
| `static int` | `https://developer.android.com/reference/androidx/core/math/MathUtils#subtractExact(int,int)(int x, int y)` Returns the difference of the arguments, throwing an exception if the result overflows an `int`. |
| `static long` | `https://developer.android.com/reference/androidx/core/math/MathUtils#subtractExact(long,long)(long x, long y)` Returns the difference of the arguments, throwing an exception if the result overflows a `long`. |
| `static int` | `https://developer.android.com/reference/androidx/core/math/MathUtils#toIntExact(long)(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](https://developer.android.com/jetpack/androidx/releases/core#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 |
|---|---|
| `https://developer.android.com/reference/java/lang/ArithmeticException.html` | if the result overflows an int |

### addExact

Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/core#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 |
|---|---|
| `https://developer.android.com/reference/java/lang/ArithmeticException.html` | if the result overflows a long |

### clamp

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#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](https://developer.android.com/jetpack/androidx/releases/core#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](https://developer.android.com/jetpack/androidx/releases/core#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](https://developer.android.com/jetpack/androidx/releases/core#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](https://developer.android.com/jetpack/androidx/releases/core#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 `https://developer.android.com/reference/java/lang/Integer.html#MIN_VALUE`.

| Parameters |
|---|---|
| `int a` | the value to decrement |

| Returns |
|---|---|
| `int` | the result |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/ArithmeticException.html` | if the result overflows an int |

### decrementExact

Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/core#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 `https://developer.android.com/reference/java/lang/Long.html#MIN_VALUE`.

| Parameters |
|---|---|
| `long a` | the value to decrement |

| Returns |
|---|---|
| `long` | the result |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/ArithmeticException.html` | if the result overflows a long |

### incrementExact

Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/core#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 `https://developer.android.com/reference/java/lang/Integer.html#MAX_VALUE`.

| Parameters |
|---|---|
| `int a` | the value to increment |

| Returns |
|---|---|
| `int` | the result |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/ArithmeticException.html` | if the result overflows an int |

### incrementExact

Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/core#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 `https://developer.android.com/reference/java/lang/Long.html#MAX_VALUE`.

| Parameters |
|---|---|
| `long a` | the value to increment |

| Returns |
|---|---|
| `long` | the result |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/ArithmeticException.html` | if the result overflows a long |

### multiplyExact

Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/core#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 |
|---|---|
| `https://developer.android.com/reference/java/lang/ArithmeticException.html` | if the result overflows an int |

### multiplyExact

Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/core#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 |
|---|---|
| `https://developer.android.com/reference/java/lang/ArithmeticException.html` | if the result overflows a long |

### negateExact

Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/core#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 `https://developer.android.com/reference/java/lang/Integer.html#MIN_VALUE`.

| Parameters |
|---|---|
| `int a` | the value to negate |

| Returns |
|---|---|
| `int` | the result |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/ArithmeticException.html` | if the result overflows an int |

### negateExact

Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/core#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 `https://developer.android.com/reference/java/lang/Long.html#MIN_VALUE`.

| Parameters |
|---|---|
| `long a` | the value to negate |

| Returns |
|---|---|
| `long` | the result |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/ArithmeticException.html` | if the result overflows a long |

### subtractExact

Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/core#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 |
|---|---|
| `https://developer.android.com/reference/java/lang/ArithmeticException.html` | if the result overflows an int |

### subtractExact

Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/core#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 |
|---|---|
| `https://developer.android.com/reference/java/lang/ArithmeticException.html` | if the result overflows a long |

### toIntExact

Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/core#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 |
|---|---|
| `https://developer.android.com/reference/java/lang/ArithmeticException.html` | if the `argument` overflows an int |