diff options
author | yenlin.lai <yenlin.lai@cobinhood.com> | 2019-03-29 10:51:32 +0800 |
---|---|---|
committer | Jhih-Ming Huang <jm.huang@cobinhood.com> | 2019-04-11 10:39:59 +0800 |
commit | ae32646fd86f0aa102adb788a4050badc38db918 (patch) | |
tree | 7da53bf9d5627a9f9bcfaa75fcc1fdaf8b9b6498 /core/vm/sqlvm/ast | |
parent | d6fcba4ee624241128e1f086a6b20c3c75bf928c (diff) | |
download | dexon-ae32646fd86f0aa102adb788a4050badc38db918.tar dexon-ae32646fd86f0aa102adb788a4050badc38db918.tar.gz dexon-ae32646fd86f0aa102adb788a4050badc38db918.tar.bz2 dexon-ae32646fd86f0aa102adb788a4050badc38db918.tar.lz dexon-ae32646fd86f0aa102adb788a4050badc38db918.tar.xz dexon-ae32646fd86f0aa102adb788a4050badc38db918.tar.zst dexon-ae32646fd86f0aa102adb788a4050badc38db918.zip |
vm: sqlvm: refine after updating dexon-foundation/decimal
Refine behaviors:
1. Check decimal.NewFromString by type instead of string compare.
2. Use Rescale in DecimalEncode.
Diffstat (limited to 'core/vm/sqlvm/ast')
-rw-r--r-- | core/vm/sqlvm/ast/types.go | 13 | ||||
-rw-r--r-- | core/vm/sqlvm/ast/types_test.go | 2 |
2 files changed, 4 insertions, 11 deletions
diff --git a/core/vm/sqlvm/ast/types.go b/core/vm/sqlvm/ast/types.go index 53f3217a0..975fd81ba 100644 --- a/core/vm/sqlvm/ast/types.go +++ b/core/vm/sqlvm/ast/types.go @@ -15,7 +15,6 @@ import ( var ( bigIntOne = big.NewInt(1) - bigIntTen = big.NewInt(10) ) // BoolValue represents a boolean value used by SQL three-valued logic. @@ -304,15 +303,9 @@ func (dt DataType) GetMinMax() (decimal.Decimal, decimal.Decimal, bool) { return pair.Min, pair.Max, ok } -func decimalToBig(d decimal.Decimal) (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) - } - return +func decimalToBig(d decimal.Decimal) *big.Int { + d = d.Rescale(0) + return d.Coefficient() } // Don't handle overflow here. diff --git a/core/vm/sqlvm/ast/types_test.go b/core/vm/sqlvm/ast/types_test.go index accbbe741..0973ba92a 100644 --- a/core/vm/sqlvm/ast/types_test.go +++ b/core/vm/sqlvm/ast/types_test.go @@ -156,7 +156,7 @@ func (s *TypesTestSuite) TestEncodeAndDecodeDecimal() { } func (s *TypesTestSuite) TestDataTypeGetMinMax() { - decAddressMax := decimal.New(2, 0).Pow(decimal.New(common.AddressLength*8, 0)).Sub(dec.One) + decAddressMax := decimal.Two.Pow(decimal.New(common.AddressLength*8, 0)).Sub(decimal.One) testcases := []struct { Name string In DataType |