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-05-06 10:44:04 +0800 |
commit | c6f123b6f7e343d6cf32521b6aa2a9543a6948dd (patch) | |
tree | c45caf2d8f0f5e9f5b69a7c827ab2f7d08883527 /core/vm/sqlvm/ast | |
parent | cdd653fd663f6550536e10f8b8b701327b2e88c7 (diff) | |
download | dexon-c6f123b6f7e343d6cf32521b6aa2a9543a6948dd.tar dexon-c6f123b6f7e343d6cf32521b6aa2a9543a6948dd.tar.gz dexon-c6f123b6f7e343d6cf32521b6aa2a9543a6948dd.tar.bz2 dexon-c6f123b6f7e343d6cf32521b6aa2a9543a6948dd.tar.lz dexon-c6f123b6f7e343d6cf32521b6aa2a9543a6948dd.tar.xz dexon-c6f123b6f7e343d6cf32521b6aa2a9543a6948dd.tar.zst dexon-c6f123b6f7e343d6cf32521b6aa2a9543a6948dd.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 |