Skip to content

Commit

Permalink
fix: ef parsing for large numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
lbressler13 committed Feb 20, 2024
1 parent 814cec0 commit 7cf68f2
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 126 deletions.
2 changes: 1 addition & 1 deletion exact-numbers/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "xyz.lbres"
version = "1.0.1"
version = "1.0.2"

val githubUsername: String? = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME")
val githubPassword: String? = project.findProperty("gpr.key") as String? ?: System.getenv("TOKEN")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package xyz.lbres.exactnumbers.exactfraction

import xyz.lbres.kotlinutils.general.simpleIf
import xyz.lbres.kotlinutils.general.succeeds
import xyz.lbres.kotlinutils.string.ext.isInt
import java.math.BigDecimal
import java.math.BigInteger

Expand Down Expand Up @@ -87,5 +86,5 @@ internal fun checkIsEFString(s: String): Boolean {
}

val numbers = trimmed.substring(efPrefix.length, s.length - efSuffix.length).split(' ')
return numbers.size == 2 && numbers[0].isInt() && numbers[1].isInt()
return numbers.size == 2 && succeeds { BigInteger(numbers[0]) } && succeeds { BigInteger(numbers[1]) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class ExactFractionConstructorsTest {
assertEquals(BigInteger("-7"), ef.numerator)
assertEquals(BigInteger("3"), ef.denominator)

// large value
ef = ExactFraction("4444444444444444444444444444444444444444444")
assertEquals(BigInteger("4444444444444444444444444444444444444444444"), ef.numerator)
assertEquals(BigInteger.ONE, ef.denominator)

// invalid
assertFailsWithMessage<NumberFormatException>("Error parsing []") { ExactFraction("[]") }
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ fun runParseDecimalTests() {
expected = ExactFraction(BigInteger(n), BigInteger(d))
assertEquals(expected, parseDecimal(s))

s = "4444444444444444444444444444444444444444444"
expected = ExactFraction(BigInteger(s), 1)
assertEquals(expected, parseDecimal(s))

// e-notation
s = "3.90E-3" // 0.00390
expected = ExactFraction(39, 10000)
Expand Down Expand Up @@ -177,6 +181,10 @@ fun runParseEFStringTests() {
expected = ExactFraction(-17, 29)
assertEquals(expected, parseEFString(s))

s = "EF[4444444444444444444444444444444444444444444 1]"
expected = ExactFraction(BigInteger("4444444444444444444444444444444444444444444"), 1)
assertEquals(expected, parseEFString(s))

// errors
s = "EF[1 0]"
assertDivByZero { parseEFString(s) }
Expand Down
2 changes: 1 addition & 1 deletion signatures/v1.0.0/v1.0.0.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*Versions: v1.0.0, v1.0.1*
*Versions: v1.0.0, v1.0.1, v1.0.2*

### xyz.lbres.exactnumbers.exactfraction

Expand Down

0 comments on commit 7cf68f2

Please sign in to comment.