mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-25 05:40:49 +01:00
feedback, fixes bug
This commit is contained in:
parent
afe2a66041
commit
0a79029c95
1 changed files with 11 additions and 4 deletions
15
decimal.go
15
decimal.go
|
@ -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.
|
||||||
|
|
Loading…
Reference in a new issue