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
60 | 60 | if _, _, err := nums[i].SetString(buf.String()); err != nil { |
61 | 61 | b.Fatal(err) |
62 | 62 | } |
63 | nums[i].SetExponent(int32(s - numDigits)) | |
63 | nums[i].Exponent = int32(s - numDigits) | |
64 | 64 | } |
65 | 65 | b.Run( |
66 | 66 | fmt.Sprintf("P%d/S%d/D%d", p, s, d), |
181 | 181 | if set, res, err := c.setIfNaN(d, x); set { |
182 | 182 | return res, err |
183 | 183 | } |
184 | if x.IsZero() { | |
185 | d.Set(x) | |
186 | d.Negative = false | |
187 | } else { | |
188 | d.Neg(x) | |
189 | } | |
184 | d.Neg(x) | |
190 | 185 | return c.Round(d, d) |
191 | 186 | } |
192 | 187 | |
500 | 495 | // is odd or even. |
501 | 496 | approx := new(Decimal) |
502 | 497 | if e%2 == 0 { |
503 | approx.SetCoefficient(819).SetExponent(-3) | |
498 | approx.SetFinite(819, -3) | |
504 | 499 | ed.Mul(approx, approx, f) |
505 | 500 | ed.Add(approx, approx, New(259, -3)) |
506 | 501 | } else { |
507 | 502 | f.Exponent-- |
508 | 503 | e++ |
509 | approx.SetCoefficient(259).SetExponent(-2) | |
504 | approx.SetFinite(259, -2) | |
510 | 505 | ed.Mul(approx, approx, f) |
511 | 506 | ed.Add(approx, approx, New(819, -4)) |
512 | 507 | } |
727 | 722 | // tmp1 = z - 1 |
728 | 723 | ed.Sub(tmp1, z, decimalOne) |
729 | 724 | // tmp3 = 0.1 |
730 | tmp3.SetCoefficient(1).SetExponent(-1) | |
725 | tmp3.SetFinite(1, -1) | |
731 | 726 | |
732 | 727 | usePowerSeries := false |
733 | 728 | |
741 | 736 | // We multiplied the input by 10^-expDelta, we will need to add |
742 | 737 | // ln(10^expDelta) = expDelta * ln(10) |
743 | 738 | // to the result. |
744 | resAdjust.SetCoefficient(int64(expDelta)) | |
739 | resAdjust.setCoefficient(int64(expDelta)) | |
745 | 740 | ed.Mul(resAdjust, resAdjust, decimalLn10.get(p)) |
746 | 741 | |
747 | 742 | // tmp1 = z - 1 |
789 | 784 | ed.Mul(tmp3, tmp3, tmp2) |
790 | 785 | |
791 | 786 | // tmp4 = 2n+1 |
792 | tmp4.SetCoefficient(int64(2*n + 1)).SetExponent(0) | |
787 | tmp4.SetFinite(int64(2*n+1), 0) | |
793 | 788 | |
794 | 789 | ed.Quo(tmp4, tmp3, tmp4) |
795 | 790 | |
918 | 913 | if x.Sign() < 0 { |
919 | 914 | res = res.negateOverflowFlags() |
920 | 915 | res |= Clamped |
921 | d.SetCoefficient(0) | |
922 | d.Exponent = c.etiny() | |
916 | d.SetFinite(0, c.etiny()) | |
923 | 917 | } else { |
924 | 918 | d.Set(decimalInfinity) |
925 | 919 | } |
926 | 920 | return c.goError(res) |
927 | 921 | } |
928 | 922 | // if abs(x) <= setexp(.9, -currentprecision); then result 1 |
929 | tmp2.SetCoefficient(9).SetExponent(int32(-cp) - 1) | |
923 | tmp2.SetFinite(9, int32(-cp)-1) | |
930 | 924 | if tmp1.Cmp(tmp2) <= 0 { |
931 | 925 | d.Set(decimalOne) |
932 | 926 | return c.goError(res) |
966 | 960 | sum := New(1, 0) |
967 | 961 | tmp2.Exponent = 0 |
968 | 962 | for i := n - 1; i > 0; i-- { |
969 | tmp2.SetCoefficient(i) | |
963 | tmp2.setCoefficient(i) | |
970 | 964 | // tmp1 = r / i |
971 | 965 | ed.Quo(tmp1, r, tmp2) |
972 | 966 | // sum = sum * r / i |
88 | 88 | |
89 | 89 | // NewWithBigInt creates a new decimal with the given coefficient and exponent. |
90 | 90 | func NewWithBigInt(coeff *big.Int, exponent int32) *Decimal { |
91 | return &Decimal{ | |
92 | Coeff: *coeff, | |
91 | d := &Decimal{ | |
93 | 92 | Exponent: exponent, |
94 | 93 | } |
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 | |
95 | 100 | } |
96 | 101 | |
97 | 102 | func consumePrefix(s, prefix string) (string, bool) { |
225 | 230 | |
226 | 231 | // SetInt64 sets d to x and returns d. |
227 | 232 | 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 | |
230 | 240 | return d |
231 | 241 | } |
232 | 242 | |
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) { | |
236 | 247 | d.Negative = x < 0 |
237 | 248 | d.Coeff.SetInt64(x) |
238 | 249 | d.Coeff.Abs(&d.Coeff) |
239 | 250 | 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 | |
247 | 251 | } |
248 | 252 | |
249 | 253 | // SetFloat64 sets d's Coefficient and Exponent to x and returns d. d will |
683 | 687 | // Neg sets d to -x and returns d. |
684 | 688 | func (d *Decimal) Neg(x *Decimal) *Decimal { |
685 | 689 | d.Set(x) |
686 | d.Negative = !d.Negative | |
690 | if d.IsZero() { | |
691 | d.Negative = false | |
692 | } else { | |
693 | d.Negative = !d.Negative | |
694 | } | |
687 | 695 | return d |
688 | 696 | } |
689 | 697 | |
706 | 714 | switch x.Sign() { |
707 | 715 | case 0: |
708 | 716 | nd = int(d.NumDigits()) |
709 | d.SetCoefficient(0) | |
710 | d.Exponent = 0 | |
717 | d.SetInt64(0) | |
711 | 718 | return d, nd - 1 |
712 | 719 | case -1: |
713 | 720 | neg = true |
50 | 50 | t.Fatalf("%s: %+v", s, err) |
51 | 51 | } |
52 | 52 | 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 | } | |
53 | 82 | } |
54 | 83 | |
55 | 84 | func TestUpscale(t *testing.T) { |
689 | 718 | z := d.IsZero() |
690 | 719 | if z != tc.zero { |
691 | 720 | 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) | |
692 | 745 | } |
693 | 746 | }) |
694 | 747 | } |
22 | 22 | // If format is a different character, Text returns a "%" followed by the |
23 | 23 | // unrecognized.Format character. The 'f' format has the possibility of |
24 | 24 | // 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. | |
26 | 27 | func (d *Decimal) Text(format byte) string { |
27 | 28 | cap := 10 // TODO(gri) determine a good/better value here |
28 | 29 | return string(d.Append(make([]byte, 0, cap), format)) |