# MathUtils

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

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/core/math/MathUtils "View this page in Java")

<br />

```
class MathUtils
```

<br />

*** ** * ** ***

A utility class providing functions useful for common mathematical operations.

## Summary

|                                      ### Public functions                                       |
|-------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `java-static `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)       | [addExact](https://developer.android.com/reference/kotlin/androidx/core/math/MathUtils#addExact(int,int))`(x: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`, y: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Returns the sum of its arguments, throwing an exception if the result overflows an`int`.                                                                                                               |
| `java-static `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)     | [addExact](https://developer.android.com/reference/kotlin/androidx/core/math/MathUtils#addExact(long,long))`(x: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`, y: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Returns the sum of its arguments, throwing an exception if the result overflows a`long`.                                                                                                         |
| `java-static `[Double](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-double/index.html) | [clamp](https://developer.android.com/reference/kotlin/androidx/core/math/MathUtils#clamp(double,double,double))`(value: `[Double](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-double/index.html)`, min: `[Double](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-double/index.html)`, max: `[Double](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-double/index.html)`)` This method takes a numerical value and ensures it fits in a given numerical range. |
| `java-static `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)   | [clamp](https://developer.android.com/reference/kotlin/androidx/core/math/MathUtils#clamp(float,float,float))`(value: `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)`, min: `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)`, max: `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)`)` This method takes a numerical value and ensures it fits in a given numerical range.          |
| `java-static `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)       | [clamp](https://developer.android.com/reference/kotlin/androidx/core/math/MathUtils#clamp(int,int,int))`(value: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`, min: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`, max: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` This method takes a numerical value and ensures it fits in a given numerical range.                            |
| `java-static `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)     | [clamp](https://developer.android.com/reference/kotlin/androidx/core/math/MathUtils#clamp(long,long,long))`(value: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`, min: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`, max: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` This method takes a numerical value and ensures it fits in a given numerical range.                   |
| `java-static `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)       | [decrementExact](https://developer.android.com/reference/kotlin/androidx/core/math/MathUtils#decrementExact(int))`(a: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Returns the argument decremented by one, throwing an exception if the result overflows an`int`.                                                                                                                                                                                  |
| `java-static `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)     | [decrementExact](https://developer.android.com/reference/kotlin/androidx/core/math/MathUtils#decrementExact(long))`(a: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Returns the argument decremented by one, throwing an exception if the result overflows a`long`.                                                                                                                                                                               |
| `java-static `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)       | [incrementExact](https://developer.android.com/reference/kotlin/androidx/core/math/MathUtils#incrementExact(int))`(a: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Returns the argument incremented by one, throwing an exception if the result overflows an`int`.                                                                                                                                                                                  |
| `java-static `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)     | [incrementExact](https://developer.android.com/reference/kotlin/androidx/core/math/MathUtils#incrementExact(long))`(a: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Returns the argument incremented by one, throwing an exception if the result overflows a`long`.                                                                                                                                                                               |
| `java-static `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)       | [multiplyExact](https://developer.android.com/reference/kotlin/androidx/core/math/MathUtils#multiplyExact(int,int))`(x: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`, y: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Returns the product of the arguments, throwing an exception if the result overflows an`int`.                                                                                                 |
| `java-static `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)     | [multiplyExact](https://developer.android.com/reference/kotlin/androidx/core/math/MathUtils#multiplyExact(long,long))`(x: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`, y: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Returns the product of the arguments, throwing an exception if the result overflows a`long`.                                                                                           |
| `java-static `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)       | [negateExact](https://developer.android.com/reference/kotlin/androidx/core/math/MathUtils#negateExact(int))`(a: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Returns the negation of the argument, throwing an exception if the result overflows an`int`.                                                                                                                                                                                           |
| `java-static `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)     | [negateExact](https://developer.android.com/reference/kotlin/androidx/core/math/MathUtils#negateExact(long))`(a: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Returns the negation of the argument, throwing an exception if the result overflows a`long`.                                                                                                                                                                                        |
| `java-static `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)       | [subtractExact](https://developer.android.com/reference/kotlin/androidx/core/math/MathUtils#subtractExact(int,int))`(x: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`, y: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Returns the difference of the arguments, throwing an exception if the result overflows an`int`.                                                                                              |
| `java-static `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)     | [subtractExact](https://developer.android.com/reference/kotlin/androidx/core/math/MathUtils#subtractExact(long,long))`(x: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`, y: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Returns the difference of the arguments, throwing an exception if the result overflows a`long`.                                                                                        |
| `java-static `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)       | [toIntExact](https://developer.android.com/reference/kotlin/androidx/core/math/MathUtils#toIntExact(long))`(value: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Returns the value of the`long`argument, throwing an exception if the value overflows an`int`.                                                                                                                                                                                     |

## Public functions

### addExact

Added in[1.7.0](https://developer.android.com/jetpack/androidx/releases/core#1.7.0)  

```
java-static fun addExact(x: Int, y: Int): Int
```

Returns the sum of its arguments, throwing an exception if the result overflows an`int`.  

|                                    Parameters                                    |
|----------------------------------------------------------------------------------|------------------|
| `x: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | the first value  |
| `y: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | the second value |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | the result |

|                                                   Throws                                                    |
|-------------------------------------------------------------------------------------------------------------|--------------------------------|
| [java.lang.ArithmeticException](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)  

```
java-static fun addExact(x: Long, y: Long): Long
```

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

|                                     Parameters                                     |
|------------------------------------------------------------------------------------|------------------|
| `x: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | the first value  |
| `y: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | the second value |

|                                    Returns                                    |
|-------------------------------------------------------------------------------|------------|
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | the result |

|                                                   Throws                                                    |
|-------------------------------------------------------------------------------------------------------------|--------------------------------|
| [java.lang.ArithmeticException](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)  

```
java-static fun clamp(value: Double, min: Double, max: Double): Double
```

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](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-double/index.html) | the value to be clamped. |
| `min: `[Double](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-double/index.html)   | minimum resulting value. |
| `max: `[Double](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-double/index.html)   | maximum resulting value. |

|                                      Returns                                      |
|-----------------------------------------------------------------------------------|--------------------|
| [Double](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-double/index.html) | the clamped value. |

### clamp

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

```
java-static fun clamp(value: Float, min: Float, max: Float): Float
```

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](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html) | the value to be clamped. |
| `min: `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)   | minimum resulting value. |
| `max: `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)   | maximum resulting value. |

