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) 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) { private fun <T> Ring<T>.powWithoutOptimization(base: T, exponent: ULong): T = when (exponent) {
0UL -> one 0UL -> one
1UL -> base 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: 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 { public operator fun times(other: Int): BigInt = when {
other > 0 -> this * kotlin.math.abs(other).toUInt() other > 0 -> this * kotlin.math.abs(other).toUInt()

View File

@ -26,7 +26,12 @@ internal class BigIntAlgebraTest {
fun testKBigIntegerRingPow() { fun testKBigIntegerRingPow() {
BigIntField { BigIntField {
for (num in -5..5) 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( assertEquals(
num.toDouble().pow(exponent.toInt()).toLong().toBigInt(), num.toDouble().pow(exponent.toInt()).toLong().toBigInt(),
num.toBigInt().pow(exponent), num.toBigInt().pow(exponent),
@ -34,6 +39,7 @@ internal class BigIntAlgebraTest {
) )
} }
} }
}
@Test @Test
fun testKBigIntegerRingSum_100_000_000__100_000_000() { fun testKBigIntegerRingSum_100_000_000__100_000_000() {