diff options
author | yenlinlai <38415072+yenlinlai@users.noreply.github.com> | 2019-03-18 16:45:03 +0800 |
---|---|---|
committer | Jhih-Ming Huang <jm.huang@cobinhood.com> | 2019-03-26 17:48:22 +0800 |
commit | 063a8f6370f55404709cff25d476d1bba4b6ec48 (patch) | |
tree | 4a6715c467b6973bb795af01cb28fe90e3f1180f /core/vm/sqlvm/ast/types.go | |
parent | 382a5c1b3640e5a17fe1484aa8d650858a710abd (diff) | |
download | dexon-063a8f6370f55404709cff25d476d1bba4b6ec48.tar dexon-063a8f6370f55404709cff25d476d1bba4b6ec48.tar.gz dexon-063a8f6370f55404709cff25d476d1bba4b6ec48.tar.bz2 dexon-063a8f6370f55404709cff25d476d1bba4b6ec48.tar.lz dexon-063a8f6370f55404709cff25d476d1bba4b6ec48.tar.xz dexon-063a8f6370f55404709cff25d476d1bba4b6ec48.tar.zst dexon-063a8f6370f55404709cff25d476d1bba4b6ec48.zip |
core: vm: sqlvm: types: fix encode bug when exponent is negative (#270)
It is possible that the number to encode is produced by arithmetic
operations and has negative exponent even when it is an integer.
Properly handle this case and modify test cases to check it.
Diffstat (limited to 'core/vm/sqlvm/ast/types.go')
-rw-r--r-- | core/vm/sqlvm/ast/types.go | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/core/vm/sqlvm/ast/types.go b/core/vm/sqlvm/ast/types.go index 1928d2338..c006086e9 100644 --- a/core/vm/sqlvm/ast/types.go +++ b/core/vm/sqlvm/ast/types.go @@ -163,8 +163,14 @@ func decimalEncode(size int, d decimal.Decimal) []byte { return ret } - exp := new(big.Int).Exp(bigIntTen, big.NewInt(int64(d.Exponent())), nil) - b := new(big.Int).Mul(d.Coefficient(), exp) + var b *big.Int + if exponent := int64(d.Exponent()); exponent >= 0 { + exp := new(big.Int).Exp(bigIntTen, big.NewInt(exponent), nil) + b = new(big.Int).Mul(d.Coefficient(), exp) + } else { + exp := new(big.Int).Exp(bigIntTen, big.NewInt(-exponent), nil) + b = new(big.Int).Div(d.Coefficient(), exp) + } if s > 0 { bs := b.Bytes() |