Skip to content

Commit

Permalink
Pseudo-operator for exponentiation
Browse files Browse the repository at this point in the history
  • Loading branch information
binkley committed Jun 26, 2020
1 parent db4e01f commit 2c610d6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/main/kotlin/hm/binkley/math/BigRationalBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,10 @@ fun <T : BigRationalBase<T>> T.pow(exponent: Int): T = when {
else -> reciprocal.pow(-exponent)
}

/** Provides a pseudo-operator for exponentiation. */
@Suppress("FunctionName")
infix fun <T : BigRationalBase<T>> T.`**`(exponent: Int) = pow(exponent)

/**
* Returns the rational square root.
*
Expand Down
10 changes: 5 additions & 5 deletions src/test/kotlin/hm/binkley/math/fixed/FixedBigRationalTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package hm.binkley.math.fixed

import hm.binkley.math.BDouble
import hm.binkley.math.BInt
import hm.binkley.math.`**` // ktlint-disable no-wildcard-imports
import hm.binkley.math.ceil
import hm.binkley.math.compareTo
import hm.binkley.math.div
Expand All @@ -21,13 +22,12 @@ import hm.binkley.math.isZero
import hm.binkley.math.lcm
import hm.binkley.math.minus
import hm.binkley.math.plus
import hm.binkley.math.pow
import hm.binkley.math.rangeTo
import hm.binkley.math.rem
import hm.binkley.math.truncate
import hm.binkley.math.sqrt
import hm.binkley.math.step
import hm.binkley.math.times
import hm.binkley.math.truncate
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertNotEquals
Expand Down Expand Up @@ -1085,15 +1085,15 @@ internal class FixedBigRationalTest {
fun `should raise`() {
assertEquals(
9 over 25,
(3 over 5).pow(2)
(3 over 5) `**` 2
)
assertEquals(
ONE,
(3 over 5).pow(0)
(3 over 5) `**` 0
)
assertEquals(
25 over 9,
(3 over 5).pow(-2)
(3 over 5) `**` -2
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package hm.binkley.math.floating

import hm.binkley.math.BDouble
import hm.binkley.math.BInt
import hm.binkley.math.`**` // ktlint-disable no-wildcard-imports
import hm.binkley.math.backAgain
import hm.binkley.math.ceil
import hm.binkley.math.compareTo
Expand All @@ -25,7 +26,6 @@ import hm.binkley.math.isZero
import hm.binkley.math.lcm
import hm.binkley.math.minus
import hm.binkley.math.plus
import hm.binkley.math.pow
import hm.binkley.math.rangeTo
import hm.binkley.math.rem
import hm.binkley.math.sqrt
Expand Down Expand Up @@ -1461,15 +1461,15 @@ internal class FloatingBigRationalTest {
fun `should raise`() {
assertEquals(
9 over 25,
(3 over 5).pow(2)
(3 over 5) `**` 2
)
assertEquals(
ONE,
(3 over 5).pow(0)
(3 over 5) `**` 0
)
assertEquals(
25 over 9,
(3 over 5).pow(-2)
(3 over 5) `**` -2
)
}

Expand Down

0 comments on commit 2c610d6

Please # to comment.