forked from kscience/kmath
UInt can be used in exponent (in pow) now.
This commit is contained in:
parent
1e71f29d5e
commit
8443fac4ae
@ -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
|
||||||
|
@ -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()
|
||||||
|
@ -26,12 +26,18 @@ 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),
|
||||||
"$num ^ $exponent"
|
"$num ^ $exponent"
|
||||||
)
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user