MathUtils

Added in 1.1.0

class MathUtils


A utility class providing functions useful for common mathematical operations.

Summary

Public functions

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

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

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

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

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

This method takes a numerical value and ensures it fits in a given numerical range.

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

This method takes a numerical value and ensures it fits in a given numerical range.

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

This method takes a numerical value and ensures it fits in a given numerical range.

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

This method takes a numerical value and ensures it fits in a given numerical range.

java-static Int

Returns the argument decremented by one, throwing an exception if the result overflows an int.

java-static Long

Returns the argument decremented by one, throwing an exception if the result overflows a long.

java-static Int

Returns the argument incremented by one, throwing an exception if the result overflows an int.

java-static Long

Returns the argument incremented by one, throwing an exception if the result overflows a long.

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

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

java-static Long

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

java-static Int

Returns the negation of the argument, throwing an exception if the result overflows an int.

java-static Long

Returns the negation of the argument, throwing an exception if the result overflows a long.

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

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

java-static Long

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

java-static Int
toIntExact(value: Long)

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

Public functions

addExact

Added in 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

the first value

y: Int

the second value

Returns
Int

the result

Throws
java.lang.ArithmeticException

if the result overflows an int

addExact

Added in 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

the first value

y: Long

the second value

Returns
Long

the result

Throws
java.lang.ArithmeticException

if the result overflows a long

clamp

Added in 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

the value to be clamped.

min: Double

minimum resulting value.

max: Double

maximum resulting value.

Returns
Double

the clamped value.

clamp

Added in 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

the value to be clamped.

min: Float

minimum resulting value.

max: Float

maximum resulting value.

Returns
Float

the clamped value.

clamp

Added in 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

the value to be clamped.

min: Int

minimum resulting value.

max: Int

maximum resulting value.

Returns
Int

the clamped value.

clamp

Added in 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

the value to be clamped.

min: Long

minimum resulting value.

max: Long

maximum resulting value.

Returns
Long

the clamped value.

decrementExact

Added in 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.

Parameters
a: Int

the value to decrement

Returns
Int

the result

Throws
java.lang.ArithmeticException

if the result overflows an int

decrementExact

Added in 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.

Parameters
a: Long

the value to decrement

Returns
Long

the result

Throws
java.lang.ArithmeticException

if the result overflows a long

incrementExact

Added in 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.

Parameters
a: Int

the value to increment

Returns
Int

the result

Throws
java.lang.ArithmeticException

if the result overflows an int

incrementExact

Added in 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.

Parameters
a: Long

the value to increment

Returns
Long

the result

Throws
java.lang.ArithmeticException

if the result overflows a long

multiplyExact

Added in 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

the first value

y: Int

the second value

Returns
Int

the result

Throws
java.lang.ArithmeticException

if the result overflows an int

multiplyExact

Added in 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

the first value

y: Long

the second value

Returns
Long

the result

Throws
java.lang.ArithmeticException

if the result overflows a long

negateExact

Added in 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.

Parameters
a: Int

the value to negate

Returns
Int

the result

Throws
java.lang.ArithmeticException

if the result overflows an int

negateExact

Added in 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.

Parameters
a: Long

the value to negate

Returns
Long

the result

Throws
java.lang.ArithmeticException

if the result overflows a long

subtractExact

Added in 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

the first value

y: Int

the second value to subtract from the first

Returns
Int

the result

Throws
java.lang.ArithmeticException

if the result overflows an int

subtractExact

Added in 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

the first value

y: Long

the second value to subtract from the first

Returns
Long

the result

Throws
java.lang.ArithmeticException

if the result overflows a long

toIntExact

Added in 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

the long value

Returns
Int

the argument as an int

Throws
java.lang.ArithmeticException

if the argument overflows an int