forked from MathIsFun0/Talisman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbignumber.lua
354 lines (290 loc) · 7.48 KB
/
bignumber.lua
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
-- class table
Big = {
m = 0,
e = 0
}
-- metatable
BigMeta = {}
BigMeta.__index = Big
--- Create a new Big number
--
-- numbers are stored in the form `m * 10 ^ e`
function Big:new(m, e)
if type(m) == "table" then
return setmetatable({m = m.m, e = m.e}, BigMeta):normalized()
end
if e == nil then e = 0 end
if type(m) == "string" then
return Big.parse(m)
end
return setmetatable({m = m, e = e}, BigMeta):normalized()
end
function Big:normalize()
if self.m == 0 then
self.e = 0
else
local n_log = math.floor(math.log(math.abs(self.m), 10))
self.m = self.m / 10 ^ n_log
self.e = self.e + n_log
self.e = math.floor(self.e)
end
end
function Big:normalized()
if (tostring(self.e) == "nan" or tostring(self.e) == "inf") then
self.m = 0/0
self.e = 10^1000
return self
end
self:normalize()
return self
end
function Big:add(b)
if type(b) == "number" then b = Big:new(b) end
local delta = b.e - self.e
if delta > 14 then return b end
if delta < -14 then return self end
return Big:new(self.m + b.m * 10 ^ delta, self.e):normalized()
end
function BigMeta.__add(b1, b2)
if type(b1) == "number" then return Big:new(b1) + b2 end
return b1:add(b2)
end
function Big:sub(b)
if type(b) == "number" then b = Big:new(b) end
local nb = Big:new(b.m * -1, b.e) --negate b
return self:add(nb)
end
function BigMeta.__sub(b1, b2)
if type(b1) == "number" then return Big:new(b1) - b2 end
return b1:sub(b2)
end
function Big:mul(b)
if type(b) == "number" then b = Big:new(b) end
return Big:new(self.m * b.m, self.e + b.e):normalized()
end
function BigMeta.__mul(b1, b2)
if type(b1) == "number" then return Big:new(b1) * b2 end
return b1:mul(b2)
end
function Big:div(b)
if type(b) == "number" then b = Big:new(b) end
return Big:new(self.m / b.m, self.e - b.e):normalized()
end
function BigMeta.__div(b1, b2)
if type(b1) == "number" then return Big:new(b1) / b2 end
return b1:div(b2)
end
function Big:mod(other)
other = Big:create(other)
if (other:eq(R.ZERO)) then
Big:create(R.ZERO)
end
if (self.sign*other.sign == -1) then
return self:abs():mod(other:abs()):neg()
end
if (self.sign==-1) then
return self:abs():mod(other:abs())
end
return self:sub(self:div(other):floor():mul(other))
end
function Big:negate()
return self:mul(Big:new(-1))
end
function BigMeta.__unm(b1)
return b1:negate()
end
function Big:log10()
-- The default value of 0 is to make Balatro happy...
if self.e == nan and self.m == nan then return 0 end
if self:lte(Big:new(0)) then return 0 end
return self.e + math.log(self.m, 10)
end
function Big:log(base)
-- The default value of 0 is to make Balatro happy...
if self.e == nan and self.m == nan then return 0 end
if self:lte(Big:new(0)) then return 0 end
return self:log10() / math.log(base, 10)
end
function Big:ln()
return self:log(math.exp(1))
end
function Big:ld()
return self:log(2)
end
function Big:pow(pow)
-- faster than self:eq(Big:new(0))
if self.m == 0 and self.e == 0 then
return Big:new(0)
end
local log = self:log10()
local new_log = log * pow
return Big:new(10 ^ (new_log % 1), math.floor(new_log)):normalized()
end
function BigMeta.__pow(b1, n)
if type(n) == "table" then n = n:to_number() end
if type(b1) ~= "table" then b1 = Big:new(b1) end
return b1:pow(n)
end
function Big.exp(n)
return Big:new(math.exp(1)):pow(n)
end
function Big:recp()
return self:pow(-1)
end
function Big:sqrt()
return self:pow(0.5)
end
function Big:cbrt()
return self:pow(1 / 3)
end
function Big:round()
if self.e > 100 then return self end
local num = self:to_number()
if num % 1 < 0.5 then
return Big:new(math.floor(num))
else
return Big:new(math.ceil(num))
end
end
function Big:floor()
if self.e > 100 then return self end
return Big:new(math.floor(self:to_number()))
end
function Big:ceil()
if self.e > 100 then return self end
return Big:new(math.ceil(self:to_number()))
end
function Big:floor_m(digits)
if self.e > 100 then return self end
return Big:new(math.floor(self.m * 10 ^ digits) / 10 ^ digits, self.e)
end
function Big:ceil_m(digits)
if self.e > 100 then return self end
return Big:new(math.ceil(self.m * 10 ^ digits) / 10 ^ digits, self.e)
end
function Big:sin()
return Big:new(math.sin(self:to_number()))
end
function Big:asin()
return Big:new(math.asin(self:to_number()))
end
function Big:cos()
return Big:new(math.cos(self:to_number()))
end
function Big:acos()
return Big:new(math.acos(self:to_number()))
end
function Big:tan()
return Big:new(math.tan(self:to_number()))
end
function Big:is_positive()
return self.m >= 0
end
function Big:is_negative()
return self.m < 0
end
function Big:is_naneinf()
return tostring(self.m) == "nan" and (tostring(self.e) == "nan" or tostring(self.e) == "inf")
end
function Big:compare(b)
b = Big:new(b)
if self.m == b.m and self.e == b.e then
return 0
end
if self:is_positive() and b:is_negative() then
return 1
end
if self:is_negative() and b:is_positive() then
return -1
end
if self.e > b.e then return 1 end
if self.e < b.e then return -1 end
if self:is_positive() and self.m > b.m then return 1 end
if self:is_positive() and self.m < b.m then return -1 end
if self:is_negative() and self.m > b.m then return -1 end
if self:is_negative() and self.m < b.m then return 1 end
end
function Big:gt(b)
return self:compare(b) == 1
end
function Big:gte(b)
return self:compare(b) >= 0
end
function Big:lt(b)
return self:compare(b) == -1
end
function BigMeta.__lt(b1, b2)
if b2:is_naneinf() then
return true
end
if b1:is_naneinf() then
return false
end
return b1:lt(b2)
end
function Big:lte(b)
return self:compare(b) <= 0
end
function BigMeta.__le(b1, b2)
if b2:is_naneinf() then
return true
end
if b1:is_naneinf() then
return false
end
return b1:lte(b2)
end
function Big:gt(b)
return self:compare(b) == 1
end
function Big:eq(b)
return self:compare(b) == 0
end
function BigMeta.__eq(b1, b2)
return b1:eq(b2)
end
function Big:neq(b)
return self:compare(b) ~= 0
end
function Big:to_string()
return self.m.."e"..self.e
end
function Big:to_number()
return self.m * 10 ^ self.e
end
function BigMeta.__tostring(b)
return number_format(b)
end
function Big.parse(str)
local to_n = tonumber(str)
if to_n ~= nil and to_n < math.huge then
return Big:new(to_n):normalized()
end
local parts = {}
for m, e in str:gmatch("(.+)e(.+)") do
parts = {m, e}
end
return Big:new(tonumber(parts[1]), math.floor(tonumber(parts[2]))):normalized()
end
--Adding things OmegaNum has that this doesn't...
R = {}
R.ZERO = 0
R.ONE = 1
R.E = math.exp(1)
R.LN2 = math.log(2, R.E)
R.LN10 = math.log(10, R.E)
R.LOG2E = math.log(R.E, 2)
R.LOG10E = math.log(R.E, 0)
R.PI = math.pi
R.SQRT1_2 = math.sqrt(0.5)
R.SQRT2 = math.sqrt(2)
R.MAX_SAFE_INTEGER=9007199254740991
R.MIN_SAFE_INTEGER=-9007199254740992
R.MAX_DISP_INTEGER=1000000
R.NaN=0/0
R.NEGATIVE_INFINITY = -1/0
R.POSITIVE_INFINITY = 1/0
R.E_MAX_SAFE_INTEGER="e"..tostring(R.MAX_SAFE_INTEGER)
R.EE_MAX_SAFE_INTEGER="ee"..tostring(R.MAX_SAFE_INTEGER)
R.TETRATED_MAX_SAFE_INTEGER="10^^"..tostring(R.MAX_SAFE_INTEGER)
return Big