diff options
Diffstat (limited to 'src/main/kotlin/io/dico/parcels2/util/ext/Math.kt')
-rw-r--r-- | src/main/kotlin/io/dico/parcels2/util/ext/Math.kt | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/main/kotlin/io/dico/parcels2/util/ext/Math.kt b/src/main/kotlin/io/dico/parcels2/util/ext/Math.kt new file mode 100644 index 0000000..32540fd --- /dev/null +++ b/src/main/kotlin/io/dico/parcels2/util/ext/Math.kt @@ -0,0 +1,32 @@ +package io.dico.parcels2.util.ext + +fun Double.floor(): Int { + val down = toInt() + if (down.toDouble() != this && (java.lang.Double.doubleToRawLongBits(this).ushr(63).toInt()) == 1) { + return down - 1 + } + return down +} + +infix fun Int.umod(divisor: Int): Int { + val out = this % divisor + if (out < 0) { + return out + divisor + } + return out +} + +val Int.even: Boolean get() = and(1) == 0 + +fun IntRange.clamp(min: Int, max: Int): IntRange { + if (first < min) { + if (last > max) { + return IntRange(min, max) + } + return IntRange(min, last) + } + if (last > max) { + return IntRange(first, max) + } + return this +}
\ No newline at end of file |