-
Notifications
You must be signed in to change notification settings - Fork 19
/
int.go
323 lines (271 loc) · 5.8 KB
/
int.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package num
import (
"math/big"
"github.com/holiman/uint256"
)
var intZero = NewInt(0)
// Int a wrapper to a signed big int.
type Int struct {
// The unsigned version of the integer
U *Uint
// The sign of the integer true = positive, false = negative
s bool
}
func IntFromUint(u *Uint, s bool) *Int {
return &Int{
s: s,
U: u.Clone(),
}
}
func IntToString(u *Int) string {
if u != nil {
return u.String()
}
return "0"
}
// IntFromString creates a new Int from a string
// interpreted using the give base.
// A big.Int is used to read the string, so
// all error related to big.Int parsing applied here.
// will return true if an error happened.
func IntFromString(str string, base int) (*Int, bool) {
b, ok := big.NewInt(0).SetString(str, base)
if !ok {
return NewInt(0), true
}
return IntFromBig(b)
}
// IntFromBig construct a new Int with a big.Int
// returns true if overflow happened.
func IntFromBig(b *big.Int) (*Int, bool) {
positive := true
if b.Sign() < 0 {
b.Neg(b)
positive = false
}
u, overflow := uint256.FromBig(b)
if overflow {
return NewInt(0), true
}
return &Int{
U: &Uint{*u},
s: positive,
}, false
}
// IntFromDecimal returns the Int part of a decimal.
func IntFromDecimal(d Decimal) (*Int, bool) {
dd := d
// if its negative it'll overflow so need to negate before going to Uint
if d.IsNegative() {
dd = d.Neg()
}
u, overflow := dd.Uint()
return &Int{
U: &Uint{*u},
s: d.IsPositive(),
}, overflow
}
// IsNegative tests if the stored value is negative
// true if < 0
// false if >= 0.
func (i *Int) IsNegative() bool {
return !i.s && !i.U.IsZero()
}
// IsPositive tests if the stored value is positive
// true if > 0
// false if <= 0.
func (i *Int) IsPositive() bool {
return i.s && !i.U.IsZero()
}
// IsZero tests if the stored value is zero
// true if == 0.
func (i *Int) IsZero() bool {
return i.U.IsZero()
}
// FlipSign changes the sign of the number from - to + and back again.
func (i *Int) FlipSign() {
i.s = !i.s
}
// Clone creates a copy of the object so nothing is shared.
func (i Int) Clone() *Int {
return &Int{
U: i.U.Clone(),
s: i.s,
}
}
func (i Int) EQ(o *Int) bool {
return i.s == o.s && i.U.EQ(o.U)
}
// GT returns if i > o.
func (i Int) GT(o *Int) bool {
if i.IsNegative() {
if o.IsPositive() || o.IsZero() {
return false
}
return i.U.LT(o.U)
}
if i.IsPositive() {
if o.IsZero() || o.IsNegative() {
return true
}
return i.U.GT(o.U)
}
return o.IsNegative()
}
func (i Int) GTE(o *Int) bool {
return i.GT(o) || i.EQ(o)
}
// LT returns if i < o.
func (i Int) LT(o *Int) bool {
if i.IsNegative() {
if o.IsPositive() || o.IsZero() {
return true
}
return i.U.GT(o.U)
}
if i.IsPositive() {
if o.IsZero() || o.IsNegative() {
return false
}
return i.U.LT(o.U)
}
return o.IsPositive()
}
func (i Int) LTE(o *Int) bool {
return i.LT(o) || i.EQ(o)
}
func (i Int) Int64() int64 {
val := int64(i.U.Uint64())
if i.IsNegative() {
return -val
}
return val
}
// String returns a string version of the number.
func (i Int) String() string {
val := i.U.String()
if i.IsNegative() {
return "-" + val
}
return val
}
// Add will add the passed in value to the base value
// i = i + a.
func (i *Int) Add(a *Int) *Int {
// Handle cases where we have a zero
if a.IsZero() {
return i
}
if i.IsZero() {
i.U.Set(a.U)
i.s = a.s
return i
}
// Handle the easy cases were both are the same sign
if i.IsPositive() && a.IsPositive() {
i.U.Add(i.U, a.U)
return i
}
if i.IsNegative() && a.IsNegative() {
i.U.Add(i.U, a.U)
return i
}
// Now the cases where the signs are different
if i.IsNegative() {
if i.U.GTE(a.U) {
// abs(i) >= a
i.U.Sub(i.U, a.U)
} else {
// abs(i) < a
i.U.Sub(a.U, i.U)
i.s = true
}
return i
}
if i.U.GTE(a.U) {
// i >= abs(a)
i.U.Sub(i.U, a.U)
} else {
// i < abs(a)
i.U.Sub(a.U, i.U)
i.s = false
}
return i
}
// Sub will subtract the passed in value from the base value
// i = i - a.
func (i *Int) Sub(a *Int) *Int {
a.FlipSign()
i.Add(a)
a.FlipSign()
return i
}
// AddSum adds all of the parameters to i
// i = i + a + b + c.
func (i *Int) AddSum(vals ...*Int) *Int {
for _, x := range vals {
i.Add(x)
}
return i
}
// SubSum subtracts all of the parameters from i
// i = i - a - b - c.
func (i *Int) SubSum(vals ...*Int) *Int {
for _, x := range vals {
i.Sub(x)
}
return i
}
// Mul will multiply the passed in value to the base value
// i = i * m.
func (i *Int) Mul(m *Int) *Int {
i.U.Mul(i.U, m.U)
i.s = i.s == m.s
return i
}
// Mul will divide the passed in value to the base value
// i = i / m.
func (i *Int) Div(m *Int) *Int {
i.U.Div(i.U, m.U)
i.s = i.s == m.s
return i
}
// NewInt creates a new Int with the value of the
// int64 passed as a parameter.
func NewInt(val int64) *Int {
if val < 0 {
return &Int{
U: NewUint(uint64(-val)),
s: false,
}
}
return &Int{
U: NewUint(uint64(val)),
s: true,
}
}
func IntZero() *Int {
return intZero.Clone()
}
// NewIntFromUint creates a new Int with the value of the
// uint passed as a parameter.
func NewIntFromUint(val *Uint) *Int {
return &Int{
U: val,
s: true,
}
}