UInt can be used in exponent (in pow) now.

This commit is contained in:
zhelenskiy 2021-05-12 14:10:32 +03:00
parent 1e71f29d5e
commit 8443fac4ae
3 changed files with 10 additions and 1 deletions

View File

@ -259,6 +259,8 @@ public fun <T> Ring<T>.pow(base: T, exponent: ULong): T = when {
else -> powWithoutOptimization(base, exponent)
}
public fun <T> Ring<T>.pow(base: T, exponent: UInt): T = pow(base, exponent.toULong())
private fun <T> Ring<T>.powWithoutOptimization(base: T, exponent: ULong): T = when (exponent) {
0UL -> one
1UL -> base

View File

@ -99,6 +99,7 @@ public class BigInt internal constructor(
}
public fun pow(other: ULong): BigInt = BigIntField { pow(this@BigInt, other) }
public fun pow(other: UInt): BigInt = BigIntField { pow(this@BigInt, other) }
public operator fun times(other: Int): BigInt = when {
other > 0 -> this * kotlin.math.abs(other).toUInt()

View File

@ -26,12 +26,18 @@ internal class BigIntAlgebraTest {
fun testKBigIntegerRingPow() {
BigIntField {
for (num in -5..5)
for (exponent in 0UL..10UL)
for (exponent in 0U..10U) {
assertEquals(
num.toDouble().pow(exponent.toInt()).toLong().toBigInt(),
num.toBigInt().pow(exponent.toULong()),
"$num ^ $exponent"
)
assertEquals(
num.toDouble().pow(exponent.toInt()).toLong().toBigInt(),
num.toBigInt().pow(exponent),
"$num ^ $exponent"
)
}
}
}