mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-25 05:40:49 +01:00
Compare commits
5 commits
dcf2a306fd
...
ceada80758
Author | SHA1 | Date | |
---|---|---|---|
|
ceada80758 | ||
|
d538aec685 | ||
|
0a79029c95 | ||
|
afe2a66041 | ||
|
547861c49b |
1 changed files with 25 additions and 25 deletions
50
decimal.go
50
decimal.go
|
@ -1229,26 +1229,27 @@ 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))
|
||||||
// lg10 may be off by 1, need to verify
|
|
||||||
lg10 := int(float64(abs.BitLen()) / math.Log2(10))
|
// estimatedNumDigits (lg10) may be off by 1, need to verify
|
||||||
check := big.NewInt(int64(lg10))
|
digitsBigInt := big.NewInt(int64(estimatedNumDigits))
|
||||||
return lg10 + abs.Cmp(check.Exp(tenInt, check, nil))
|
errorCorrectionUnit := digitsBigInt.Exp(tenInt, digitsBigInt, nil)
|
||||||
|
|
||||||
|
if d.value.CmpAbs(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.
|
||||||
|
@ -1801,19 +1802,18 @@ func (d *Decimal) UnmarshalBinary(data []byte) error {
|
||||||
|
|
||||||
// MarshalBinary implements the encoding.BinaryMarshaler interface.
|
// MarshalBinary implements the encoding.BinaryMarshaler interface.
|
||||||
func (d Decimal) MarshalBinary() (data []byte, err error) {
|
func (d Decimal) MarshalBinary() (data []byte, err error) {
|
||||||
// Write the exponent first since it's a fixed size
|
// exp is written first, but encode value first to know output size
|
||||||
v1 := make([]byte, 4)
|
var valueData []byte
|
||||||
binary.BigEndian.PutUint32(v1, uint32(d.exp))
|
if valueData, err = d.value.GobEncode(); err != nil {
|
||||||
|
return nil, err
|
||||||
// Add the value
|
|
||||||
var v2 []byte
|
|
||||||
if v2, err = d.value.GobEncode(); err != nil {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Write the exponent in front, since it's a fixed size
|
||||||
|
expData := make([]byte, 4, len(valueData)+4)
|
||||||
|
binary.BigEndian.PutUint32(expData, uint32(d.exp))
|
||||||
|
|
||||||
// Return the byte array
|
// Return the byte array
|
||||||
data = append(v1, v2...)
|
return append(expData, valueData...), nil
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scan implements the sql.Scanner interface for database deserialization.
|
// Scan implements the sql.Scanner interface for database deserialization.
|
||||||
|
|
Loading…
Reference in a new issue