-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharithmetic.go
More file actions
336 lines (291 loc) · 8.43 KB
/
arithmetic.go
File metadata and controls
336 lines (291 loc) · 8.43 KB
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
package dec128
import "github.com/jokruger/dec128/state"
// Add returns the sum of the Dec128 and the other Dec128.
// If any of the Dec128 is NaN, the result will be NaN.
// In case of overflow, the result will be NaN.
func (d Dec128) Add(other Dec128) Dec128 {
// Return immediately if either value is in an error state.
switch {
case d.state >= state.Error:
return d
case other.state >= state.Error:
return other
}
// Try a fast-path add on the non‑canonical forms.
if r, ok := d.tryAdd(other); ok {
return r
}
// Canonicalize both values and try again.
if r, ok := d.Canonical().tryAdd(other.Canonical()); ok {
return r
}
// If addition could not be performed without overflow, return an overflow Dec128.
return Dec128{state: state.Overflow}
}
// AddInt returns the sum of the Dec128 and the int.
// If Dec128 is NaN, the result will be NaN.
// In case of overflow, the result will be NaN.
func (d Dec128) AddInt(other int) Dec128 {
return d.AddInt64(int64(other))
}
// AddInt64 returns the sum of the Dec128 and the int.
// If Dec128 is NaN, the result will be NaN.
// In case of overflow, the result will be NaN.
func (d Dec128) AddInt64(other int64) Dec128 {
return d.Add(FromInt64(other))
}
// Sub returns the difference of the Dec128 and the other Dec128.
// If any of the Dec128 is NaN, the result will be NaN.
// In case of overflow/underflow, the result will be NaN.
func (d Dec128) Sub(other Dec128) Dec128 {
// Return immediately if either value is in an error state.
switch {
case d.state >= state.Error:
return d
case other.state >= state.Error:
return other
}
// Try a fast-path sub on the non‑canonical forms.
if r, ok := d.trySub(other); ok {
return r
}
// Canonicalize both values and try again.
if r, ok := d.Canonical().trySub(other.Canonical()); ok {
return r
}
// If subtraction could not be performed without overflow, return an overflow Dec128.
return Dec128{state: state.Overflow}
}
// SubInt returns the difference of the Dec128 and the int.
// If Dec128 is NaN, the result will be NaN.
// In case of overflow/underflow, the result will be NaN.
func (d Dec128) SubInt(other int) Dec128 {
return d.SubInt64(int64(other))
}
// SubInt64 returns the difference of the Dec128 and the int.
// If Dec128 is NaN, the result will be NaN.
// In case of overflow/underflow, the result will be NaN.
func (d Dec128) SubInt64(other int64) Dec128 {
return d.Sub(FromInt64(other))
}
// Mul returns d * other.
// If any of the Dec128 is NaN, the result will be NaN.
// In case of overflow, the result will be NaN.
func (d Dec128) Mul(other Dec128) Dec128 {
switch {
case d.state >= state.Error:
return d
case other.state >= state.Error:
return other
case d.coef.IsZero() || other.coef.IsZero():
return Zero
}
r, ok := d.tryMul(other)
if ok {
return r
}
// Fallback is unreachable; retained only for future changes.
//a := d.Canonical()
//b := other.Canonical()
//r, ok = a.tryMul(b)
//if ok {
// return r
//}
return Dec128{state: state.Overflow}
}
// MulInt returns d * other.
// If Dec128 is NaN, the result will be NaN.
// In case of overflow, the result will be NaN.
func (d Dec128) MulInt(other int) Dec128 {
return d.MulInt64(int64(other))
}
// MulInt64 returns d * other.
// If Dec128 is NaN, the result will be NaN.
// In case of overflow, the result will be NaN.
func (d Dec128) MulInt64(other int64) Dec128 {
return d.Mul(FromInt64(other))
}
// Div returns d / other.
// If any of the Dec128 is NaN, the result will be NaN.
// In case of overflow, underflow, or division by zero, the result will be NaN.
func (d Dec128) Div(other Dec128) Dec128 {
switch {
case d.state >= state.Error:
return d
case other.state >= state.Error:
return other
case other.coef.IsZero():
return Dec128{state: state.DivisionByZero}
case d.coef.IsZero():
return Zero
}
r, ok := d.tryDiv(other)
if ok {
return r
}
a := d.Canonical()
b := other.Canonical()
r, ok = a.tryDiv(b)
if ok {
return r
}
return Dec128{state: state.Overflow}
}
// DivInt returns d / other.
// If Dec128 is NaN, the result will be NaN.
// In case of overflow, underflow, or division by zero, the result will be NaN.
func (d Dec128) DivInt(other int) Dec128 {
return d.DivInt64(int64(other))
}
// DivInt64 returns d / other.
// If Dec128 is NaN, the result will be NaN.
// In case of overflow, underflow, or division by zero, the result will be NaN.
func (d Dec128) DivInt64(other int64) Dec128 {
return d.Div(FromInt64(other))
}
// Mod returns d % other.
// If any of the Dec128 is NaN, the result will be NaN.
// In case of overflow, underflow, or division by zero, the result will be NaN.
func (d Dec128) Mod(other Dec128) Dec128 {
switch {
case d.state >= state.Error:
return d
case other.state >= state.Error:
return other
case other.coef.IsZero():
return Dec128{state: state.DivisionByZero}
case d.coef.IsZero():
return Zero
}
_, r, ok := d.tryQuoRem(other)
if ok {
return r
}
a := d.Canonical()
b := other.Canonical()
_, r, ok = a.tryQuoRem(b)
if ok {
return r
}
return Dec128{state: state.Overflow}
}
// ModInt returns d % other.
// If Dec128 is NaN, the result will be NaN.
// In case of overflow, underflow, or division by zero, the result will be NaN.
func (d Dec128) ModInt(other int) Dec128 {
return d.ModInt64(int64(other))
}
// ModInt64 returns d % other.
// If Dec128 is NaN, the result will be NaN.
// In case of overflow, underflow, or division by zero, the result will be NaN.
func (d Dec128) ModInt64(other int64) Dec128 {
return d.Mod(FromInt64(other))
}
// QuoRem returns the quotient and remainder of the division of Dec128 by other Dec128.
// If any of the Dec128 is NaN, the result will be NaN.
// In case of overflow, underflow, or division by zero, the result will be NaN.
func (d Dec128) QuoRem(other Dec128) (Dec128, Dec128) {
switch {
case d.state >= state.Error:
return d, d
case other.state >= state.Error:
return other, other
case other.coef.IsZero():
return Dec128{state: state.DivisionByZero}, Dec128{state: state.DivisionByZero}
case d.coef.IsZero():
return Zero, Zero
}
q, r, ok := d.tryQuoRem(other)
if ok {
return q, r
}
a := d.Canonical()
b := other.Canonical()
q, r, ok = a.tryQuoRem(b)
if ok {
return q, r
}
return Dec128{state: state.Overflow}, Dec128{state: state.Overflow}
}
// QuoRemInt returns the quotient and remainder of the division of Dec128 by int.
// If Dec128 is NaN, the result will be NaN.
// In case of overflow, underflow, or division by zero, the result will be NaN.
func (d Dec128) QuoRemInt(other int) (Dec128, Dec128) {
return d.QuoRemInt64(int64(other))
}
// QuoRemInt64 returns the quotient and remainder of the division of Dec128 by int.
// If Dec128 is NaN, the result will be NaN.
// In case of overflow, underflow, or division by zero, the result will be NaN.
func (d Dec128) QuoRemInt64(other int64) (Dec128, Dec128) {
return d.QuoRem(FromInt64(other))
}
// Abs returns |d|
// If Dec128 is NaN, the result will be NaN.
func (d Dec128) Abs() Dec128 {
if d.state >= state.Error {
return d
}
return Dec128{coef: d.coef, scale: d.scale}
}
// Neg returns -d
// If Dec128 is NaN, the result will be NaN.
func (d Dec128) Neg() Dec128 {
switch {
case d.state >= state.Error:
return d
case d.state == state.Neg:
return Dec128{coef: d.coef, scale: d.scale}
case d.coef.IsZero():
return d
default:
return Dec128{coef: d.coef, scale: d.scale, state: state.Neg}
}
}
// Sqrt returns the square root of the Dec128.
// If Dec128 is NaN, the result will be NaN.
// If Dec128 is negative, the result will be NaN.
// In case of overflow, the result will be NaN.
func (d Dec128) Sqrt() Dec128 {
switch {
case d.state >= state.Error:
return d
case d.coef.IsZero():
return Zero
case d.state == state.Neg:
return Dec128{state: state.SqrtNegative}
case d.Equal(One):
return One
}
r, ok := d.trySqrt()
if ok {
return r
}
// Fallback is unreachable; retained only for future changes.
//a := d.Canonical()
//r, ok = a.trySqrt()
//if ok {
// return r
//}
return Dec128{state: state.Overflow}
}
// PowInt returns Dec128 raised to the power of n.
func (d Dec128) PowInt(n int) Dec128 {
return d.PowInt64(int64(n))
}
// PowInt64 returns Dec128 raised to the power of n.
func (d Dec128) PowInt64(n int64) Dec128 {
switch {
case d.state >= state.Error:
return d
case n < 0:
return One.Div(d.PowInt64(-n))
case n == 0:
return One
case n == 1:
return d
case (n & 1) == 0:
return d.Mul(d).PowInt64(n / 2)
default:
return d.Mul(d).PowInt64((n - 1) / 2).Mul(d)
}
}