From 0a79029c95a3382ab626ed767a704670e57f6a7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philip=20Dub=C3=A9?= Date: Wed, 3 Apr 2024 20:29:23 +0000 Subject: [PATCH] feedback, fixes bug --- decimal.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/decimal.go b/decimal.go index 247c09b..84b92f1 100644 --- a/decimal.go +++ b/decimal.go @@ -1245,10 +1245,17 @@ func (d Decimal) NumDigits() int { } abs := new(big.Int).Abs(d.value) - // lg10 may be off by 1, need to verify - lg10 := int(float64(abs.BitLen()) / math.Log2(10)) - check := big.NewInt(int64(lg10)) - return lg10 + abs.Cmp(check.Exp(tenInt, check, nil)) + estimatedNumDigits := int(float64(abs.BitLen()) / math.Log2(10)) + + // estimatedNumDigits (lg10) may be off by 1, need to verify + digitsBigInt := big.NewInt(int64(estimatedNumDigits)) + errorCorrectionUnit := digitsBigInt.Exp(tenInt, digitsBigInt, nil) + + if abs.Cmp(errorCorrectionUnit) >= 0 { + return estimatedNumDigits + 1 + } + + return estimatedNumDigits } // IsInteger returns true when decimal can be represented as an integer value, otherwise, it returns false.