-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3cc4f44
commit 0be734c
Showing
8 changed files
with
282 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
exact-numbers/src/main/kotlin/xyz/lbres/exactnumbers/exactfraction/exactFractionHelpers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package xyz.lbres.exactnumbers.exactfraction | ||
|
||
import xyz.lbres.kotlinutils.biginteger.ext.isNegative | ||
import xyz.lbres.kotlinutils.biginteger.ext.isZero | ||
import xyz.lbres.kotlinutils.biginteger.getGCD | ||
import java.math.BigInteger | ||
|
||
/** | ||
* Simplify numerator and denominator values to smallest values with same ratio, and move all negatives into numerator | ||
* | ||
* @param numerator [BigInteger]: numerator of fraction to simplify | ||
* @param denominator [BigInteger]: denominator of fraction to simplify | ||
* @return Pair<BigInteger, BigInteger>: pair where first value represents simplified numerator, and second value represents simplified denominator | ||
*/ | ||
internal fun simplifyFraction(numerator: BigInteger, denominator: BigInteger): Pair<BigInteger, BigInteger> { | ||
// set denominator to 1 when numerator is 0 | ||
if (numerator.isZero()) { | ||
return Pair(BigInteger.ZERO, BigInteger.ONE) | ||
} | ||
|
||
// simplify using greatest common divisor | ||
val gcd = getGCD(numerator, denominator) | ||
val simplifiedNumerator = numerator / gcd | ||
val simplifiedDenominator = denominator / gcd | ||
|
||
// move negatives to numerator | ||
return if (denominator.isNegative()) { | ||
Pair(-simplifiedNumerator, -simplifiedDenominator) | ||
} else { | ||
Pair(simplifiedNumerator, simplifiedDenominator) | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
exact-numbers/src/main/kotlin/xyz/lbres/exactnumbers/exactfraction/exactFractionOperators.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package xyz.lbres.exactnumbers.exactfraction | ||
|
||
import java.math.BigInteger | ||
|
||
/** | ||
* Add two ExactFractions | ||
* | ||
* @param ef1 [ExactFraction] | ||
* @param ef2 [ExactFraction] | ||
* @return [ExactFraction]: sum of [ef1] and [ef2] | ||
*/ | ||
internal fun efAdd(ef1: ExactFraction, ef2: ExactFraction): ExactFraction { | ||
if (ef1.denominator == ef2.denominator) { | ||
val newNumerator = ef1.numerator + ef2.numerator | ||
return ExactFraction(newNumerator, ef1.denominator) | ||
} | ||
|
||
val newNumerator = ef1.numerator * ef2.denominator + ef2.numerator * ef1.denominator | ||
val newDenominator = ef1.denominator * ef2.denominator | ||
return ExactFraction(newNumerator, newDenominator) | ||
} | ||
|
||
/** | ||
* Multiply two ExactFractions | ||
* | ||
* @param ef1 [ExactFraction] | ||
* @param ef2 [ExactFraction] | ||
* @return [ExactFraction]: product of [ef1] and [ef2] | ||
*/ | ||
internal fun efTimes(ef1: ExactFraction, ef2: ExactFraction): ExactFraction { | ||
val newNumerator = ef1.numerator * ef2.numerator | ||
val newDenominator = ef1.denominator * ef2.denominator | ||
return ExactFraction(newNumerator, newDenominator) | ||
} | ||
|
||
/** | ||
* Apply exponent to an ExactFraction | ||
* | ||
* @param base [ExactFraction]: base of exponentiation | ||
* @param exponent [ExactFraction]: exponent, must be a whole number | ||
* @return [ExactFraction] | ||
*/ | ||
internal fun efPow(base: ExactFraction, exponent: ExactFraction): ExactFraction { | ||
// TODO | ||
// if (!exponent.isWholeNumber()) { | ||
if (exponent.denominator != BigInteger.ONE) { | ||
throw ArithmeticException("Exponents must be whole numbers") | ||
} | ||
|
||
when { | ||
base == ExactFraction.ONE || exponent.isZero() -> return ExactFraction.ONE | ||
base.isZero() -> return ExactFraction.ZERO | ||
exponent == ExactFraction.ONE -> return base | ||
} | ||
|
||
var powNumerator = BigInteger.ONE | ||
var powDenominator = BigInteger.ONE | ||
var remaining = exponent.numerator.abs() | ||
val intMax = Int.MAX_VALUE | ||
|
||
try { | ||
while (remaining > BigInteger.ZERO) { | ||
if (remaining > intMax.toBigInteger()) { | ||
powNumerator *= base.numerator.pow(intMax) | ||
powDenominator *= base.denominator.pow(intMax) | ||
remaining -= intMax.toBigInteger() | ||
} else { | ||
val exp = remaining.toInt() | ||
powNumerator = base.numerator.pow(exp) | ||
powDenominator = base.denominator.pow(exp) | ||
remaining = BigInteger.ZERO | ||
} | ||
} | ||
} catch (e: ArithmeticException) { | ||
if (e.message == "BigInteger would overflow supported range") { | ||
throw ExactFractionOverflowException() | ||
} | ||
|
||
throw e | ||
} | ||
|
||
return if (exponent.isNegative()) { | ||
ExactFraction(powDenominator, powNumerator) | ||
} else { | ||
ExactFraction(powNumerator, powDenominator) | ||
} | ||
} | ||
|
||
/** | ||
* Compare two ExactFractions | ||
* | ||
* @param ef1 [ExactFraction] | ||
* @param ef2 [ExactFraction] | ||
* @return [Int]: comparison of [ef1] to [ef2] | ||
*/ | ||
internal fun efCompare(ef1: ExactFraction, ef2: ExactFraction): Int { | ||
val difference = ef1 - ef2 | ||
return when { | ||
difference.isNegative() -> -1 | ||
difference.isZero() -> 0 | ||
else -> 1 | ||
} | ||
} |
Oops, something went wrong.