mod

inline fun Byte.mod(other: Byte): Byte(source)
inline fun Byte.mod(other: Short): Short(source)
inline fun Byte.mod(other: Int): Int(source)
inline fun Byte.mod(other: Long): Long(source)
inline fun Short.mod(other: Byte): Byte(source)
inline fun Short.mod(other: Short): Short(source)
inline fun Short.mod(other: Int): Int(source)
inline fun Short.mod(other: Long): Long(source)
inline fun Int.mod(other: Byte): Byte(source)
inline fun Int.mod(other: Short): Short(source)
inline fun Int.mod(other: Int): Int(source)
inline fun Int.mod(other: Long): Long(source)
inline fun Long.mod(other: Byte): Byte(source)
inline fun Long.mod(other: Short): Short(source)
inline fun Long.mod(other: Int): Int(source)
inline fun Long.mod(other: Long): Long(source)

Calculates the remainder of flooring division of this value (dividend) by the other value (divisor).

The result is either zero or has the same sign as the divisor and has the absolute value less than the absolute value of the divisor.

Since Kotlin

1.5

Samples

import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   // Regular remainder (%) takes the sign of the dividend
println((-7) % 3) // -1
// mod takes the sign of the divisor
println((-7).mod(3)) // 2
println(7.mod(3)) // 1
println(7.mod(-3)) // -2

// A zero divisor throws, just like the % operator
// 1.mod(0) // will fail with ArithmeticException 
   //sampleEnd
}

inline fun Float.mod(other: Float): Float(source)
inline fun Float.mod(other: Double): Double(source)
inline fun Double.mod(other: Float): Double(source)
inline fun Double.mod(other: Double): Double(source)

Calculates the remainder of flooring division of this value (dividend) by the other value (divisor).

The result is either zero or has the same sign as the divisor and has the absolute value less than the absolute value of the divisor.

If the result cannot be represented exactly, it is rounded to the nearest representable number. In this case the absolute value of the result can be less than or equal to the absolute value of the divisor.

Since Kotlin

1.5

Samples

import kotlin.test.assertFailsWith

fun main() { 
   //sampleStart 
   // For finite arguments, the result has the same sign as the divisor
println((-7.5).mod(3.0)) // 1.5
println(7.5.mod(-3.0)) // -1.5

// The divisor is not required to be an integer
println(5.0.mod(1.1)) // 0.5999999999999996

// A negative-zero dividend keeps its sign
println((-0.0).mod(3.0)) // -0.0

// Unlike integer mod, a zero divisor produces NaN instead of throwing
println(5.0.mod(0.0)) // NaN

// If either argument is NaN, or the dividend is infinite, the result is NaN
println(Double.NaN.mod(3.0)) // NaN
println(Double.POSITIVE_INFINITY.mod(3.0)) // NaN

// If only the divisor is infinite, the result is the dividend when the
// signs agree, and the infinite divisor otherwise
println(3.0.mod(Double.POSITIVE_INFINITY)) // 3.0
println((-3.0).mod(Double.POSITIVE_INFINITY)) // Infinity 
   //sampleEnd
}