Skip to content

Commit

Permalink
remove copy, don't need Abs(d.value)
Browse files Browse the repository at this point in the history
  • Loading branch information
serprex committed Apr 3, 2024
1 parent d78b46e commit a18188a
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions decimal.go
Expand Up @@ -950,29 +950,23 @@ func (d Decimal) NumDigits() int {
return 1
}

if d.value.IsUint64() {
u64 := d.value.Uint64()
if u64 < (1 << 53) {
if u64 == 0 {
if d.value.IsInt64() {
i64 := d.value.Int64()
if i64 < (1<<53) && i64 > -(1<<53) {
if i64 == 0 {
return 1
}
return int(math.Log10(float64(u64))) + 1
}
} else if d.value.IsInt64() {
i64 := d.value.Int64()
if i64 > -(1 << 53) {
return int(math.Log10(float64(-i64))) + 1
return int(math.Log10(math.Abs(float64(i64)))) + 1
}
}

abs := new(big.Int).Abs(d.value)
estimatedNumDigits := int(float64(abs.BitLen()) / math.Log2(10))
estimatedNumDigits := int(float64(d.value.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 {
if d.value.CmpAbs(errorCorrectionUnit) >= 0 {
return estimatedNumDigits + 1
}

Expand Down

0 comments on commit a18188a

Please sign in to comment.