This repository has been archived by the owner on May 14, 2022. It is now read-only.
/
example_test.go
293 lines (242 loc) · 7.54 KB
/
example_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
// Copyright 2014 Denis Bernard. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dec_test
import (
"."
"./util"
"fmt"
"sync"
)
// Go re-implementation of decNumber's example1.c - simple addition.
//
// Cnvert the first two argument words to decNumber, add them together, and display the result.
func Example_example1() {
var (
arg1 = "1.27"
arg2 = "2.23"
digits int32 = 34
)
ctx := dec.NewContext(dec.InitBase, digits)
a := dec.NewNumber(digits).FromString(arg1, ctx) // Should not ignore errors...
b := dec.NewNumber(digits).FromString(arg2, ctx)
a.Add(a, b, ctx) // a=a+b
// there is no need to call c.String(). fmt.Print("%s") takes care of it.
fmt.Printf("%s + %s => %s\n", arg1, arg2, a)
// Output:
// 1.27 + 2.23 => 3.50
}
// Go re-implementation of decNumber's example5.c. Compressed formats.
func Example_example5() {
var (
a = new(dec.Quad) // Should be dec.Double, but not implemented yet
d *dec.Number
ctx *dec.Context
hexes string
)
ctx = dec.NewContext(dec.InitDecimal128, 0) // will be 16 digits
a.FromString("127.9984", ctx)
// lay out the Quad as hexadecimal pairs
// big endian - ordered
for _, b := range a.Bytes() {
if dec.LittleEndian {
hexes = fmt.Sprintf("%02X ", b) + hexes
} else {
hexes += fmt.Sprintf("%02X ", b)
}
}
d = a.ToNumber(nil)
fmt.Printf("%s => %s=> %s\n", a, hexes, d)
// Output:
// 127.9984 => 22 07 00 00 00 00 00 00 00 00 00 00 00 15 E6 8E => 127.9984
}
// Go re-implementation of decNumber's example6.c. Packed Decimal numbers.
//
// This example reworks Example 2, starting and ending with Packed Decimal numbers.
func Example_example6() {
// This is our main function where we setup a NumberPool and collect
// arguments.
// Create a global NumberPool
p := &dec.NumberPool{
&util.Pool{New: func() interface{} { return dec.NewNumber(gCtx.Digits()) }},
gCtx,
}
// arguments for CompoundInterest
var (
startp = dec.Packed{[]byte{0x5C}, -4} // 5e+4 = 50000
ratep = dec.Packed{[]byte{0x31, 0x7C}, 2} // 3.17
yearsp = dec.Packed{[]byte{0x01, 0x2C}, 0} // 12
start, _ = startp.ToNumber(nil)
rate, _ = ratep.ToNumber(nil)
years, _ = yearsp.ToNumber(nil)
)
// do some computing at last
total, err := CompoundInterest(p, start, rate, years)
// error check ?
if err != nil {
fmt.Printf("Error: %v\n", err)
}
// print results
fmt.Printf("%s at %s%% for %s years => %s\n", start, rate, years, total)
resPacked := new(dec.Packed)
resPacked.FromNumber(total)
// and dispose of the result
defer p.Put(total)
for _, d := range resPacked.Buf {
fmt.Printf("%02X ", d)
}
fmt.Printf("(scale: %d)\n", resPacked.Scale)
// Output:
// 5E+4 at 3.17% for 12 years => 72712.85
// 72 71 28 5C (scale: 2)
}
// Go re-implementation of decNumber's example7.c. Using decQuad to add two numbers together.
func Example_example7() {
var (
a = new(dec.Quad)
b = new(dec.Quad)
ctx *dec.Context
)
// Context suitable for Quads
ctx = dec.NewContext(dec.InitQuad, 0)
a.FromString("123.456", ctx)
b.FromString("7890.12", ctx)
a.Add(a, b, ctx) // a = a + b
s := a.String()
fmt.Printf("123.456 + %s => %s\n", b, s)
// Output:
// 123.456 + 7890.12 => 8013.576
}
// Go re-implementation of decNumber's example8.c. Using Quad with Number
func Example_example8() {
var (
a = new(dec.Quad)
numa, numb *dec.Number
ctx = dec.NewContext(dec.InitQuad, 0) // Initialize
)
a.FromString("1234.567", ctx) // get a
as := a.String() // keep a string copy for test output
a.Add(a, a, ctx) // double a
numa = a.ToNumber(nil) // convert to Number
numb = dec.NewNumber(ctx.Digits()).FromString("98.7654", ctx)
numa.Power(numa, numb, ctx) // numa=numa**numb
a.FromNumber(numa, ctx) // back to quad
fmt.Printf("power(2*%s, %s) => %s \n", as, numb, a)
// Output:
// power(2*1234.567, 98.7654) => 1.164207353496260978533862797373143E+335
}
// NewNumber() example
func Example_NewNumber() {
// create a context with 99 digits precision, just for kicks
ctx := dec.NewContext(dec.InitBase, 99)
// create a number
n := dec.NewNumber(ctx.Digits())
// an IEEE 754 decimal128 type context
// using the default 34 digits precision
ctx = dec.NewContext(dec.InitDecimal128, 0)
n = dec.NewNumber(ctx.Digits())
// Set it to zero
n.Zero()
fmt.Println(n)
// Output:
// 0
}
// Accpeted formats and error handling demo.
func ExampleNumber_FromString() {
// new context
ctx := dec.NewContext(dec.InitDecimal64, 0)
// We're lazy, and since we can do it, define a shorthand
New := func(s string) *dec.Number {
return dec.NewNumber(ctx.Digits()).FromString(s, ctx)
}
n := New("378.2654651646516165416165315131232")
if err := ctx.ErrorStatus(); err != nil {
fmt.Println(err.Error)
}
// Decimal64 has only 16 digits, the number will be truncated to the context's precision
fmt.Printf("%s\n", n.String())
// infinite number
n = New("-INF")
if err := ctx.ErrorStatus(); err != nil {
fmt.Println(err.Error())
}
fmt.Printf("%s\n", n.String())
// Scientific notation
n = New("1.275654e16")
if err := ctx.ErrorStatus(); err != nil {
fmt.Println(err.Error())
}
fmt.Printf("%s\n", n.String())
// error. Will cause an overflow and set the number to +Infinity
// This is still a "valid" number for some applications
n = New("1.275654e321455")
if err := ctx.ErrorStatus(); err != nil {
fmt.Println(err.Error())
}
fmt.Printf("%s\n", n.String())
// Here, we will get a conversion syntax error
// and the number will be set to NaN (not a number)
//
// We call ZeroStatus() to clear any previous error
ctx.ZeroStatus()
n = New("12garbage524")
if err := ctx.ErrorStatus(); err != nil {
fmt.Println(err.Error())
}
fmt.Printf("%s\n", n.String())
// Output:
// 378.2654651646516
// -Infinity
// 1.275654E+16
// Overflow
// Infinity
// Conversion syntax
// NaN
}
// Example use of a pool to manage a free list of numbers
func ExampleNumberPool_1() {
// Create a Context
ctx := dec.NewContext(dec.InitDecimal128, 0)
// New() function for the pool to create new numbers
newFunc := func() interface{} { return dec.NewNumber(ctx.Digits()) }
// create a pool. Either dec.Pool or sync.Pool will do
syncPool := sync.Pool{New: newFunc}
// We can use Get().(*dec.Number) to get new or reusable numbers
number := syncPool.Get().(*dec.Number)
fmt.Printf("from sync.Pool: %s\n", number.Zero())
// We're done with it, put it back in the pool
syncPool.Put(number)
// Or, wrap it with a NumberPool so that Get() returns *Number instead of interface{}.
// NumberPool also helps keeping track of the context.
pool := &dec.NumberPool{&syncPool, ctx}
// and benefit: no need to type-cast
number = pool.Get()
// Introducing the idiomatic code: defer Put() the *Number right after Get()
defer pool.Put(number)
fmt.Printf("from sync.Pool: %s\n", number.FromString("1243", pool.Context))
// Output:
// from sync.Pool: 0
// from sync.Pool: 1243
}
// Compact version of example 1, using dec.Pool
func ExampleNumberPool_2() {
// Create a Context
ctx := dec.NewContext(dec.InitDecimal128, 0)
// And a usable pool based on dec.Pool
pool := &dec.NumberPool{
&util.Pool{
New: func() interface{} { return dec.NewNumber(ctx.Digits()) },
},
ctx,
}
// Now create numbers
number := pool.Get()
defer pool.Put(number)
fmt.Printf("from dec.Pool: %s\n", number.Zero())
number = pool.Get()
defer pool.Put(number)
fmt.Printf("from dec.Pool: %s\n", number.FromString("1243", pool.Context))
// Output:
// from dec.Pool: 0
// from dec.Pool: 1243
}