Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Karatsuba added, 2 bugs are fixed #328

Merged
merged 15 commits into from
May 14, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ public fun <T, S> Sequence<T>.averageWith(space: S): T where S : Ring<T>, S : Sc
* @author Evgeniy Zhelenskiy
*/
public fun <T> Ring<T>.power(arg: T, power: UInt): T = when {
this == zero && power > 0U -> zero
this == one -> arg
this == -one -> powWithoutOptimization(arg, power % 2U)
arg == zero && power > 0U -> zero
arg == one -> arg
arg == -one -> powWithoutOptimization(arg, power % 2U)
else -> powWithoutOptimization(arg, power)
}

Expand All @@ -135,7 +135,7 @@ private fun <T> Ring<T>.powWithoutOptimization(base: T, exponent: UInt): T = whe
* @return the base raised to the power.
* @author Iaroslav Postovalov, Evgeniy Zhelenskiy
*/
public fun <T> Field<T>.power(arg: T, power: Int): T = when {
power < 0 -> one / (this as Ring<T>).power(arg, if (power == Int.MIN_VALUE) Int.MAX_VALUE.toUInt().inc() else (-power).toUInt())
else -> (this as Ring<T>).power(arg, power.toUInt())
public fun <T> Field<T>.power(arg: T, power: UInt): T = when {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@altavir You are wrong here: Field can have negative power as Field has division. That was even in the previous implementation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is reverted.

power < 0 -> one / (this as Ring<T>).power(arg, power)
else -> (this as Ring<T>).power(arg, power)
}