Skip to main content

Android Math Utils

Android's androidx.core.math library contains a utility class related to mathematical operations named MathUtils.

Overflow-Safe Operations​

MathUtils class contains a lot of methods for performing overflow-safe mathematical operations. When an overflow occurs, the methods will throw an exception.

Addition​

To perform safe addition operation, use the addExact() method (available for int and long type):

import android.util.Log
import androidx.core.math.MathUtils

// Unsafe operation
val unsafeResult = Int.MAX_VALUE + 1
Log.i("TAG", unsafeResult.toString()) // Prints "-2147483648"

// Safe operation
try {
MathUtils.addExact(Int.MAX_VALUE, 1) // This will throw exception
} catch (e: Exception) {
Log.i("TAG", "An overlow happens!")
}

Subtraction​

To perform safe subtraction operation, use the subtractExact() method (available for int and long type):

import android.util.Log
import androidx.core.math.MathUtils

// Unsafe operation
var unsafeResult = Int.MIN_VALUE - 1
Log.i("TAG", unsafeResult.toString()) // Prints "2147483647"

// Safe operation
try {
MathUtils.subtractExact(Int.MIN_VALUE, 1) // This will throw exception
} catch (e: Exception) {
Log.i("TAG", "An overlow happens!")
}

Increment​

To perform safe increment operation, use the incrementExact() method (available for int and long type):

import android.util.Log
import androidx.core.math.MathUtils

// Unsafe operation
var unsafeResult = Int.MAX_VALUE + 1
Log.i("TAG", unsafeResult.toString()) // Prints "-2147483648"

// Safe operation
try {
MathUtils.incrementExact(Int.MAX_VALUE) // This will throw exception
} catch (e: Exception) {
Log.i("TAG", "An overlow happens!")
}

Decrement​

To perform safe decrement operation, use the decrementExact() method (available for int and long type):

import android.util.Log
import androidx.core.math.MathUtils

// Unsafe operation
var unsafeResult = Int.MIN_VALUE
unsafeResult--
Log.i("TAG", unsafeResult.toString()) // Prints "2147483647"

// Safe operation
try {
MathUtils.decrementExact(Int.MIN_VALUE) // This will throw exception
} catch (e: Exception) {
Log.i("TAG", "An overlow happens!")
}

Negation​

To perform safe negation operation, use the negateExact() method (available for int and long type):

import android.util.Log
import androidx.core.math.MathUtils

// Unsafe operation
var unsafeResult = -Int.MIN_VALUE
Log.i("TAG", unsafeResult.toString()) // Prints "-2147483648"

// Safe operation
try {
MathUtils.negateExact(Int.MIN_VALUE) // This will throw exception
} catch (e: Exception) {
Log.i("TAG", "An overlow happens!")
}

Multiplication​

To perform safe multiplication operation, use the multiplyExact() method (available for int and long type):

import android.util.Log
import androidx.core.math.MathUtils

// Unsafe operation
var unsafeResult = Int.MAX_VALUE * 2
Log.i("TAG", unsafeResult.toString()) // Prints "-2"

// Safe operation
try {
MathUtils.multiplyExact(Int.MAX_VALUE, 2) // This will throw exception
} catch (e: Exception) {
Log.i("TAG", "An overlow happens!")
}

Long to Int Type Conversion​

To perform safe type conversion from long to int, use the toIntExact() method:

import android.util.Log
import androidx.core.math.MathUtils

// Unsafe operation
var unsafeResult = Long.MAX_VALUE.toInt()
Log.i("TAG", unsafeResult.toString()) // Prints "-1"

// Safe operation
try {
MathUtils.toIntExact(Long.MAX_VALUE) // This will throw exception
} catch (e: Exception) {
Log.i("TAG", "An overlow happens!")
}

Clamping​

MathUtils class also contains methods for clamping a numerical value between a numerical range (available for double, float, int, and long type):

import androidx.core.math.MathUtils

val x = MathUtils.clamp(5, 1, 10) // x == 5
val y = MathUtils.clamp(-1, 1, 10) // y == 1
val z = MathUtils.clamp(12, 1, 10) // z == 10

References​