remove copy, don't need Abs(d.value)

9007199254740992 converts to float64
This commit is contained in:
Philip Dubé 2024-04-03 21:09:19 +00:00
parent 0a79029c95
commit d538aec685

View file

@ -1229,29 +1229,23 @@ func (d Decimal) NumDigits() int {
return 1 return 1
} }
if d.value.IsUint64() { if d.value.IsInt64() {
u64 := d.value.Uint64() i64 := d.value.Int64()
if u64 < (1 << 53) { if i64 <= (1<<53) && i64 >= -(1<<53) {
if u64 == 0 { if i64 == 0 {
return 1 return 1
} }
return int(math.Log10(float64(u64))) + 1 return int(math.Log10(math.Abs(float64(i64)))) + 1
}
} else if d.value.IsInt64() {
i64 := d.value.Int64()
if i64 > -(1 << 53) {
return int(math.Log10(float64(-i64))) + 1
} }
} }
abs := new(big.Int).Abs(d.value) estimatedNumDigits := int(float64(d.value.BitLen()) / math.Log2(10))
estimatedNumDigits := int(float64(abs.BitLen()) / math.Log2(10))
// estimatedNumDigits (lg10) may be off by 1, need to verify // estimatedNumDigits (lg10) may be off by 1, need to verify
digitsBigInt := big.NewInt(int64(estimatedNumDigits)) digitsBigInt := big.NewInt(int64(estimatedNumDigits))
errorCorrectionUnit := digitsBigInt.Exp(tenInt, digitsBigInt, nil) errorCorrectionUnit := digitsBigInt.Exp(tenInt, digitsBigInt, nil)
if abs.Cmp(errorCorrectionUnit) >= 0 { if d.value.CmpAbs(errorCorrectionUnit) >= 0 {
return estimatedNumDigits + 1 return estimatedNumDigits + 1
} }