From ec89c09a0470c364f288975dae368fa13af06ed1 Mon Sep 17 00:00:00 2001 From: Ugljesa Jovanovic Date: Tue, 13 Apr 2021 19:50:15 +0200 Subject: [PATCH] Return 1 for digit lenght when number is 0 --- .../kotlin/com/ionspin/kotlin/bignum/integer/BigInteger.kt | 3 +++ .../kotlin/bignum/decimal/ReportedIssueReplicationTest.kt | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/BigInteger.kt b/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/BigInteger.kt index 91ebeeca..b5d2af38 100644 --- a/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/BigInteger.kt +++ b/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/integer/BigInteger.kt @@ -516,6 +516,9 @@ class BigInteger internal constructor(wordArray: WordArray, requestedSign: Sign) } override fun numberOfDecimalDigits(): Long { + if (isZero()) { + return 1 + } val bitLenght = arithmetic.bitLength(magnitude) val minDigit = ceil((bitLenght - 1) * LOG_10_OF_2) // val maxDigit = floor(bitLenght * LOG_10_OF_2) + 1 diff --git a/bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/decimal/ReportedIssueReplicationTest.kt b/bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/decimal/ReportedIssueReplicationTest.kt index 176a94f8..e1dace9b 100644 --- a/bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/decimal/ReportedIssueReplicationTest.kt +++ b/bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/decimal/ReportedIssueReplicationTest.kt @@ -257,5 +257,12 @@ class ReportedIssueReplicationTest { val result = a.divide(b, DecimalMode(3, RoundingMode.ROUND_HALF_AWAY_FROM_ZERO, 1)) result == 10.1.toBigDecimal() && result.precision == 3L } + + assertTrue { + val a = 1.toBigDecimal(decimalMode = DecimalMode(3, RoundingMode.ROUND_HALF_AWAY_FROM_ZERO, 4)) + val b = (-1).toBigDecimal(decimalMode = DecimalMode(3, RoundingMode.ROUND_HALF_AWAY_FROM_ZERO, 4)) + val result = a + b + result == BigDecimal.ZERO + } } }