feedback, fixes bug

This commit is contained in:
Philip Dubé 2024-04-03 20:29:23 +00:00
parent afe2a66041
commit 0a79029c95

View file

@ -1245,10 +1245,17 @@ func (d Decimal) NumDigits() int {
} }
abs := new(big.Int).Abs(d.value) abs := new(big.Int).Abs(d.value)
// lg10 may be off by 1, need to verify estimatedNumDigits := int(float64(abs.BitLen()) / math.Log2(10))
lg10 := int(float64(abs.BitLen()) / math.Log2(10))
check := big.NewInt(int64(lg10)) // estimatedNumDigits (lg10) may be off by 1, need to verify
return lg10 + abs.Cmp(check.Exp(tenInt, check, nil)) 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. // IsInteger returns true when decimal can be represented as an integer value, otherwise, it returns false.