diff options
author | yenlin.lai <yenlin.lai@cobinhood.com> | 2019-03-21 16:20:57 +0800 |
---|---|---|
committer | yenlinlai <38415072+yenlinlai@users.noreply.github.com> | 2019-04-03 15:09:02 +0800 |
commit | f5edd6155c5f3d8dfcd316f6f165998c01f52f43 (patch) | |
tree | 4501deeb07002fe53116e16404dd188ce7593db4 | |
parent | 8819f2e5d2cd694dc08e8dd809e15d9044c07d39 (diff) | |
download | dexon-f5edd6155c5f3d8dfcd316f6f165998c01f52f43.tar dexon-f5edd6155c5f3d8dfcd316f6f165998c01f52f43.tar.gz dexon-f5edd6155c5f3d8dfcd316f6f165998c01f52f43.tar.bz2 dexon-f5edd6155c5f3d8dfcd316f6f165998c01f52f43.tar.lz dexon-f5edd6155c5f3d8dfcd316f6f165998c01f52f43.tar.xz dexon-f5edd6155c5f3d8dfcd316f6f165998c01f52f43.tar.zst dexon-f5edd6155c5f3d8dfcd316f6f165998c01f52f43.zip |
misc: replace shopspring/decimal with our fork
17 files changed, 318 insertions, 240 deletions
diff --git a/core/vm/sqlvm/ast/ast.go b/core/vm/sqlvm/ast/ast.go index d5ebb35af..a2f0748f5 100644 --- a/core/vm/sqlvm/ast/ast.go +++ b/core/vm/sqlvm/ast/ast.go @@ -3,8 +3,9 @@ package ast import ( "fmt" + "github.com/dexon-foundation/decimal" + "github.com/dexon-foundation/dexon/core/vm/sqlvm/errors" - "github.com/shopspring/decimal" ) // --------------------------------------------------------------------------- diff --git a/core/vm/sqlvm/ast/types.go b/core/vm/sqlvm/ast/types.go index ee8036576..53f3217a0 100644 --- a/core/vm/sqlvm/ast/types.go +++ b/core/vm/sqlvm/ast/types.go @@ -6,7 +6,7 @@ import ( "math" "math/big" - "github.com/shopspring/decimal" + "github.com/dexon-foundation/decimal" "github.com/dexon-foundation/dexon/common" dec "github.com/dexon-foundation/dexon/core/vm/sqlvm/common/decimal" diff --git a/core/vm/sqlvm/ast/types_test.go b/core/vm/sqlvm/ast/types_test.go index d2051c4b5..accbbe741 100644 --- a/core/vm/sqlvm/ast/types_test.go +++ b/core/vm/sqlvm/ast/types_test.go @@ -4,7 +4,7 @@ import ( "database/sql" "testing" - "github.com/shopspring/decimal" + "github.com/dexon-foundation/decimal" "github.com/stretchr/testify/suite" "github.com/dexon-foundation/dexon/common" diff --git a/core/vm/sqlvm/common/decimal/decimal.go b/core/vm/sqlvm/common/decimal/decimal.go index 0c3d5971a..b02a8ed23 100644 --- a/core/vm/sqlvm/common/decimal/decimal.go +++ b/core/vm/sqlvm/common/decimal/decimal.go @@ -1,6 +1,6 @@ package decimal -import "github.com/shopspring/decimal" +import "github.com/dexon-foundation/decimal" // Shared vars. var ( diff --git a/core/vm/sqlvm/common/storage.go b/core/vm/sqlvm/common/storage.go index 674efbf17..22ef85885 100644 --- a/core/vm/sqlvm/common/storage.go +++ b/core/vm/sqlvm/common/storage.go @@ -3,7 +3,7 @@ package common import ( "math/big" - "github.com/shopspring/decimal" + "github.com/dexon-foundation/decimal" "golang.org/x/crypto/sha3" "github.com/dexon-foundation/dexon/common" diff --git a/core/vm/sqlvm/parser/internal/helpers.go b/core/vm/sqlvm/parser/internal/helpers.go index 38033fcf0..386a4be7e 100644 --- a/core/vm/sqlvm/parser/internal/helpers.go +++ b/core/vm/sqlvm/parser/internal/helpers.go @@ -8,10 +8,11 @@ import ( "strings" "unicode/utf8" + "github.com/dexon-foundation/decimal" + "github.com/dexon-foundation/dexon/common" "github.com/dexon-foundation/dexon/core/vm/sqlvm/ast" "github.com/dexon-foundation/dexon/core/vm/sqlvm/errors" - "github.com/shopspring/decimal" ) // Parser was generated with pigeon v1.0.0-99-gbb0192c. diff --git a/core/vm/sqlvm/runtime/instructions.go b/core/vm/sqlvm/runtime/instructions.go index 17691d0d4..a1c992988 100644 --- a/core/vm/sqlvm/runtime/instructions.go +++ b/core/vm/sqlvm/runtime/instructions.go @@ -4,7 +4,7 @@ import ( "fmt" "strings" - "github.com/shopspring/decimal" + "github.com/dexon-foundation/decimal" dexCommon "github.com/dexon-foundation/dexon/common" "github.com/dexon-foundation/dexon/core/vm/sqlvm/ast" diff --git a/core/vm/sqlvm/runtime/instructions_test.go b/core/vm/sqlvm/runtime/instructions_test.go index 4d2e0b153..2601fcce3 100644 --- a/core/vm/sqlvm/runtime/instructions_test.go +++ b/core/vm/sqlvm/runtime/instructions_test.go @@ -6,7 +6,7 @@ import ( "reflect" "testing" - "github.com/shopspring/decimal" + "github.com/dexon-foundation/decimal" "github.com/stretchr/testify/suite" dexCommon "github.com/dexon-foundation/dexon/common" diff --git a/core/vm/sqlvm/schema/schema.go b/core/vm/sqlvm/schema/schema.go index d50a3ff46..708bbf672 100644 --- a/core/vm/sqlvm/schema/schema.go +++ b/core/vm/sqlvm/schema/schema.go @@ -4,7 +4,7 @@ import ( "errors" "io" - "github.com/shopspring/decimal" + "github.com/dexon-foundation/decimal" "github.com/dexon-foundation/dexon/common" "github.com/dexon-foundation/dexon/core/vm/sqlvm/ast" diff --git a/core/vm/sqlvm/schema/schema_test.go b/core/vm/sqlvm/schema/schema_test.go index 71f0ee6d3..37fa90bb0 100644 --- a/core/vm/sqlvm/schema/schema_test.go +++ b/core/vm/sqlvm/schema/schema_test.go @@ -7,7 +7,7 @@ import ( "reflect" "testing" - "github.com/shopspring/decimal" + "github.com/dexon-foundation/decimal" "github.com/stretchr/testify/suite" "github.com/dexon-foundation/dexon/core/vm/sqlvm/ast" diff --git a/vendor/github.com/shopspring/decimal/LICENSE b/vendor/github.com/dexon-foundation/decimal/LICENSE index ad2148aaf..0a794ae74 100644 --- a/vendor/github.com/shopspring/decimal/LICENSE +++ b/vendor/github.com/dexon-foundation/decimal/LICENSE @@ -1,6 +1,7 @@ The MIT License (MIT) Copyright (c) 2015 Spring, Inc. +Copyright (c) 2019 DEXON Foundation. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/vendor/github.com/shopspring/decimal/README.md b/vendor/github.com/dexon-foundation/decimal/README.md index 54c1a6b4a..ef7358bb4 100644 --- a/vendor/github.com/shopspring/decimal/README.md +++ b/vendor/github.com/dexon-foundation/decimal/README.md @@ -1,11 +1,15 @@ # decimal -[![Build Status](https://travis-ci.org/shopspring/decimal.png?branch=master)](https://travis-ci.org/shopspring/decimal) [![GoDoc](https://godoc.org/github.com/shopspring/decimal?status.svg)](https://godoc.org/github.com/shopspring/decimal) [![Go Report Card](https://goreportcard.com/badge/github.com/shopspring/decimal)](https://goreportcard.com/report/github.com/shopspring/decimal) +[![Build Status](https://travis-ci.com/dexon-foundation/decimal.png?branch=master)](https://travis-ci.com/dexon-foundation/decimal) [![GoDoc](https://godoc.org/github.com/dexon-foundation/decimal?status.svg)](https://godoc.org/github.com/dexon-foundation/decimal) [![Go Report Card](https://goreportcard.com/badge/github.com/dexon-foundation/decimal)](https://goreportcard.com/report/github.com/dexon-foundation/decimal) Arbitrary-precision fixed-point decimal numbers in go. NOTE: can "only" represent numbers with a maximum of 2^31 digits after the decimal point. +## THIS IS UNSTABLE BRANCH +master branch is under development. API changes can be introduced any time. If +you are seeking a stable version, please access v1.x tags. + ## Features * the zero-value is 0, and is safe to use without initialization @@ -16,7 +20,7 @@ NOTE: can "only" represent numbers with a maximum of 2^31 digits after the decim ## Install -Run `go get github.com/shopspring/decimal` +Run `go get github.com/dexon-foundation/decimal` ## Usage @@ -25,7 +29,7 @@ package main import ( "fmt" - "github.com/shopspring/decimal" + "github.com/dexon-foundation/decimal" ) func main() { @@ -55,12 +59,7 @@ func main() { ## Documentation -http://godoc.org/github.com/shopspring/decimal - -## Production Usage - -* [Spring](https://shopspring.com/), since August 14, 2014. -* If you are using this in production, please let us know! +https://godoc.org/github.com/dexon-foundation/decimal ## FAQ @@ -69,7 +68,7 @@ http://godoc.org/github.com/shopspring/decimal Because float64s (or any binary floating point type, actually) can't represent numbers such as 0.1 exactly. -Consider this code: http://play.golang.org/p/TQBd4yJe6B You might expect that +Consider this code: https://play.golang.org/p/TQBd4yJe6B You might expect that it prints out `10`, but it actually prints `9.999999999999831`. Over time, these small errors can really add up! @@ -84,7 +83,7 @@ out, the string output has to stop somewhere (let's say it stops at 3 decimal digits, for simplicity), so you'll get 0.333, 0.333, and 0.333. But where did the other 0.001 go? -Here's the above example as code: http://play.golang.org/p/lCZZs0w9KE +Here's the above example as code: https://play.golang.org/p/lCZZs0w9KE With Decimal, the strings being printed out represent the number exactly. So, if you have `x = y = 1/3` (with precision 3), they will actually be equal to @@ -123,4 +122,4 @@ than performance. The MIT License (MIT) -This is a heavily modified fork of [fpd.Decimal](https://github.com/oguzbilgic/fpd), which was also released under the MIT License. +This is a fork of [shopspring/decimal](https://github.com/shopspring/decimal), which was also released under the MIT License. diff --git a/vendor/github.com/shopspring/decimal/decimal-go.go b/vendor/github.com/dexon-foundation/decimal/decimal-go.go index e08a15ce4..9958d6902 100644 --- a/vendor/github.com/shopspring/decimal/decimal-go.go +++ b/vendor/github.com/dexon-foundation/decimal/decimal-go.go @@ -8,6 +8,7 @@ // Can do binary floating point in multiprecision decimal precisely // because 2 divides 10; cannot do decimal floating point // in multiprecision binary precisely. + package decimal type decimal struct { diff --git a/vendor/github.com/shopspring/decimal/decimal.go b/vendor/github.com/dexon-foundation/decimal/decimal.go index 134ece2ff..0f5079c42 100644 --- a/vendor/github.com/shopspring/decimal/decimal.go +++ b/vendor/github.com/dexon-foundation/decimal/decimal.go @@ -52,11 +52,14 @@ var DivisionPrecision = 16 // silently lose precision. var MarshalJSONWithoutQuotes = false -// Zero constant, to make computations faster. -var Zero = New(0, 1) - -// fiveDec used in Cash Rounding -var fiveDec = New(5, 0) +// Common decimal constants, to make computations faster. +var ( + Zero = New(0, 0) + One = New(1, 0) + Two = New(2, 0) + Five = New(5, 0) + Ten = New(1, 1) +) var zeroInt = big.NewInt(0) var oneInt = big.NewInt(1) @@ -113,9 +116,13 @@ func NewFromString(value string) (Decimal, error) { expInt, err := strconv.ParseInt(value[eIndex+1:], 10, 32) if err != nil { if e, ok := err.(*strconv.NumError); ok && e.Err == strconv.ErrRange { - return Decimal{}, fmt.Errorf("can't convert %s to decimal: fractional part too long", value) + return Decimal{}, &ErrorExponentLimit{value: value} + } + return Decimal{}, &ErrorInvalidFormat{ + reason: fmt.Sprintf( + "can't convert %s to decimal: exponent is not numeric", + value), } - return Decimal{}, fmt.Errorf("can't convert %s to decimal: exponent is not numeric", value) } value = value[:eIndex] exp = expInt @@ -130,21 +137,32 @@ func NewFromString(value string) (Decimal, error) { // strip the insignificant digits for more accurate comparisons. decimalPart := strings.TrimRight(parts[1], "0") intString = parts[0] + decimalPart + if intString == "" && parts[1] != "" { + intString = "0" + } expInt := -len(decimalPart) exp += int64(expInt) } else { - return Decimal{}, fmt.Errorf("can't convert %s to decimal: too many .s", value) + return Decimal{}, &ErrorInvalidFormat{ + reason: fmt.Sprintf( + "can't convert %s to decimal: too many .s", + value), + } } dValue := new(big.Int) _, ok := dValue.SetString(intString, 10) if !ok { - return Decimal{}, fmt.Errorf("can't convert %s to decimal", value) + return Decimal{}, &ErrorInvalidFormat{ + reason: fmt.Sprintf("can't convert %s to decimal", value), + } } if exp < math.MinInt32 || exp > math.MaxInt32 { // NOTE(vadim): I doubt a string could realistically be this long - return Decimal{}, fmt.Errorf("can't convert %s to decimal: fractional part too long", originalInput) + return Decimal{}, &ErrorExponentLimit{ + value: originalInput, + } } return Decimal{ @@ -186,7 +204,7 @@ func NewFromFloat(value float64) Decimal { return newFromFloat(value, math.Float64bits(value), &float64info) } -// NewFromFloat converts a float32 to Decimal. +// NewFromFloat32 converts a float32 to Decimal. // // The converted number will contain the number of significant digits that can be // represented in a float with reliable roundtrip. @@ -272,10 +290,9 @@ func NewFromFloatWithExponent(value float64, exp int32) Decimal { // specials if mant == 0 { return Decimal{} - } else { - // subnormal - exp2++ } + // subnormal + exp2++ } else { // normal mant |= 1 << 52 @@ -339,6 +356,14 @@ func NewFromFloatWithExponent(value float64, exp int32) Decimal { } } +// Rescale returns a rescaled version of the decimal. Returned +// decimal may be less precise if the given exponent is bigger +// than the initial exponent of the Decimal. +// NOTE: this will truncate, NOT round +func (d Decimal) Rescale(exp int32) Decimal { + return d.rescale(exp) +} + // rescale returns a rescaled version of the decimal. Returned // decimal may be less precise if the given exponent is bigger // than the initial exponent of the Decimal. @@ -545,9 +570,9 @@ func (d Decimal) Mod(d2 Decimal) Decimal { func (d Decimal) Pow(d2 Decimal) Decimal { var temp Decimal if d2.IntPart() == 0 { - return NewFromFloat(1) + return One } - temp = d.Pow(d2.Div(NewFromFloat(2))) + temp = d.Pow(d2.Div(Two)) if d2.IntPart()%2 == 0 { return temp.Mul(temp) } @@ -834,7 +859,7 @@ func (d Decimal) RoundCash(interval uint8) Decimal { dOne := New(10^-int64(orgExp), orgExp) d2 := d d2.exp = 0 - if d2.Mod(fiveDec).Equal(Zero) { + if d2.Mod(Five).IsZero() { d2.exp = orgExp d2 = d2.Sub(dOne) d = d2 @@ -918,13 +943,13 @@ func (d *Decimal) UnmarshalJSON(decimalBytes []byte) error { str, err := unquoteIfQuoted(decimalBytes) if err != nil { - return fmt.Errorf("Error decoding string '%s': %s", decimalBytes, err) + return err } decimal, err := NewFromString(str) *d = decimal if err != nil { - return fmt.Errorf("Error decoding string '%s': %s", str, err) + return err } return nil } @@ -1012,7 +1037,7 @@ func (d *Decimal) UnmarshalText(text []byte) error { dec, err := NewFromString(str) *d = dec if err != nil { - return fmt.Errorf("Error decoding string '%s': %s", str, err) + return err } return nil @@ -1158,8 +1183,11 @@ func unquoteIfQuoted(value interface{}) (string, error) { case []byte: bytes = v default: - return "", fmt.Errorf("Could not convert value '%+v' to byte array of type '%T'", - value, value) + return "", &ErrorInvalidType{ + reason: fmt.Sprintf( + "Could not convert value '%+v' to byte array of type '%T'", + value, value), + } } // If the amount is quoted, strip the quotes @@ -1214,31 +1242,37 @@ func (d NullDecimal) MarshalJSON() ([]byte, error) { // Trig functions -// Atan returns the arctangent, in radians, of x. -func (x Decimal) Atan() Decimal { - if x.Equal(NewFromFloat(0.0)) { - return x +// Atan returns the arctangent, in radians, of d. +func (d Decimal) Atan() Decimal { + if d.IsZero() { + return d } - if x.GreaterThan(NewFromFloat(0.0)) { - return x.satan() + if d.IsPositive() { + return d.satan() } - return x.Neg().satan().Neg() + return d.Neg().satan().Neg() +} + +var _xatanP = [...]Decimal{ + NewFromFloat(-8.750608600031904122785e-01), + NewFromFloat(-1.615753718733365076637e+01), + NewFromFloat(-7.500855792314704667340e+01), + NewFromFloat(-1.228866684490136173410e+02), + NewFromFloat(-6.485021904942025371773e+01), +} + +var _xatanQ = [...]Decimal{ + NewFromFloat(2.485846490142306297962e+01), + NewFromFloat(1.650270098316988542046e+02), + NewFromFloat(4.328810604912902668951e+02), + NewFromFloat(4.853903996359136964868e+02), + NewFromFloat(1.945506571482613964425e+02), } func (d Decimal) xatan() Decimal { - P0 := NewFromFloat(-8.750608600031904122785e-01) - P1 := NewFromFloat(-1.615753718733365076637e+01) - P2 := NewFromFloat(-7.500855792314704667340e+01) - P3 := NewFromFloat(-1.228866684490136173410e+02) - P4 := NewFromFloat(-6.485021904942025371773e+01) - Q0 := NewFromFloat(2.485846490142306297962e+01) - Q1 := NewFromFloat(1.650270098316988542046e+02) - Q2 := NewFromFloat(4.328810604912902668951e+02) - Q3 := NewFromFloat(4.853903996359136964868e+02) - Q4 := NewFromFloat(1.945506571482613964425e+02) z := d.Mul(d) - b1 := P0.Mul(z).Add(P1).Mul(z).Add(P2).Mul(z).Add(P3).Mul(z).Add(P4).Mul(z) - b2 := z.Add(Q0).Mul(z).Add(Q1).Mul(z).Add(Q2).Mul(z).Add(Q3).Mul(z).Add(Q4) + b1 := _xatanP[0].Mul(z).Add(_xatanP[1]).Mul(z).Add(_xatanP[2]).Mul(z).Add(_xatanP[3]).Mul(z).Add(_xatanP[4]).Mul(z) + b2 := z.Add(_xatanQ[0]).Mul(z).Add(_xatanQ[1]).Mul(z).Add(_xatanQ[2]).Mul(z).Add(_xatanQ[3]).Mul(z).Add(_xatanQ[4]) z = b1.Div(b2) z = d.Mul(z).Add(d) return z @@ -1251,184 +1285,184 @@ func (d Decimal) satan() Decimal { Tan3pio8 := NewFromFloat(2.41421356237309504880) // tan(3*pi/8) pi := NewFromFloat(3.14159265358979323846264338327950288419716939937510582097494459) - if d.LessThanOrEqual(NewFromFloat(0.66)) { + if d.LessThanOrEqual(New(66, -2)) { return d.xatan() } if d.GreaterThan(Tan3pio8) { - return pi.Div(NewFromFloat(2.0)).Sub(NewFromFloat(1.0).Div(d).xatan()).Add(Morebits) + return pi.Div(Two).Sub(One.Div(d).xatan()).Add(Morebits) } - return pi.Div(NewFromFloat(4.0)).Add((d.Sub(NewFromFloat(1.0)).Div(d.Add(NewFromFloat(1.0)))).xatan()).Add(NewFromFloat(0.5).Mul(Morebits)) + return pi.Div(New(4, 0)).Add((d.Sub(One).Div(d.Add(One))).xatan()).Add(New(5, -1).Mul(Morebits)) } // sin coefficients - var _sin = [...]Decimal{ - NewFromFloat(1.58962301576546568060E-10), // 0x3de5d8fd1fd19ccd - NewFromFloat(-2.50507477628578072866E-8), // 0xbe5ae5e5a9291f5d - NewFromFloat(2.75573136213857245213E-6), // 0x3ec71de3567d48a1 - NewFromFloat(-1.98412698295895385996E-4), // 0xbf2a01a019bfdf03 - NewFromFloat(8.33333333332211858878E-3), // 0x3f8111111110f7d0 - NewFromFloat(-1.66666666666666307295E-1), // 0xbfc5555555555548 - } +var _sin = [...]Decimal{ + NewFromFloat(1.58962301576546568060e-10), // 0x3de5d8fd1fd19ccd + NewFromFloat(-2.50507477628578072866e-8), // 0xbe5ae5e5a9291f5d + NewFromFloat(2.75573136213857245213e-6), // 0x3ec71de3567d48a1 + NewFromFloat(-1.98412698295895385996e-4), // 0xbf2a01a019bfdf03 + NewFromFloat(8.33333333332211858878e-3), // 0x3f8111111110f7d0 + NewFromFloat(-1.66666666666666307295e-1), // 0xbfc5555555555548 +} // Sin returns the sine of the radian argument x. - func (d Decimal) Sin() Decimal { - PI4A := NewFromFloat(7.85398125648498535156E-1) // 0x3fe921fb40000000, Pi/4 split into three parts - PI4B := NewFromFloat(3.77489470793079817668E-8) // 0x3e64442d00000000, - PI4C := NewFromFloat(2.69515142907905952645E-15) // 0x3ce8469898cc5170, - M4PI := NewFromFloat(1.273239544735162542821171882678754627704620361328125) // 4/pi - - if d.Equal(NewFromFloat(0.0)) { - return d - } - // make argument positive but save the sign - sign := false - if d.LessThan(NewFromFloat(0.0)) { - d = d.Neg() - sign = true - } - - j := d.Mul(M4PI).IntPart() // integer part of x/(Pi/4), as integer for tests on the phase angle - y := NewFromFloat(float64(j)) // integer part of x/(Pi/4), as float - - // map zeros to origin - if j&1 == 1 { - j++ - y = y.Add(NewFromFloat(1.0)) - } - j &= 7 // octant modulo 2Pi radians (360 degrees) - // reflect in x axis - if j > 3 { - sign = !sign - j -= 4 - } - z := d.Sub(y.Mul(PI4A)).Sub(y.Mul(PI4B)).Sub(y.Mul(PI4C)) // Extended precision modular arithmetic - zz := z.Mul(z) - - if j == 1 || j == 2 { - w := zz.Mul(zz).Mul(_cos[0].Mul(zz).Add(_cos[1]).Mul(zz).Add(_cos[2]).Mul(zz).Add(_cos[3]).Mul(zz).Add(_cos[4]).Mul(zz).Add(_cos[5])) - y = NewFromFloat(1.0).Sub(NewFromFloat(0.5).Mul(zz)).Add(w) - } else { - y = z.Add(z.Mul(zz).Mul(_sin[0].Mul(zz).Add(_sin[1]).Mul(zz).Add(_sin[2]).Mul(zz).Add(_sin[3]).Mul(zz).Add(_sin[4]).Mul(zz).Add(_sin[5]))) - } - if sign { - y = y.Neg() - } - return y - } - - // cos coefficients - var _cos = [...]Decimal{ - NewFromFloat(-1.13585365213876817300E-11), // 0xbda8fa49a0861a9b - NewFromFloat(2.08757008419747316778E-9), // 0x3e21ee9d7b4e3f05 - NewFromFloat(-2.75573141792967388112E-7), // 0xbe927e4f7eac4bc6 - NewFromFloat(2.48015872888517045348E-5), // 0x3efa01a019c844f5 - NewFromFloat(-1.38888888888730564116E-3), // 0xbf56c16c16c14f91 - NewFromFloat(4.16666666666665929218E-2), // 0x3fa555555555554b - } - - // Cos returns the cosine of the radian argument x. - func (d Decimal) Cos() Decimal { - - PI4A := NewFromFloat(7.85398125648498535156E-1) // 0x3fe921fb40000000, Pi/4 split into three parts - PI4B := NewFromFloat(3.77489470793079817668E-8) // 0x3e64442d00000000, - PI4C := NewFromFloat(2.69515142907905952645E-15) // 0x3ce8469898cc5170, - M4PI := NewFromFloat(1.273239544735162542821171882678754627704620361328125) // 4/pi - - // make argument positive - sign := false - if d.LessThan(NewFromFloat(0.0)) { - d = d.Neg() - } - - j := d.Mul(M4PI).IntPart() // integer part of x/(Pi/4), as integer for tests on the phase angle - y := NewFromFloat(float64(j)) // integer part of x/(Pi/4), as float - - // map zeros to origin - if j&1 == 1 { - j++ - y = y.Add(NewFromFloat(1.0)) - } - j &= 7 // octant modulo 2Pi radians (360 degrees) - // reflect in x axis - if j > 3 { - sign = !sign - j -= 4 - } - if j > 1 { - sign = !sign - } - - z := d.Sub(y.Mul(PI4A)).Sub(y.Mul(PI4B)).Sub(y.Mul(PI4C)) // Extended precision modular arithmetic - zz := z.Mul(z) - - if j == 1 || j == 2 { - y = z.Add(z.Mul(zz).Mul(_sin[0].Mul(zz).Add(_sin[1]).Mul(zz).Add(_sin[2]).Mul(zz).Add(_sin[3]).Mul(zz).Add(_sin[4]).Mul(zz).Add(_sin[5]))) - } else { - w := zz.Mul(zz).Mul(_cos[0].Mul(zz).Add(_cos[1]).Mul(zz).Add(_cos[2]).Mul(zz).Add(_cos[3]).Mul(zz).Add(_cos[4]).Mul(zz).Add(_cos[5])) - y = NewFromFloat(1.0).Sub(NewFromFloat(0.5).Mul(zz)).Add(w) - } - if sign { - y = y.Neg() - } - return y - } - - var _tanP = [...]Decimal{ - NewFromFloat(-1.30936939181383777646E+4), // 0xc0c992d8d24f3f38 - NewFromFloat(1.15351664838587416140E+6), // 0x413199eca5fc9ddd - NewFromFloat(-1.79565251976484877988E+7), // 0xc1711fead3299176 - } - var _tanQ = [...]Decimal{ - NewFromFloat(1.00000000000000000000E+0), - NewFromFloat(1.36812963470692954678E+4), //0x40cab8a5eeb36572 - NewFromFloat(-1.32089234440210967447E+6), //0xc13427bc582abc96 - NewFromFloat(2.50083801823357915839E+7), //0x4177d98fc2ead8ef - NewFromFloat(-5.38695755929454629881E+7), //0xc189afe03cbe5a31 - } - - // Tan returns the tangent of the radian argument x. - func (d Decimal) Tan() Decimal { - - PI4A := NewFromFloat(7.85398125648498535156E-1) // 0x3fe921fb40000000, Pi/4 split into three parts - PI4B := NewFromFloat(3.77489470793079817668E-8) // 0x3e64442d00000000, - PI4C := NewFromFloat(2.69515142907905952645E-15) // 0x3ce8469898cc5170, - M4PI := NewFromFloat(1.273239544735162542821171882678754627704620361328125) // 4/pi - - if d.Equal(NewFromFloat(0.0)) { - return d - } +func (d Decimal) Sin() Decimal { + PI4A := NewFromFloat(7.85398125648498535156e-1) // 0x3fe921fb40000000, Pi/4 split into three parts + PI4B := NewFromFloat(3.77489470793079817668e-8) // 0x3e64442d00000000, + PI4C := NewFromFloat(2.69515142907905952645e-15) // 0x3ce8469898cc5170, + M4PI := NewFromFloat(1.273239544735162542821171882678754627704620361328125) // 4/pi - // make argument positive but save the sign - sign := false - if d.LessThan(NewFromFloat(0.0)) { - d = d.Neg() - sign = true - } - - j := d.Mul(M4PI).IntPart() // integer part of x/(Pi/4), as integer for tests on the phase angle - y := NewFromFloat(float64(j)) // integer part of x/(Pi/4), as float - - // map zeros to origin - if j&1 == 1 { - j++ - y = y.Add(NewFromFloat(1.0)) - } - - z := d.Sub(y.Mul(PI4A)).Sub(y.Mul(PI4B)).Sub(y.Mul(PI4C)) // Extended precision modular arithmetic - zz := z.Mul(z) - - if zz.GreaterThan(NewFromFloat(1e-14)) { - w := zz.Mul(_tanP[0].Mul(zz).Add(_tanP[1]).Mul(zz).Add(_tanP[2])) - x := zz.Add(_tanQ[1]).Mul(zz).Add(_tanQ[2]).Mul(zz).Add(_tanQ[3]).Mul(zz).Add(_tanQ[4]) - y = z.Add(z.Mul(w.Div(x))) - } else { - y = z - } - if j&2 == 2 { - y = NewFromFloat(-1.0).Div(y) - } - if sign { - y = y.Neg() - } - return y - } + if d.IsZero() { + return d + } + // make argument positive but save the sign + sign := false + if d.IsNegative() { + d = d.Neg() + sign = true + } + + j := d.Mul(M4PI).IntPart() // integer part of x/(Pi/4), as integer for tests on the phase angle + y := NewFromFloat(float64(j)) // integer part of x/(Pi/4), as float + + // map zeros to origin + if j&1 == 1 { + j++ + y = y.Add(One) + } + j &= 7 // octant modulo 2Pi radians (360 degrees) + // reflect in x axis + if j > 3 { + sign = !sign + j -= 4 + } + z := d.Sub(y.Mul(PI4A)).Sub(y.Mul(PI4B)).Sub(y.Mul(PI4C)) // Extended precision modular arithmetic + zz := z.Mul(z) + + if j == 1 || j == 2 { + w := zz.Mul(zz).Mul(_cos[0].Mul(zz).Add(_cos[1]).Mul(zz).Add(_cos[2]).Mul(zz).Add(_cos[3]).Mul(zz).Add(_cos[4]).Mul(zz).Add(_cos[5])) + y = One.Sub(New(5, -1).Mul(zz)).Add(w) + } else { + y = z.Add(z.Mul(zz).Mul(_sin[0].Mul(zz).Add(_sin[1]).Mul(zz).Add(_sin[2]).Mul(zz).Add(_sin[3]).Mul(zz).Add(_sin[4]).Mul(zz).Add(_sin[5]))) + } + if sign { + y = y.Neg() + } + return y +} + +// cos coefficients +var _cos = [...]Decimal{ + NewFromFloat(-1.13585365213876817300e-11), // 0xbda8fa49a0861a9b + NewFromFloat(2.08757008419747316778e-9), // 0x3e21ee9d7b4e3f05 + NewFromFloat(-2.75573141792967388112e-7), // 0xbe927e4f7eac4bc6 + NewFromFloat(2.48015872888517045348e-5), // 0x3efa01a019c844f5 + NewFromFloat(-1.38888888888730564116e-3), // 0xbf56c16c16c14f91 + NewFromFloat(4.16666666666665929218e-2), // 0x3fa555555555554b +} + +// Cos returns the cosine of the radian argument x. +func (d Decimal) Cos() Decimal { + + PI4A := NewFromFloat(7.85398125648498535156e-1) // 0x3fe921fb40000000, Pi/4 split into three parts + PI4B := NewFromFloat(3.77489470793079817668e-8) // 0x3e64442d00000000, + PI4C := NewFromFloat(2.69515142907905952645e-15) // 0x3ce8469898cc5170, + M4PI := NewFromFloat(1.273239544735162542821171882678754627704620361328125) // 4/pi + + // make argument positive + sign := false + if d.IsNegative() { + d = d.Neg() + } + + j := d.Mul(M4PI).IntPart() // integer part of x/(Pi/4), as integer for tests on the phase angle + y := NewFromFloat(float64(j)) // integer part of x/(Pi/4), as float + + // map zeros to origin + if j&1 == 1 { + j++ + y = y.Add(One) + } + j &= 7 // octant modulo 2Pi radians (360 degrees) + // reflect in x axis + if j > 3 { + sign = !sign + j -= 4 + } + if j > 1 { + sign = !sign + } + + z := d.Sub(y.Mul(PI4A)).Sub(y.Mul(PI4B)).Sub(y.Mul(PI4C)) // Extended precision modular arithmetic + zz := z.Mul(z) + + if j == 1 || j == 2 { + y = z.Add(z.Mul(zz).Mul(_sin[0].Mul(zz).Add(_sin[1]).Mul(zz).Add(_sin[2]).Mul(zz).Add(_sin[3]).Mul(zz).Add(_sin[4]).Mul(zz).Add(_sin[5]))) + } else { + w := zz.Mul(zz).Mul(_cos[0].Mul(zz).Add(_cos[1]).Mul(zz).Add(_cos[2]).Mul(zz).Add(_cos[3]).Mul(zz).Add(_cos[4]).Mul(zz).Add(_cos[5])) + y = One.Sub(New(5, -1).Mul(zz)).Add(w) + } + if sign { + y = y.Neg() + } + return y +} + +var _tanP = [...]Decimal{ + NewFromFloat(-1.30936939181383777646e+4), // 0xc0c992d8d24f3f38 + NewFromFloat(1.15351664838587416140e+6), // 0x413199eca5fc9ddd + NewFromFloat(-1.79565251976484877988e+7), // 0xc1711fead3299176 +} +var _tanQ = [...]Decimal{ + NewFromFloat(1.00000000000000000000e+0), + NewFromFloat(1.36812963470692954678e+4), //0x40cab8a5eeb36572 + NewFromFloat(-1.32089234440210967447e+6), //0xc13427bc582abc96 + NewFromFloat(2.50083801823357915839e+7), //0x4177d98fc2ead8ef + NewFromFloat(-5.38695755929454629881e+7), //0xc189afe03cbe5a31 +} + +// Tan returns the tangent of the radian argument x. +func (d Decimal) Tan() Decimal { + + PI4A := NewFromFloat(7.85398125648498535156e-1) // 0x3fe921fb40000000, Pi/4 split into three parts + PI4B := NewFromFloat(3.77489470793079817668e-8) // 0x3e64442d00000000, + PI4C := NewFromFloat(2.69515142907905952645e-15) // 0x3ce8469898cc5170, + M4PI := NewFromFloat(1.273239544735162542821171882678754627704620361328125) // 4/pi + + if d.IsZero() { + return d + } + + // make argument positive but save the sign + sign := false + if d.IsNegative() { + d = d.Neg() + sign = true + } + + j := d.Mul(M4PI).IntPart() // integer part of x/(Pi/4), as integer for tests on the phase angle + y := NewFromFloat(float64(j)) // integer part of x/(Pi/4), as float + + // map zeros to origin + if j&1 == 1 { + j++ + y = y.Add(One) + } + + z := d.Sub(y.Mul(PI4A)).Sub(y.Mul(PI4B)).Sub(y.Mul(PI4C)) // Extended precision modular arithmetic + zz := z.Mul(z) + + if zz.GreaterThan(New(1, -14)) { + w := zz.Mul(_tanP[0].Mul(zz).Add(_tanP[1]).Mul(zz).Add(_tanP[2])) + x := zz.Add(_tanQ[1]).Mul(zz).Add(_tanQ[2]).Mul(zz).Add(_tanQ[3]).Mul(zz).Add(_tanQ[4]) + y = z.Add(z.Mul(w.Div(x))) + } else { + y = z + } + if j&2 == 2 { + y = New(-1, 0).Div(y) + } + if sign { + y = y.Neg() + } + return y +} diff --git a/vendor/github.com/dexon-foundation/decimal/errors.go b/vendor/github.com/dexon-foundation/decimal/errors.go new file mode 100644 index 000000000..47346144c --- /dev/null +++ b/vendor/github.com/dexon-foundation/decimal/errors.go @@ -0,0 +1,40 @@ +package decimal + +import "fmt" + +// ErrorExponentLimit is returned when the decimal exponent exceed int32 range. +type ErrorExponentLimit struct { + value string +} + +// Error implements error interface. +func (e *ErrorExponentLimit) Error() string { + return fmt.Sprintf("can't convert %s to decimal: fractional part too long", e.value) +} + +// ErrorInvalidFormat is returned when the input string is not valid integer. +type ErrorInvalidFormat struct { + reason string +} + +// Error implements error interface. +func (e *ErrorInvalidFormat) Error() string { + return e.reason +} + +// ErrorInvalidType is returned when the value passed into sql.Scanner is not +// with expected type. (valid types: int64, float64, []byte, string) +type ErrorInvalidType struct { + reason string +} + +// Error implements error interface. +func (e *ErrorInvalidType) Error() string { + return e.reason +} + +func assertErrorInterface() { + var _ error = (*ErrorExponentLimit)(nil) + var _ error = (*ErrorInvalidFormat)(nil) + var _ error = (*ErrorInvalidType)(nil) +} diff --git a/vendor/github.com/shopspring/decimal/rounding.go b/vendor/github.com/dexon-foundation/decimal/rounding.go index fdd74eaa8..8008f55cb 100644 --- a/vendor/github.com/shopspring/decimal/rounding.go +++ b/vendor/github.com/dexon-foundation/decimal/rounding.go @@ -8,6 +8,7 @@ // Can do binary floating point in multiprecision decimal precisely // because 2 divides 10; cannot do decimal floating point // in multiprecision binary precisely. + package decimal type floatInfo struct { diff --git a/vendor/vendor.json b/vendor/vendor.json index 0f11868bc..dd0d82ad5 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -139,6 +139,12 @@ "versionExact": "dev" }, { + "checksumSHA1": "7puYGOPhizIgr5nZ6GcRPNfr4Dc=", + "path": "github.com/dexon-foundation/decimal", + "revision": "05ad7315b3c581519964b7762074e00de3c4ceac", + "revisionTime": "2019-03-25T09:02:55Z" + }, + { "checksumSHA1": "8EuKVkP1v/w5fRuuvUaXX5k/F+I=", "path": "github.com/dexon-foundation/dexon-consensus/common", "revision": "fa3b5a29499739e90b3cf17f9a0cf60a72a64fc0", @@ -631,12 +637,6 @@ "revisionTime": "2016-06-18T19:32:21Z" }, { - "checksumSHA1": "dhcoAs6YFyvXiG55mx5WDxg+/4g=", - "path": "github.com/shopspring/decimal", - "revision": "cd690d0c9e2447b1ef2a129a6b7b49077da89b8e", - "revisionTime": "2018-07-09T20:31:17Z" - }, - { "checksumSHA1": "mGbTYZ8dHVTiPTTJu3ktp+84pPI=", "path": "github.com/stretchr/testify/assert", "revision": "890a5c3458b43e6104ff5da8dfa139d013d77544", |