mod
Начиная с Kotlin: 1.5
inline fun Byte.mod(other: Byte): Byte
inline fun Byte.mod(other: Short): Short
inline fun Byte.mod(other: Int): Int
inline fun Byte.mod(other: Long): Long
inline fun Short.mod(other: Byte): Byte
inline fun Short.mod(other: Short): Short
inline fun Short.mod(other: Int): Int
inline fun Short.mod(other: Long): Long
inline fun Int.mod(other: Byte): Byte
inline fun Int.mod(other: Short): Short
inline fun Int.mod(other: Int): Int
inline fun Int.mod(other: Long): Long
inline fun Long.mod(other: Byte): Byte
inline fun Long.mod(other: Short): Short
inline fun Long.mod(other: Int): Int
inline fun Long.mod(other: Long): Long
Вычисляет остаток от деления с округлением вниз этого значения (делимого) на другое значение (делитель).
Результат равен нулю или имеет тот же знак, что и делитель, а его абсолютное значение меньше абсолютного значения делителя.
Примеры
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
inline fun Float.mod(other: Double): Double
inline fun Double.mod(other: Float): Double
inline fun Double.mod(other: Double): Double
Вычисляет остаток от деления с округлением вниз этого значения (делимого) на другое значение (делитель).
Результат равен нулю или имеет тот же знак, что и делитель, а его абсолютное значение меньше абсолютного значения делителя.
Если результат нельзя представить точно, он округляется до ближайшего представимого числа. В этом случае абсолютное значение результата может быть меньше абсолютного значения делителя или равно ему.
Примеры
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
}
© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/mod.html