forked from fritzpay/paymentd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rounder.go
320 lines (294 loc) · 9.21 KB
/
rounder.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
package dec
// This file implements signed multi-precision decimals.
import (
"math/big"
)
// Rounder represents a method for rounding the (possibly infinite decimal)
// result of a division to a finite Dec. It is used by Dec.Round() and
// Dec.Quo().
//
type Rounder interface {
// When UseRemainder() returns true, the Round() method is passed the
// remainder of the division, expressed as the numerator and denominator of
// a rational.
UseRemainder() bool
// Round sets the rounded value of a quotient to z, and returns z.
// quo is rounded down (truncated towards zero) to the scale obtained from
// the Scaler in Quo().
//
// When the remainder is not used, remNum and remDen are nil.
// When used, the remainder is normalized between -1 and 1; that is:
//
// -|remDen| < remNum < |remDen|
//
// remDen has the same sign as y, and remNum is zero or has the same sign
// as x.
Round(z, quo *Dec, remNum, remDen *big.Int) *Dec
}
type rounder struct {
useRem bool
round func(z, quo *Dec, remNum, remDen *big.Int) *Dec
}
func (r rounder) UseRemainder() bool {
return r.useRem
}
func (r rounder) Round(z, quo *Dec, remNum, remDen *big.Int) *Dec {
return r.round(z, quo, remNum, remDen)
}
// RoundExact returns quo if rem is zero, or nil otherwise. It is intended to
// be used with ScaleQuoExact when it is guaranteed that the result can be
// obtained without rounding. QuoExact is a shorthand for such a quotient
// operation.
//
var RoundExact Rounder = roundExact
// RoundDown rounds towards 0; that is, returns the Dec with the greatest
// absolute value not exceeding that of the result represented by quo and rem.
//
// The following table shows examples of the results for
// Quo(x, y, Scale(scale), RoundDown).
//
// x y scale result
// ------------------------------
// -1.8 10 1 -0.1
// -1.5 10 1 -0.1
// -1.2 10 1 -0.1
// -1.0 10 1 -0.1
// -0.8 10 1 -0.0
// -0.5 10 1 -0.0
// -0.2 10 1 -0.0
// 0.0 10 1 0.0
// 0.2 10 1 0.0
// 0.5 10 1 0.0
// 0.8 10 1 0.0
// 1.0 10 1 0.1
// 1.2 10 1 0.1
// 1.5 10 1 0.1
// 1.8 10 1 0.1
//
var RoundDown Rounder = roundDown
// RoundUp rounds away from 0; that is, returns the Dec with the smallest
// absolute value not smaller than that of the result represented by quo and
// rem.
//
// The following table shows examples of the results for
// Quo(x, y, Scale(scale), RoundUp).
//
// x y scale result
// ------------------------------
// -1.8 10 1 -0.2
// -1.5 10 1 -0.2
// -1.2 10 1 -0.2
// -1.0 10 1 -0.1
// -0.8 10 1 -0.1
// -0.5 10 1 -0.1
// -0.2 10 1 -0.1
// 0.0 10 1 0.0
// 0.2 10 1 0.1
// 0.5 10 1 0.1
// 0.8 10 1 0.1
// 1.0 10 1 0.1
// 1.2 10 1 0.2
// 1.5 10 1 0.2
// 1.8 10 1 0.2
//
var RoundUp Rounder = roundUp
// RoundHalfDown rounds to the nearest Dec, and when the remainder is 1/2, it
// rounds to the Dec with the lower absolute value.
//
// The following table shows examples of the results for
// Quo(x, y, Scale(scale), RoundHalfDown).
//
// x y scale result
// ------------------------------
// -1.8 10 1 -0.2
// -1.5 10 1 -0.1
// -1.2 10 1 -0.1
// -1.0 10 1 -0.1
// -0.8 10 1 -0.1
// -0.5 10 1 -0.0
// -0.2 10 1 -0.0
// 0.0 10 1 0.0
// 0.2 10 1 0.0
// 0.5 10 1 0.0
// 0.8 10 1 0.1
// 1.0 10 1 0.1
// 1.2 10 1 0.1
// 1.5 10 1 0.1
// 1.8 10 1 0.2
//
var RoundHalfDown Rounder = roundHalfDown
// RoundHalfUp rounds to the nearest Dec, and when the remainder is 1/2, it
// rounds to the Dec with the greater absolute value.
//
// The following table shows examples of the results for
// Quo(x, y, Scale(scale), RoundHalfUp).
//
// x y scale result
// ------------------------------
// -1.8 10 1 -0.2
// -1.5 10 1 -0.2
// -1.2 10 1 -0.1
// -1.0 10 1 -0.1
// -0.8 10 1 -0.1
// -0.5 10 1 -0.1
// -0.2 10 1 -0.0
// 0.0 10 1 0.0
// 0.2 10 1 0.0
// 0.5 10 1 0.1
// 0.8 10 1 0.1
// 1.0 10 1 0.1
// 1.2 10 1 0.1
// 1.5 10 1 0.2
// 1.8 10 1 0.2
//
var RoundHalfUp Rounder = roundHalfUp
// RoundHalfEven rounds to the nearest Dec, and when the remainder is 1/2, it
// rounds to the Dec with even last digit.
//
// The following table shows examples of the results for
// Quo(x, y, Scale(scale), RoundHalfEven).
//
// x y scale result
// ------------------------------
// -1.8 10 1 -0.2
// -1.5 10 1 -0.2
// -1.2 10 1 -0.1
// -1.0 10 1 -0.1
// -0.8 10 1 -0.1
// -0.5 10 1 -0.0
// -0.2 10 1 -0.0
// 0.0 10 1 0.0
// 0.2 10 1 0.0
// 0.5 10 1 0.0
// 0.8 10 1 0.1
// 1.0 10 1 0.1
// 1.2 10 1 0.1
// 1.5 10 1 0.2
// 1.8 10 1 0.2
//
var RoundHalfEven Rounder = roundHalfEven
// RoundFloor rounds towards negative infinity; that is, returns the greatest
// Dec not exceeding the result represented by quo and rem.
//
// The following table shows examples of the results for
// Quo(x, y, Scale(scale), RoundFloor).
//
// x y scale result
// ------------------------------
// -1.8 10 1 -0.2
// -1.5 10 1 -0.2
// -1.2 10 1 -0.2
// -1.0 10 1 -0.1
// -0.8 10 1 -0.1
// -0.5 10 1 -0.1
// -0.2 10 1 -0.1
// 0.0 10 1 0.0
// 0.2 10 1 0.0
// 0.5 10 1 0.0
// 0.8 10 1 0.0
// 1.0 10 1 0.1
// 1.2 10 1 0.1
// 1.5 10 1 0.1
// 1.8 10 1 0.1
//
var RoundFloor Rounder = roundFloor
// RoundCeil rounds towards positive infinity; that is, returns the
// smallest Dec not smaller than the result represented by quo and rem.
//
// The following table shows examples of the results for
// Quo(x, y, Scale(scale), RoundCeil).
//
// x y scale result
// ------------------------------
// -1.8 10 1 -0.1
// -1.5 10 1 -0.1
// -1.2 10 1 -0.1
// -1.0 10 1 -0.1
// -0.8 10 1 -0.0
// -0.5 10 1 -0.0
// -0.2 10 1 -0.0
// 0.0 10 1 0.0
// 0.2 10 1 0.1
// 0.5 10 1 0.1
// 0.8 10 1 0.1
// 1.0 10 1 0.1
// 1.2 10 1 0.2
// 1.5 10 1 0.2
// 1.8 10 1 0.2
//
var RoundCeil Rounder = roundCeil
var intSign = []*big.Int{big.NewInt(-1), big.NewInt(0), big.NewInt(1)}
var roundExact = rounder{true,
func(z, q *Dec, rA, rB *big.Int) *Dec {
if rA.Sign() != 0 {
return nil
}
return z.Set(q)
}}
var roundDown = rounder{false,
func(z, q *Dec, rA, rB *big.Int) *Dec {
return z.Set(q)
}}
var roundUp = rounder{true,
func(z, q *Dec, rA, rB *big.Int) *Dec {
z.Set(q)
if rA.Sign() != 0 {
z.Unscaled().Add(z.Unscaled(), intSign[rA.Sign()*rB.Sign()+1])
}
return z
}}
func roundHalf(f func(c int, odd uint) (roundUp bool)) func(z, q *Dec, rA, rB *big.Int) *Dec {
return func(z, q *Dec, rA, rB *big.Int) *Dec {
z.Set(q)
brA, brB := rA.BitLen(), rB.BitLen()
if brA < brB-1 {
// brA < brB-1 => |rA| < |rB/2|
return z
}
roundUp := false
srA, srB := rA.Sign(), rB.Sign()
s := srA * srB
if brA == brB-1 {
rA2 := new(big.Int).Lsh(rA, 1)
if s < 0 {
rA2.Neg(rA2)
}
roundUp = f(rA2.Cmp(rB)*srB, z.Unscaled().Bit(0))
} else {
// brA > brB-1 => |rA| > |rB/2|
roundUp = true
}
if roundUp {
z.Unscaled().Add(z.Unscaled(), intSign[s+1])
}
return z
}
}
var roundHalfDown = rounder{true, roundHalf(
func(c int, odd uint) bool {
return c > 0
})}
var roundHalfUp = rounder{true, roundHalf(
func(c int, odd uint) bool {
return c >= 0
})}
var roundHalfEven = rounder{true, roundHalf(
func(c int, odd uint) bool {
return c > 0 || c == 0 && odd == 1
})}
var roundFloor = rounder{true,
func(z, q *Dec, rA, rB *big.Int) *Dec {
z.Set(q)
if rA.Sign()*rB.Sign() < 0 {
z.Unscaled().Add(z.Unscaled(), intSign[0])
}
return z
}}
var roundCeil = rounder{true,
func(z, q *Dec, rA, rB *big.Int) *Dec {
z.Set(q)
if rA.Sign()*rB.Sign() > 0 {
z.Unscaled().Add(z.Unscaled(), intSign[2])
}
return z
}}