Skip to content

Commit

Permalink
Check if exponent is less than zero or if the divisor is larger than …
Browse files Browse the repository at this point in the history
…divisor, fixes #195
  • Loading branch information
ionspin committed Dec 1, 2021
1 parent d39c8dd commit 3aa137f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions bignum/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ tasks {
val jvmTest by getting(Test::class) {
testLogging {
events("PASSED", "FAILED", "SKIPPED")
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1328,9 +1328,15 @@ class BigDecimal private constructor(

/**
* Quotient and remainder of **integer** division on this bigDecimal. If there is no rounding mode defined in decimal mode
* #RoundingMode.FLOOR will be used
* #RoundingMode.FLOOR will be used. Uses truncating division to determine quotient, which means that the sign of remainder will be same as sign of dividend
*/
override fun divideAndRemainder(other: BigDecimal): Pair<BigDecimal, BigDecimal> {
if (exponent < 0) {
return Pair(ZERO, this)
}
if (other.abs() > this.abs()) {
return Pair(ZERO, this)
}
val resolvedRoundingMode = this.decimalMode ?: DecimalMode(exponent + 1, RoundingMode.FLOOR)
val quotient = divide(other, resolvedRoundingMode)
val quotientInfinitePrecision = quotient.copy(decimalMode = DecimalMode.DEFAULT)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ionspin.kotlin.bignum.decimal

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue

Expand Down Expand Up @@ -105,4 +106,55 @@ class BigDecimalDivisionTest {
val result = a / b
}
}

@Test
fun testDivideAndRemainder() {
run {
val a = 0.14.toBigDecimal()
val b = BigDecimal.ONE
val (quotient, remainder) = a.divideAndRemainder(b)
assertEquals(BigDecimal.ZERO, quotient)
assertEquals(a, remainder)
}

run {
val a = 1.14.toBigDecimal()
val b = BigDecimal.ONE
val (quotient, remainder) = a.divideAndRemainder(b)
assertEquals(BigDecimal.ONE, quotient)
assertEquals(0.14.toBigDecimal(), remainder)
}

run {
val a = 1.toBigDecimal()
val b = BigDecimal.ONE
val (quotient, remainder) = a.divideAndRemainder(b)
assertEquals(BigDecimal.ONE, quotient)
assertEquals(BigDecimal.ZERO, remainder)
}

run {
val a = 1.toBigDecimal()
val b = BigDecimal.TWO
val (quotient, remainder) = a.divideAndRemainder(b)
assertEquals(BigDecimal.ZERO, quotient)
assertEquals(BigDecimal.ONE, remainder)
}

run {
val a = 1.toBigDecimal()
val b = BigDecimal.TWO.negate()
val (quotient, remainder) = a.divideAndRemainder(b)
assertEquals(BigDecimal.ZERO, quotient)
assertEquals(BigDecimal.ONE, remainder)
}

run {
val a = -1.toBigDecimal()
val b = BigDecimal.TWO.negate()
val (quotient, remainder) = a.divideAndRemainder(b)
assertEquals(BigDecimal.ZERO, quotient)
assertEquals(BigDecimal.ONE.negate(), remainder)
}
}
}

0 comments on commit 3aa137f

Please sign in to comment.