diff options
author | Dico <dico.karssiens@gmail.com> | 2018-09-27 09:03:28 +0100 |
---|---|---|
committer | Dico <dico.karssiens@gmail.com> | 2018-09-27 09:03:28 +0100 |
commit | 09aaa9ff725787be4f56986d58e61ee41e3140cb (patch) | |
tree | 5f4df15c500194ada3e3b4ab4ccc814894871a89 /src/main/kotlin/io/dico/parcels2/util/math/Math.kt | |
parent | f28e4393db408c44684dd6287cb811a00eeece97 (diff) |
More commandlib tweaks
Diffstat (limited to 'src/main/kotlin/io/dico/parcels2/util/math/Math.kt')
-rw-r--r-- | src/main/kotlin/io/dico/parcels2/util/math/Math.kt | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/main/kotlin/io/dico/parcels2/util/math/Math.kt b/src/main/kotlin/io/dico/parcels2/util/math/Math.kt new file mode 100644 index 0000000..12c3e9f --- /dev/null +++ b/src/main/kotlin/io/dico/parcels2/util/math/Math.kt @@ -0,0 +1,42 @@ +package io.dico.parcels2.util.math + +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 +} + +// the name coerceAtMost is bad +fun Int.clampMax(max: Int) = coerceAtMost(max) +fun Double.clampMin(min: Double) = coerceAtLeast(min) +fun Double.clampMax(max: Double) = coerceAtMost(max) + +// Why does this not exist? +infix fun Int.ceilDiv(divisor: Int): Int { + return -Math.floorDiv(-this, divisor) +}
\ No newline at end of file |