Codebase list golang-github-cockroachdb-apd / 3c7eb54
Update upstream source from tag 'upstream/2.0.1' Update to upstream version '2.0.1' with Debian dir 11f9f205421ebfa520201a2c0fba380c075b8beb Dmitry Smirnov 4 years ago
9 changed file(s) with 95 addition(s) and 37 deletion(s). Raw diff Collapse all Expand all
+0
-2
.gitignore less more
0
1 .pc
00 language: go
11
22 go:
3 - 1.8.x
43 - 1.9.x
4 - 1.10.x
55
66 script: go test
6060 if _, _, err := nums[i].SetString(buf.String()); err != nil {
6161 b.Fatal(err)
6262 }
63 nums[i].SetExponent(int32(s - numDigits))
63 nums[i].Exponent = int32(s - numDigits)
6464 }
6565 b.Run(
6666 fmt.Sprintf("P%d/S%d/D%d", p, s, d),
181181 if set, res, err := c.setIfNaN(d, x); set {
182182 return res, err
183183 }
184 if x.IsZero() {
185 d.Set(x)
186 d.Negative = false
187 } else {
188 d.Neg(x)
189 }
184 d.Neg(x)
190185 return c.Round(d, d)
191186 }
192187
500495 // is odd or even.
501496 approx := new(Decimal)
502497 if e%2 == 0 {
503 approx.SetCoefficient(819).SetExponent(-3)
498 approx.SetFinite(819, -3)
504499 ed.Mul(approx, approx, f)
505500 ed.Add(approx, approx, New(259, -3))
506501 } else {
507502 f.Exponent--
508503 e++
509 approx.SetCoefficient(259).SetExponent(-2)
504 approx.SetFinite(259, -2)
510505 ed.Mul(approx, approx, f)
511506 ed.Add(approx, approx, New(819, -4))
512507 }
727722 // tmp1 = z - 1
728723 ed.Sub(tmp1, z, decimalOne)
729724 // tmp3 = 0.1
730 tmp3.SetCoefficient(1).SetExponent(-1)
725 tmp3.SetFinite(1, -1)
731726
732727 usePowerSeries := false
733728
741736 // We multiplied the input by 10^-expDelta, we will need to add
742737 // ln(10^expDelta) = expDelta * ln(10)
743738 // to the result.
744 resAdjust.SetCoefficient(int64(expDelta))
739 resAdjust.setCoefficient(int64(expDelta))
745740 ed.Mul(resAdjust, resAdjust, decimalLn10.get(p))
746741
747742 // tmp1 = z - 1
789784 ed.Mul(tmp3, tmp3, tmp2)
790785
791786 // tmp4 = 2n+1
792 tmp4.SetCoefficient(int64(2*n + 1)).SetExponent(0)
787 tmp4.SetFinite(int64(2*n+1), 0)
793788
794789 ed.Quo(tmp4, tmp3, tmp4)
795790
918913 if x.Sign() < 0 {
919914 res = res.negateOverflowFlags()
920915 res |= Clamped
921 d.SetCoefficient(0)
922 d.Exponent = c.etiny()
916 d.SetFinite(0, c.etiny())
923917 } else {
924918 d.Set(decimalInfinity)
925919 }
926920 return c.goError(res)
927921 }
928922 // if abs(x) <= setexp(.9, -currentprecision); then result 1
929 tmp2.SetCoefficient(9).SetExponent(int32(-cp) - 1)
923 tmp2.SetFinite(9, int32(-cp)-1)
930924 if tmp1.Cmp(tmp2) <= 0 {
931925 d.Set(decimalOne)
932926 return c.goError(res)
966960 sum := New(1, 0)
967961 tmp2.Exponent = 0
968962 for i := n - 1; i > 0; i-- {
969 tmp2.SetCoefficient(i)
963 tmp2.setCoefficient(i)
970964 // tmp1 = r / i
971965 ed.Quo(tmp1, r, tmp2)
972966 // sum = sum * r / i
8888
8989 // NewWithBigInt creates a new decimal with the given coefficient and exponent.
9090 func NewWithBigInt(coeff *big.Int, exponent int32) *Decimal {
91 return &Decimal{
92 Coeff: *coeff,
91 d := &Decimal{
9392 Exponent: exponent,
9493 }
94 d.Coeff.Set(coeff)
95 if d.Coeff.Sign() < 0 {
96 d.Negative = true
97 d.Coeff.Abs(&d.Coeff)
98 }
99 return d
95100 }
96101
97102 func consumePrefix(s, prefix string) (string, bool) {
225230
226231 // SetInt64 sets d to x and returns d.
227232 func (d *Decimal) SetInt64(x int64) *Decimal {
228 d.SetCoefficient(x)
229 d.Exponent = 0
233 return d.SetFinite(x, 0)
234 }
235
236 // SetFinite sets d to x with exponent e and returns d.
237 func (d *Decimal) SetFinite(x int64, e int32) *Decimal {
238 d.setCoefficient(x)
239 d.Exponent = e
230240 return d
231241 }
232242
233 // SetCoefficient sets d's coefficient and negative value to x, its Form to
234 // Finite, and returns d. The exponent is not changed.
235 func (d *Decimal) SetCoefficient(x int64) *Decimal {
243 // setCoefficient sets d's coefficient and negative value to x and its Form
244 // to Finite The exponent is not changed. Since the exponent is not changed
245 // (and this is thus easy to misuse), this is unexported for internal use only.
246 func (d *Decimal) setCoefficient(x int64) {
236247 d.Negative = x < 0
237248 d.Coeff.SetInt64(x)
238249 d.Coeff.Abs(&d.Coeff)
239250 d.Form = Finite
240 return d
241 }
242
243 // SetExponent sets d's Exponent value to x and returns d.
244 func (d *Decimal) SetExponent(x int32) *Decimal {
245 d.Exponent = x
246 return d
247251 }
248252
249253 // SetFloat64 sets d's Coefficient and Exponent to x and returns d. d will
683687 // Neg sets d to -x and returns d.
684688 func (d *Decimal) Neg(x *Decimal) *Decimal {
685689 d.Set(x)
686 d.Negative = !d.Negative
690 if d.IsZero() {
691 d.Negative = false
692 } else {
693 d.Negative = !d.Negative
694 }
687695 return d
688696 }
689697
706714 switch x.Sign() {
707715 case 0:
708716 nd = int(d.NumDigits())
709 d.SetCoefficient(0)
710 d.Exponent = 0
717 d.SetInt64(0)
711718 return d, nd - 1
712719 case -1:
713720 neg = true
5050 t.Fatalf("%s: %+v", s, err)
5151 }
5252 return d
53 }
54
55 func TestNewWithBigInt(t *testing.T) {
56 tests := []string{
57 "0",
58 "1",
59 "-1",
60 }
61 for _, tc := range tests {
62 t.Run(tc, func(t *testing.T) {
63 expect, _, err := new(Decimal).SetString(tc)
64 if err != nil {
65 t.Fatal(err)
66 }
67 b, ok := new(big.Int).SetString(tc, 10)
68 if !ok {
69 t.Fatal("bad bigint")
70 }
71 d := NewWithBigInt(b, 0)
72 if d.Coeff.Sign() < 0 {
73 t.Fatal("unexpected negative coeff")
74 }
75 // Verify that changing b doesn't change d.
76 b.Set(big.NewInt(1234))
77 if d.CmpTotal(expect) != 0 {
78 t.Fatalf("expected %s, got %s", expect, d)
79 }
80 })
81 }
5382 }
5483
5584 func TestUpscale(t *testing.T) {
689718 z := d.IsZero()
690719 if z != tc.zero {
691720 t.Fatalf("got %v, expected %v", z, tc.zero)
721 }
722 })
723 }
724 }
725
726 func TestNeg(t *testing.T) {
727 tests := map[string]string{
728 "0": "0",
729 "-0": "0",
730 "-0.000": "0.000",
731 "-00.000100": "0.000100",
732 }
733
734 for tc, expect := range tests {
735 t.Run(tc, func(t *testing.T) {
736 d, _, err := NewFromString(tc)
737 if err != nil {
738 t.Fatal(err)
739 }
740 var z Decimal
741 z.Neg(d)
742 s := z.String()
743 if s != expect {
744 t.Fatalf("expected %s, got %s", expect, s)
692745 }
693746 })
694747 }
2222 // If format is a different character, Text returns a "%" followed by the
2323 // unrecognized.Format character. The 'f' format has the possibility of
2424 // displaying precision that is not present in the Decimal when it appends
25 // zeros. All other formats always show the exact precision of the Decimal.
25 // zeros (the 'g' format avoids the use of 'f' in this case). All other
26 // formats always show the exact precision of the Decimal.
2627 func (d *Decimal) Text(format byte) string {
2728 cap := 10 // TODO(gri) determine a good/better value here
2829 return string(d.Append(make([]byte, 0, cap), format))
0 module github.com/cockroachdb/apd/v2
1
2 require github.com/pkg/errors v0.8.0
0 github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
1 github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=