|                                     Returns                                     |
|---------------------------------------------------------------------------------|--------------------|
| [Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html) | the clamped value. |

### clamp

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

```
java-static fun clamp(value: Int, min: Int, max: Int): Int
```

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](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | the value to be clamped. |
| `min: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)   | minimum resulting value. |
| `max: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)   | maximum resulting value. |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|--------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | the clamped value. |

### clamp

Added in[1.2.0](https://developer.android.com/jetpack/androidx/releases/core#1.2.0)  

```
java-static fun clamp(value: Long, min: Long, max: Long): Long
```

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](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | the value to be clamped. |
| `min: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)   | minimum resulting value. |
| `max: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)   | maximum resulting value. |

|                                    Returns                                    |
|-------------------------------------------------------------------------------|--------------------|
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | the clamped value. |

### decrementExact

Added in[1.7.0](https://developer.android.com/jetpack/androidx/releases/core#1.7.0)  

```
java-static fun decrementExact(a: Int): Int
```

Returns the argument decremented by one, throwing an exception if the result overflows an`int`. The overflow only occurs for[the minimum value](https://developer.android.com/reference/java/lang/Integer.html#MIN_VALUE()).  

|                                    Parameters                                    |
|----------------------------------------------------------------------------------|------------------------|
| `a: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | the value to decrement |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | the result |

|                                                   Throws                                                    |
|-------------------------------------------------------------------------------------------------------------|--------------------------------|
| [java.lang.ArithmeticException](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)  

```
java-static fun decrementExact(a: Long): Long
```

Returns the argument decremented by one, throwing an exception if the result overflows a`long`. The overflow only occurs for[the minimum value](https://developer.android.com/reference/java/lang/Long.html#MIN_VALUE()).  

|                                     Parameters                                     |
|------------------------------------------------------------------------------------|------------------------|
| `a: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | the value to decrement |

|                                    Returns                                    |
|-------------------------------------------------------------------------------|------------|
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | the result |

|                                                   Throws                                                    |
|-------------------------------------------------------------------------------------------------------------|--------------------------------|
| [java.lang.ArithmeticException](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)  

```
java-static fun incrementExact(a: Int): Int
```

Returns the argument incremented by one, throwing an exception if the result overflows an`int`. The overflow only occurs for[the maximum value](https://developer.android.com/reference/java/lang/Integer.html#MAX_VALUE()).  

|                                    Parameters                                    |
|----------------------------------------------------------------------------------|------------------------|
| `a: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | the value to increment |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | the result |

|                                                   Throws                                                    |
|-------------------------------------------------------------------------------------------------------------|--------------------------------|
| [java.lang.ArithmeticException](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)  

```
java-static fun incrementExact(a: Long): Long
```

Returns the argument incremented by one, throwing an exception if the result overflows a`long`. The overflow only occurs for[the maximum value](https://developer.android.com/reference/java/lang/Long.html#MAX_VALUE()).  

|                                     Parameters                                     |
|------------------------------------------------------------------------------------|------------------------|
| `a: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | the value to increment |

|                                    Returns                                    |
|-------------------------------------------------------------------------------|------------|
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | the result |

|                                                   Throws                                                    |
|-------------------------------------------------------------------------------------------------------------|--------------------------------|
| [java.lang.ArithmeticException](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)  

```
java-static fun multiplyExact(x: Int, y: Int): Int
```

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

|                                    Parameters                                    |
|----------------------------------------------------------------------------------|------------------|
| `x: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | the first value  |
| `y: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | the second value |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | the result |

|                                                   Throws                                                    |
|-------------------------------------------------------------------------------------------------------------|--------------------------------|
| [java.lang.ArithmeticException](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)  

```
java-static fun multiplyExact(x: Long, y: Long): Long
```

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

|                                     Parameters                                     |
|------------------------------------------------------------------------------------|------------------|
| `x: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | the first value  |
| `y: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | the second value |

|                                    Returns                                    |
|-------------------------------------------------------------------------------|------------|
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | the result |

|                                                   Throws                                                    |
|-------------------------------------------------------------------------------------------------------------|--------------------------------|
| [java.lang.ArithmeticException](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)  

```
java-static fun negateExact(a: Int): Int
```

Returns the negation of the argument, throwing an exception if the result overflows an`int`. The overflow only occurs for[the minimum value](https://developer.android.com/reference/java/lang/Integer.html#MIN_VALUE()).  

|                                    Parameters                                    |
|----------------------------------------------------------------------------------|---------------------|
| `a: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | the value to negate |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | the result |

|                                                   Throws                                                    |
|-------------------------------------------------------------------------------------------------------------|--------------------------------|
| [java.lang.ArithmeticException](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)  

```
java-static fun negateExact(a: Long): Long
```

Returns the negation of the argument, throwing an exception if the result overflows a`long`. The overflow only occurs for[the minimum value](https://developer.android.com/reference/java/lang/Long.html#MIN_VALUE()).  

|                                     Parameters                                     |
|------------------------------------------------------------------------------------|---------------------|
| `a: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | the value to negate |

|                                    Returns                                    |
|-------------------------------------------------------------------------------|------------|
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | the result |

|                                                   Throws                                                    |
|-------------------------------------------------------------------------------------------------------------|--------------------------------|
| [java.lang.ArithmeticException](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)  

```
java-static fun subtractExact(x: Int, y: Int): Int
```

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

|                                    Parameters                                    |
|----------------------------------------------------------------------------------|---------------------------------------------|
| `x: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | the first value                             |
| `y: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | the second value to subtract from the first |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | the result |

|                                                   Throws                                                    |
|-------------------------------------------------------------------------------------------------------------|--------------------------------|
| [java.lang.ArithmeticException](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)  

```
java-static fun subtractExact(x: Long, y: Long): Long
```

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

|                                     Parameters                                     |
|------------------------------------------------------------------------------------|---------------------------------------------|
| `x: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | the first value                             |
| `y: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | the second value to subtract from the first |

|                                    Returns                                    |
|-------------------------------------------------------------------------------|------------|
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | the result |

|                                                   Throws                                                    |
|-------------------------------------------------------------------------------------------------------------|--------------------------------|
| [java.lang.ArithmeticException](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)  

```
java-static fun toIntExact(value: Long): Int
```

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

|                                       Parameters                                       |
|----------------------------------------------------------------------------------------|----------------|
| `value: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | the long value |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | the argument as an int |

|                                                   Throws                                                    |
|-------------------------------------------------------------------------------------------------------------|----------------------------------|
| [java.lang.ArithmeticException](https://developer.android.com/reference/java/lang/ArithmeticException.html) | if the`argument`overflows an int |