-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdoc_test.go
1430 lines (1306 loc) · 30.2 KB
/
doc_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package decimal_test
import (
"bytes"
"encoding/gob"
"encoding/json"
"encoding/xml"
"fmt"
"slices"
"strings"
"github.com/govalues/decimal"
)
// This example implements a simple calculator that evaluates mathematical
// expressions written in [postfix notation].
// The calculator can handle basic arithmetic operations such as addition,
// subtraction, multiplication, and division.
//
// [postfix notation]: https://en.wikipedia.org/wiki/Reverse_Polish_notation
func Example_postfixCalculator() {
fmt.Println(evaluate("1.23 4.56 + 10 *"))
// Output:
// 57.90 <nil>
}
func evaluate(input string) (decimal.Decimal, error) {
tokens := strings.Fields(input)
if len(tokens) == 0 {
return decimal.Decimal{}, fmt.Errorf("no tokens")
}
stack := make([]decimal.Decimal, 0, len(tokens))
for i, token := range tokens {
var err error
var result decimal.Decimal
if token == "+" || token == "-" || token == "*" || token == "/" {
if len(stack) < 2 {
return decimal.Decimal{}, fmt.Errorf("not enough operands")
}
left := stack[len(stack)-2]
right := stack[len(stack)-1]
stack = stack[:len(stack)-2]
switch token {
case "+":
result, err = left.Add(right)
case "-":
result, err = left.Sub(right)
case "*":
result, err = left.Mul(right)
case "/":
result, err = left.Quo(right)
}
} else {
result, err = decimal.Parse(token)
}
if err != nil {
return decimal.Decimal{}, fmt.Errorf("processing token %q at position %v: %w", token, i, err)
}
stack = append(stack, result)
}
if len(stack) != 1 {
return decimal.Decimal{}, fmt.Errorf("stack contains %v, expected exactly one item", stack)
}
return stack[0], nil
}
// This example calculates an approximate value of π using the [Leibniz formula].
// The Leibniz formula is an infinite series that converges to π/4, and is
// given by the equation: 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ... = π/4.
// This example computes the series up to the 500,000th term using decimal arithmetic
// and returns the approximate value of π.
//
// [Leibniz formula]: https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80
func Example_piApproximation() {
fmt.Println(approximate(500_000))
fmt.Println(decimal.Pi)
// Output:
// 3.141590653589793192 <nil>
// 3.141592653589793238
}
func approximate(terms int) (decimal.Decimal, error) {
pi := decimal.Zero
denominator := decimal.One
increment := decimal.Two
multiplier := decimal.MustParse("4")
var err error
for range terms {
pi, err = pi.AddQuo(multiplier, denominator)
if err != nil {
return decimal.Decimal{}, err
}
denominator, err = denominator.Add(increment)
if err != nil {
return decimal.Decimal{}, err
}
multiplier = multiplier.Neg()
}
return pi, nil
}
// This example demonstrates the advantage of decimals for financial calculations.
// It computes the sum 0.1 + 0.2 using both decimal and float64 arithmetic.
// In decimal arithmetic, the result is exactly 0.3, as expected.
// In float64 arithmetic, the result is 0.30000000000000004 due to floating-point inaccuracy.
func Example_floatInaccuracy() {
a := decimal.MustParse("0.1")
b := decimal.MustParse("0.2")
fmt.Println(a.Add(b))
x := 0.1
y := 0.2
fmt.Println(x + y)
// Output:
// 0.3 <nil>
// 0.30000000000000004
}
func ExampleSum() {
d := decimal.MustParse("5.67")
e := decimal.MustParse("-8")
f := decimal.MustParse("23")
fmt.Println(decimal.Sum(d, e, f))
// Output: 20.67 <nil>
}
func ExampleMean() {
d := decimal.MustParse("5.67")
e := decimal.MustParse("-8")
f := decimal.MustParse("23")
fmt.Println(decimal.Mean(d, e, f))
// Output: 6.89 <nil>
}
func ExampleProd() {
d := decimal.MustParse("5.67")
e := decimal.MustParse("-8")
f := decimal.MustParse("23")
fmt.Println(decimal.Prod(d, e, f))
// Output: -1043.28 <nil>
}
func ExampleMustNew() {
fmt.Println(decimal.MustNew(567, 0))
fmt.Println(decimal.MustNew(567, 1))
fmt.Println(decimal.MustNew(567, 2))
fmt.Println(decimal.MustNew(567, 3))
fmt.Println(decimal.MustNew(567, 4))
// Output:
// 567
// 56.7
// 5.67
// 0.567
// 0.0567
}
func ExampleNew() {
fmt.Println(decimal.New(567, 0))
fmt.Println(decimal.New(567, 1))
fmt.Println(decimal.New(567, 2))
fmt.Println(decimal.New(567, 3))
fmt.Println(decimal.New(567, 4))
// Output:
// 567 <nil>
// 56.7 <nil>
// 5.67 <nil>
// 0.567 <nil>
// 0.0567 <nil>
}
func ExampleNewFromInt64() {
fmt.Println(decimal.NewFromInt64(5, 6, 1))
fmt.Println(decimal.NewFromInt64(5, 6, 2))
fmt.Println(decimal.NewFromInt64(5, 6, 3))
fmt.Println(decimal.NewFromInt64(5, 6, 4))
fmt.Println(decimal.NewFromInt64(5, 6, 5))
// Output:
// 5.6 <nil>
// 5.06 <nil>
// 5.006 <nil>
// 5.0006 <nil>
// 5.00006 <nil>
}
func ExampleNewFromFloat64() {
fmt.Println(decimal.NewFromFloat64(5.67e-2))
fmt.Println(decimal.NewFromFloat64(5.67e-1))
fmt.Println(decimal.NewFromFloat64(5.67e0))
fmt.Println(decimal.NewFromFloat64(5.67e1))
fmt.Println(decimal.NewFromFloat64(5.67e2))
// Output:
// 0.0567 <nil>
// 0.567 <nil>
// 5.67 <nil>
// 56.7 <nil>
// 567 <nil>
}
func ExampleDecimal_Zero() {
d := decimal.MustParse("5")
e := decimal.MustParse("5.6")
f := decimal.MustParse("5.67")
fmt.Println(d.Zero())
fmt.Println(e.Zero())
fmt.Println(f.Zero())
// Output:
// 0
// 0.0
// 0.00
}
func ExampleDecimal_One() {
d := decimal.MustParse("5")
e := decimal.MustParse("5.6")
f := decimal.MustParse("5.67")
fmt.Println(d.One())
fmt.Println(e.One())
fmt.Println(f.One())
// Output:
// 1
// 1.0
// 1.00
}
func ExampleDecimal_ULP() {
d := decimal.MustParse("5")
e := decimal.MustParse("5.6")
f := decimal.MustParse("5.67")
fmt.Println(d.ULP())
fmt.Println(e.ULP())
fmt.Println(f.ULP())
// Output:
// 1
// 0.1
// 0.01
}
func ExampleParse() {
fmt.Println(decimal.Parse("5.67"))
// Output: 5.67 <nil>
}
func ExampleParseExact() {
fmt.Println(decimal.ParseExact("5.67", 0))
fmt.Println(decimal.ParseExact("5.67", 1))
fmt.Println(decimal.ParseExact("5.67", 2))
fmt.Println(decimal.ParseExact("5.67", 3))
fmt.Println(decimal.ParseExact("5.67", 4))
// Output:
// 5.67 <nil>
// 5.67 <nil>
// 5.67 <nil>
// 5.670 <nil>
// 5.6700 <nil>
}
func ExampleMustParse() {
fmt.Println(decimal.MustParse("-1.23"))
// Output: -1.23
}
func ExampleDecimal_String() {
d := decimal.MustParse("1234567890.123456789")
fmt.Println(d.String())
// Output: 1234567890.123456789
}
func unmarshalGOB(data []byte) (decimal.Decimal, error) {
var d decimal.Decimal
dec := gob.NewDecoder(bytes.NewReader(data))
err := dec.Decode(&d)
if err != nil {
return decimal.Decimal{}, err
}
return d, nil
}
func marshalGOB(s string) ([]byte, error) {
d, err := decimal.Parse(s)
if err != nil {
return nil, err
}
var data bytes.Buffer
enc := gob.NewEncoder(&data)
err = enc.Encode(d)
if err != nil {
return nil, err
}
return data.Bytes(), nil
}
func ExampleDecimal_UnmarshalBinary_gob() {
data := []byte{
0x12, 0x7f, 0x06, 0x01,
0x01, 0x07, 0x44, 0x65,
0x63, 0x69, 0x6d, 0x61,
0x6c, 0x01, 0xff, 0x80,
0x00, 0x00, 0x00, 0x08,
0xff, 0x80, 0x00, 0x04,
0x35, 0x2e, 0x36, 0x37,
}
fmt.Println(unmarshalGOB(data))
// Output:
// 5.67 <nil>
}
func ExampleDecimal_MarshalBinary_gob() {
data, err := marshalGOB("5.67")
fmt.Printf("[% x] %v\n", data, err)
// Output:
// [12 7f 06 01 01 07 44 65 63 69 6d 61 6c 01 ff 80 00 00 00 08 ff 80 00 04 35 2e 36 37] <nil>
}
func ExampleDecimal_Float64() {
d := decimal.MustParse("0.1")
e := decimal.MustParse("123.456")
f := decimal.MustParse("1234567890.123456789")
fmt.Println(d.Float64())
fmt.Println(e.Float64())
fmt.Println(f.Float64())
// Output:
// 0.1 true
// 123.456 true
// 1.2345678901234567e+09 true
}
func ExampleDecimal_Int64() {
d := decimal.MustParse("5.67")
fmt.Println(d.Int64(0))
fmt.Println(d.Int64(1))
fmt.Println(d.Int64(2))
fmt.Println(d.Int64(3))
fmt.Println(d.Int64(4))
// Output:
// 6 0 true
// 5 7 true
// 5 67 true
// 5 670 true
// 5 6700 true
}
func ExampleDecimal_UnmarshalBSONValue_bson() {
data := []byte{
0x37, 0x02, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x3c, 0x30,
}
var d decimal.Decimal
err := d.UnmarshalBSONValue(19, data)
fmt.Println(d, err)
// Output:
// 5.67 <nil>
}
func ExampleDecimal_MarshalBSONValue_bson() {
d := decimal.MustParse("5.67")
t, data, err := d.MarshalBSONValue()
fmt.Printf("%v [% x] %v\n", t, data, err)
// Output:
// 19 [37 02 00 00 00 00 00 00 00 00 00 00 00 00 3c 30] <nil>
}
type Object struct {
Number decimal.Decimal `json:"number"`
}
func unmarshalJSON(s string) (Object, error) {
var o Object
err := json.Unmarshal([]byte(s), &o)
if err != nil {
return Object{}, err
}
return o, nil
}
func marshalJSON(s string) (string, error) {
d, err := decimal.Parse(s)
if err != nil {
return "", err
}
data, err := json.Marshal(Object{Number: d})
if err != nil {
return "", err
}
return string(data), nil
}
func ExampleDecimal_UnmarshalJSON_json() {
fmt.Println(unmarshalJSON(`{"number":"5.67"}`))
fmt.Println(unmarshalJSON(`{"number":"-5.67"}`))
fmt.Println(unmarshalJSON(`{"number":5.67e-5}`))
fmt.Println(unmarshalJSON(`{"number":5.67e5}`))
// Output:
// {5.67} <nil>
// {-5.67} <nil>
// {0.0000567} <nil>
// {567000} <nil>
}
func ExampleDecimal_MarshalJSON_json() {
fmt.Println(marshalJSON("5.67"))
fmt.Println(marshalJSON("-5.67"))
// Output:
// {"number":"5.67"} <nil>
// {"number":"-5.67"} <nil>
}
type Entity struct {
Number decimal.Decimal `xml:"Number"`
}
func unmarshalXML(s string) (Entity, error) {
var e Entity
err := xml.Unmarshal([]byte(s), &e)
return e, err
}
func marshalXML(s string) (string, error) {
d, err := decimal.Parse(s)
if err != nil {
return "", err
}
data, err := xml.Marshal(Entity{Number: d})
if err != nil {
return "", err
}
return string(data), nil
}
func ExampleDecimal_UnmarshalText_xml() {
fmt.Println(unmarshalXML(`<Entity><Number>5.67</Number></Entity>`))
fmt.Println(unmarshalXML(`<Entity><Number>-5.67</Number></Entity>`))
fmt.Println(unmarshalXML(`<Entity><Number>5.67e-5</Number></Entity>`))
fmt.Println(unmarshalXML(`<Entity><Number>5.67e5</Number></Entity>`))
// Output:
// {5.67} <nil>
// {-5.67} <nil>
// {0.0000567} <nil>
// {567000} <nil>
}
func ExampleDecimal_MarshalText_xml() {
fmt.Println(marshalXML("5.67"))
fmt.Println(marshalXML("-5.67"))
fmt.Println(marshalXML("5.67e-5"))
fmt.Println(marshalXML("5.67e5"))
// Output:
// <Entity><Number>5.67</Number></Entity> <nil>
// <Entity><Number>-5.67</Number></Entity> <nil>
// <Entity><Number>0.0000567</Number></Entity> <nil>
// <Entity><Number>567000</Number></Entity> <nil>
}
func ExampleDecimal_Scan() {
var d decimal.Decimal
_ = d.Scan("5.67")
fmt.Println(d)
// Output: 5.67
}
func ExampleDecimal_Value() {
d := decimal.MustParse("5.67")
fmt.Println(d.Value())
// Output: 5.67 <nil>
}
func ExampleDecimal_Format() {
d := decimal.MustParse("5.67")
fmt.Printf("%f\n", d)
fmt.Printf("%k\n", d)
// Output:
// 5.67
// 567%
}
func ExampleDecimal_Coef() {
d := decimal.MustParse("-123")
e := decimal.MustParse("5.7")
f := decimal.MustParse("0.4")
fmt.Println(d.Coef())
fmt.Println(e.Coef())
fmt.Println(f.Coef())
// Output:
// 123
// 57
// 4
}
func ExampleDecimal_Prec() {
d := decimal.MustParse("-123")
e := decimal.MustParse("5.7")
f := decimal.MustParse("0.4")
fmt.Println(d.Prec())
fmt.Println(e.Prec())
fmt.Println(f.Prec())
// Output:
// 3
// 2
// 1
}
func ExampleDecimal_Mul() {
d := decimal.MustParse("5.7")
e := decimal.MustParse("3")
fmt.Println(d.Mul(e))
// Output: 17.1 <nil>
}
func ExampleDecimal_MulExact() {
d := decimal.MustParse("5.7")
e := decimal.MustParse("3")
fmt.Println(d.MulExact(e, 0))
fmt.Println(d.MulExact(e, 1))
fmt.Println(d.MulExact(e, 2))
fmt.Println(d.MulExact(e, 3))
fmt.Println(d.MulExact(e, 4))
// Output:
// 17.1 <nil>
// 17.1 <nil>
// 17.10 <nil>
// 17.100 <nil>
// 17.1000 <nil>
}
func ExampleDecimal_SubMul() {
d := decimal.MustParse("2")
e := decimal.MustParse("3")
f := decimal.MustParse("4")
fmt.Println(d.SubMul(e, f))
// Output: -10 <nil>
}
func ExampleDecimal_SubMulExact() {
d := decimal.MustParse("2")
e := decimal.MustParse("3")
f := decimal.MustParse("4")
fmt.Println(d.SubMulExact(e, f, 0))
fmt.Println(d.SubMulExact(e, f, 1))
fmt.Println(d.SubMulExact(e, f, 2))
fmt.Println(d.SubMulExact(e, f, 3))
fmt.Println(d.SubMulExact(e, f, 4))
// Output:
// -10 <nil>
// -10.0 <nil>
// -10.00 <nil>
// -10.000 <nil>
// -10.0000 <nil>
}
func ExampleDecimal_AddMul() {
d := decimal.MustParse("2")
e := decimal.MustParse("3")
f := decimal.MustParse("4")
fmt.Println(d.AddMul(e, f))
// Output: 14 <nil>
}
func ExampleDecimal_AddMulExact() {
d := decimal.MustParse("2")
e := decimal.MustParse("3")
f := decimal.MustParse("4")
fmt.Println(d.AddMulExact(e, f, 0))
fmt.Println(d.AddMulExact(e, f, 1))
fmt.Println(d.AddMulExact(e, f, 2))
fmt.Println(d.AddMulExact(e, f, 3))
fmt.Println(d.AddMulExact(e, f, 4))
// Output:
// 14 <nil>
// 14.0 <nil>
// 14.00 <nil>
// 14.000 <nil>
// 14.0000 <nil>
}
func ExampleDecimal_SubQuo() {
d := decimal.MustParse("2")
e := decimal.MustParse("3")
f := decimal.MustParse("4")
fmt.Println(d.SubQuo(e, f))
// Output: 1.25 <nil>
}
func ExampleDecimal_SubQuoExact() {
d := decimal.MustParse("2")
e := decimal.MustParse("3")
f := decimal.MustParse("4")
fmt.Println(d.SubQuoExact(e, f, 0))
fmt.Println(d.SubQuoExact(e, f, 1))
fmt.Println(d.SubQuoExact(e, f, 2))
fmt.Println(d.SubQuoExact(e, f, 3))
fmt.Println(d.SubQuoExact(e, f, 4))
// Output:
// 1.25 <nil>
// 1.25 <nil>
// 1.25 <nil>
// 1.250 <nil>
// 1.2500 <nil>
}
func ExampleDecimal_AddQuo() {
d := decimal.MustParse("2")
e := decimal.MustParse("3")
f := decimal.MustParse("4")
fmt.Println(d.AddQuo(e, f))
// Output: 2.75 <nil>
}
func ExampleDecimal_AddQuoExact() {
d := decimal.MustParse("2")
e := decimal.MustParse("3")
f := decimal.MustParse("4")
fmt.Println(d.AddQuoExact(e, f, 0))
fmt.Println(d.AddQuoExact(e, f, 1))
fmt.Println(d.AddQuoExact(e, f, 2))
fmt.Println(d.AddQuoExact(e, f, 3))
fmt.Println(d.AddQuoExact(e, f, 4))
// Output:
// 2.75 <nil>
// 2.75 <nil>
// 2.75 <nil>
// 2.750 <nil>
// 2.7500 <nil>
}
func ExampleDecimal_Pow() {
d := decimal.MustParse("4")
e := decimal.MustParse("0.5")
f := decimal.MustParse("-0.5")
fmt.Println(d.Pow(e))
fmt.Println(d.Pow(f))
// Output:
// 2.000000000000000000 <nil>
// 0.5000000000000000000 <nil>
}
func ExampleDecimal_PowInt() {
d := decimal.MustParse("2")
fmt.Println(d.PowInt(-2))
fmt.Println(d.PowInt(-1))
fmt.Println(d.PowInt(0))
fmt.Println(d.PowInt(1))
fmt.Println(d.PowInt(2))
// Output:
// 0.25 <nil>
// 0.5 <nil>
// 1 <nil>
// 2 <nil>
// 4 <nil>
}
func ExampleDecimal_Sqrt() {
d := decimal.MustParse("1")
e := decimal.MustParse("2")
f := decimal.MustParse("3")
g := decimal.MustParse("4")
fmt.Println(d.Sqrt())
fmt.Println(e.Sqrt())
fmt.Println(f.Sqrt())
fmt.Println(g.Sqrt())
// Output:
// 1 <nil>
// 1.414213562373095049 <nil>
// 1.732050807568877294 <nil>
// 2 <nil>
}
func ExampleDecimal_Exp() {
d := decimal.MustParse("-2.302585092994045684")
e := decimal.MustParse("0")
f := decimal.MustParse("2.302585092994045684")
fmt.Println(d.Exp())
fmt.Println(e.Exp())
fmt.Println(f.Exp())
// Output:
// 0.1000000000000000000 <nil>
// 1 <nil>
// 10.00000000000000000 <nil>
}
func ExampleDecimal_Log() {
d := decimal.MustParse("1")
e := decimal.MustParse("2")
f := decimal.MustParse("2.718281828459045236")
g := decimal.MustParse("10")
fmt.Println(d.Log())
fmt.Println(e.Log())
fmt.Println(f.Log())
fmt.Println(g.Log())
// Output:
// 0 <nil>
// 0.6931471805599453094 <nil>
// 1.000000000000000000 <nil>
// 2.302585092994045684 <nil>
}
func ExampleDecimal_Log2() {
d := decimal.MustParse("1")
e := decimal.MustParse("2")
f := decimal.MustParse("2.718281828459045236")
g := decimal.MustParse("10")
fmt.Println(d.Log2())
fmt.Println(e.Log2())
fmt.Println(f.Log2())
fmt.Println(g.Log2())
// Output:
// 0 <nil>
// 1 <nil>
// 1.442695040888963408 <nil>
// 3.321928094887362348 <nil>
}
func ExampleDecimal_Log10() {
d := decimal.MustParse("1")
e := decimal.MustParse("2")
f := decimal.MustParse("2.718281828459045236")
g := decimal.MustParse("10")
fmt.Println(d.Log10())
fmt.Println(e.Log10())
fmt.Println(f.Log10())
fmt.Println(g.Log10())
// Output:
// 0 <nil>
// 0.3010299956639811952 <nil>
// 0.4342944819032518278 <nil>
// 1 <nil>
}
func ExampleDecimal_Add() {
d := decimal.MustParse("5.67")
e := decimal.MustParse("8")
fmt.Println(d.Add(e))
// Output: 13.67 <nil>
}
func ExampleDecimal_AddExact() {
d := decimal.MustParse("5.67")
e := decimal.MustParse("8")
fmt.Println(d.AddExact(e, 0))
fmt.Println(d.AddExact(e, 1))
fmt.Println(d.AddExact(e, 2))
fmt.Println(d.AddExact(e, 3))
fmt.Println(d.AddExact(e, 4))
// Output:
// 13.67 <nil>
// 13.67 <nil>
// 13.67 <nil>
// 13.670 <nil>
// 13.6700 <nil>
}
func ExampleDecimal_Sub() {
d := decimal.MustParse("-5.67")
e := decimal.MustParse("8")
fmt.Println(d.Sub(e))
// Output: -13.67 <nil>
}
func ExampleDecimal_SubAbs() {
d := decimal.MustParse("-5.67")
e := decimal.MustParse("8")
fmt.Println(d.SubAbs(e))
// Output: 13.67 <nil>
}
func ExampleDecimal_SubExact() {
d := decimal.MustParse("8")
e := decimal.MustParse("5.67")
fmt.Println(d.SubExact(e, 0))
fmt.Println(d.SubExact(e, 1))
fmt.Println(d.SubExact(e, 2))
fmt.Println(d.SubExact(e, 3))
fmt.Println(d.SubExact(e, 4))
// Output:
// 2.33 <nil>
// 2.33 <nil>
// 2.33 <nil>
// 2.330 <nil>
// 2.3300 <nil>
}
func ExampleDecimal_Quo() {
d := decimal.MustParse("5.67")
e := decimal.MustParse("2")
fmt.Println(d.Quo(e))
// Output: 2.835 <nil>
}
func ExampleDecimal_QuoExact() {
d := decimal.MustParse("5.66")
e := decimal.MustParse("2")
fmt.Println(d.QuoExact(e, 0))
fmt.Println(d.QuoExact(e, 1))
fmt.Println(d.QuoExact(e, 2))
fmt.Println(d.QuoExact(e, 3))
fmt.Println(d.QuoExact(e, 4))
// Output:
// 2.83 <nil>
// 2.83 <nil>
// 2.83 <nil>
// 2.830 <nil>
// 2.8300 <nil>
}
func ExampleDecimal_QuoRem() {
d := decimal.MustParse("5.67")
e := decimal.MustParse("2")
fmt.Println(d.QuoRem(e))
// Output: 2 1.67 <nil>
}
func ExampleDecimal_Inv() {
d := decimal.MustParse("2")
fmt.Println(d.Inv())
// Output: 0.5 <nil>
}
func ExampleDecimal_Less() {
d := decimal.MustParse("-23")
e := decimal.MustParse("5.67")
fmt.Println(d.Less(e))
fmt.Println(e.Less(d))
// Output:
// true
// false
}
func ExampleDecimal_Equal() {
d := decimal.MustParse("-23")
e := decimal.MustParse("5.67")
fmt.Println(d.Equal(e))
fmt.Println(d.Equal(d))
// Output:
// false
// true
}
func ExampleDecimal_Equal_slices() {
s := []decimal.Decimal{
decimal.MustParse("-5.67"),
decimal.MustParse("0"),
decimal.MustParse("0"),
}
fmt.Println(slices.EqualFunc(s, s, decimal.Decimal.Equal))
fmt.Println(slices.CompactFunc(s, decimal.Decimal.Equal))
// Output:
// true
// [-5.67 0]
}
func ExampleDecimal_Cmp() {
d := decimal.MustParse("-23")
e := decimal.MustParse("5.67")
fmt.Println(d.Cmp(e))
fmt.Println(d.Cmp(d))
fmt.Println(e.Cmp(d))
// Output:
// -1
// 0
// 1
}
func ExampleDecimal_Cmp_slices() {
s := []decimal.Decimal{
decimal.MustParse("-5.67"),
decimal.MustParse("23"),
decimal.MustParse("0"),
}
fmt.Println(slices.CompareFunc(s, s, decimal.Decimal.Cmp))
fmt.Println(slices.MaxFunc(s, decimal.Decimal.Cmp))
fmt.Println(slices.MinFunc(s, decimal.Decimal.Cmp))
fmt.Println(s, slices.IsSortedFunc(s, decimal.Decimal.Cmp))
slices.SortFunc(s, decimal.Decimal.Cmp)
fmt.Println(s, slices.IsSortedFunc(s, decimal.Decimal.Cmp))
fmt.Println(slices.BinarySearchFunc(s, decimal.MustParse("1"), decimal.Decimal.Cmp))
// Output:
// 0
// 23
// -5.67
// [-5.67 23 0] false
// [-5.67 0 23] true
// 2 false
}
func ExampleDecimal_CmpAbs() {
d := decimal.MustParse("-23")
e := decimal.MustParse("5.67")
fmt.Println(d.CmpAbs(e))
fmt.Println(d.CmpAbs(d))
fmt.Println(e.CmpAbs(d))
// Output:
// 1
// 0
// -1
}
func ExampleDecimal_CmpAbs_slices() {
s := []decimal.Decimal{
decimal.MustParse("-5.67"),
decimal.MustParse("23"),
decimal.MustParse("0"),
}
fmt.Println(slices.CompareFunc(s, s, decimal.Decimal.CmpAbs))
fmt.Println(slices.MaxFunc(s, decimal.Decimal.CmpAbs))
fmt.Println(slices.MinFunc(s, decimal.Decimal.CmpAbs))
fmt.Println(s, slices.IsSortedFunc(s, decimal.Decimal.CmpAbs))
slices.SortFunc(s, decimal.Decimal.CmpAbs)
fmt.Println(s, slices.IsSortedFunc(s, decimal.Decimal.CmpAbs))
fmt.Println(slices.BinarySearchFunc(s, decimal.MustParse("1"), decimal.Decimal.CmpAbs))
// Output:
// 0
// 23
// 0
// [-5.67 23 0] false
// [0 -5.67 23] true
// 1 false
}
func ExampleDecimal_CmpTotal() {
d := decimal.MustParse("2.0")
e := decimal.MustParse("2.00")
fmt.Println(d.CmpTotal(e))
fmt.Println(d.CmpTotal(d))
fmt.Println(e.CmpTotal(d))
// Output:
// 1
// 0
// -1
}
func ExampleDecimal_CmpTotal_slices() {
s := []decimal.Decimal{
decimal.MustParse("-5.67"),
decimal.MustParse("23"),
decimal.MustParse("0"),
}
fmt.Println(slices.CompareFunc(s, s, decimal.Decimal.CmpTotal))
fmt.Println(slices.MaxFunc(s, decimal.Decimal.CmpTotal))
fmt.Println(slices.MinFunc(s, decimal.Decimal.CmpTotal))
fmt.Println(s, slices.IsSortedFunc(s, decimal.Decimal.CmpTotal))
slices.SortFunc(s, decimal.Decimal.CmpTotal)
fmt.Println(s, slices.IsSortedFunc(s, decimal.Decimal.CmpTotal))
fmt.Println(slices.BinarySearchFunc(s, decimal.MustParse("10"), decimal.Decimal.CmpTotal))
// Output:
// 0
// 23
// -5.67
// [-5.67 23 0] false
// [-5.67 0 23] true
// 2 false
}
func ExampleDecimal_Max() {
d := decimal.MustParse("23")
e := decimal.MustParse("-5.67")
fmt.Println(d.Max(e))
// Output: 23
}
func ExampleDecimal_Min() {
d := decimal.MustParse("23")
e := decimal.MustParse("-5.67")
fmt.Println(d.Min(e))
// Output: -5.67
}
//nolint:revive
func ExampleDecimal_Clamp() {
min := decimal.MustParse("-20")
max := decimal.MustParse("20")
d := decimal.MustParse("-5.67")
e := decimal.MustParse("0")
f := decimal.MustParse("23")
fmt.Println(d.Clamp(min, max))
fmt.Println(e.Clamp(min, max))
fmt.Println(f.Clamp(min, max))
// Output:
// -5.67 <nil>
// 0 <nil>
// 20 <nil>
}
func ExampleDecimal_Rescale() {
d := decimal.MustParse("5.678")
fmt.Println(d.Rescale(0))
fmt.Println(d.Rescale(1))
fmt.Println(d.Rescale(2))
fmt.Println(d.Rescale(3))
fmt.Println(d.Rescale(4))
// Output:
// 6
// 5.7
// 5.68
// 5.678
// 5.6780
}
func ExampleDecimal_Quantize() {