September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,43 @@
// version 1.0.6
infix fun Int.ipow(exp: Int): Int =
when {
this == 1 -> 1
this == -1 -> if (exp % 2 == 0) 1 else -1
exp < 0 -> throw IllegalArgumentException("invalid exponent")
exp == 0 -> 1
else -> {
var ans = 1
var base = this
var e = exp
while (e > 0) {
if (e and 1 == 1) ans *= base
e = e shr 1
base *= base
}
ans
}
}
infix fun Double.dpow(exp: Int): Double {
var ans = 1.0
var e = exp
var base = if (e < 0) 1.0 / this else this
if (e < 0) e = -e
while (e > 0) {
if (e and 1 == 1) ans *= base
e = e shr 1
base *= base
}
return ans
}
fun main(args: Array<String>) {
println("2 ^ 3 = ${2 ipow 3}")
println("1 ^ -10 = ${1 ipow -10}")
println("-1 ^ -3 = ${-1 ipow -3}")
println()
println("2.0 ^ -3 = ${2.0 dpow -3}")
println("1.5 ^ 0 = ${1.5 dpow 0}")
println("4.5 ^ 2 = ${4.5 dpow 2}")
}