Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[This API is solely built for performing with
- numbers beyond normal levels (where floating-point
- precision errors, the thing that so happens to be
- the cause of the Far Lands begin to occur). It is
- not advised to use this for small math problems.]]
- --[[All math is done in tables, so it is
- impossible to work with normal numbers.]]
- --[[giantmath.convert() turns normal numbers to
- table numbers.]]
- function convert(n)
- ntable = {}
- d = math.floor(math.log10(n)) + 1
- for i = 1, d do
- set = d-(i-1)
- enzyme = (n % 10^(set) - n % 10^(set-1)) / 10^(set-1)
- ntable[i] = enzyme
- end
- return ntable
- end
- --[[giantmath.move() is essential for keeping the
- table system stable.]]
- function move(ntable)
- if ntable[0] ~= nil then
- table.insert(ntable, 0, "nil")
- end
- if ntable[1] == 0 then
- repeat
- table.remove(ntable, 1)
- until ntable[1] ~= 0
- end
- return ntable
- end
- --[[giantmath.tablen() is used to check the amount
- of variables in a table.]]
- function tablen(ntable)
- local i = 0
- while true do
- local i = i + 1
- if ntable[i] == nil then
- return i-1
- end
- end
- end
- --[[giantmath.check() is required for checking
- carry-outs and borrows.]]
- function check(ntable)
- --Carryout
- local i = 0
- repeat
- i = i + 1
- local itst = tablen(ntable) - (i - 1)
- if ntable[itst] >= 10 then
- local ndivide = math.floor(ntable[itst] / 10)
- local nmod = ntable[itst] % 10
- ntable[itst] = nmod
- ntable[itst-1] = ntable[itst-1] + ndivide
- move(ntable)
- --Can also be used for transforming whole
- --numbers into segmented table numbers
- end
- until i == tablen(ntable)
- --Borrow
- for i = 1, tablen(ntable) do
- itst = tablen(ntable) - (i - 1)
- if ntable[itst] < 0 then
- ntable[itst-1] = ntable[itst-1] - 1
- ntable[itst] = ntable[itst] + 10
- end
- end
- return ntable
- end
- --[[giantmath.align() is a base function used to
- align two tables to be prepared for
- manipulation.]]
- function align(ntable1, ntable2)
- local x = tablen(ntable1)
- local y = tablen(ntable2)
- local state = 0
- if x == y then
- state = 0
- elseif x > y then
- state = 1
- elseif x < y then
- state = 2
- end
- local count = x - y
- if state == 1 then
- repeat
- table.insert(ntable2, 1, "0")
- until ntable2[x] ~= nil
- elseif state == 2 then
- repeat
- table.insert(ntable1, 1, "0")
- until ntable1[y] ~= nil
- elseif state == 0 then
- return ntable1, ntable2
- end
- return ntable1, ntable2
- end
- --[[giantmath.add() well... adds up two GiantMath
- numbers.]]
- function add(num, num2)
- local num, num2 = align(num, num2)
- local numb = tablen(num)
- for i = 1, numb do
- num3[i] = num1[i] + num2[i]
- print("2")
- end
- check(num3)
- return num3
- end
- num = {4, 3, 2, 1}
- num2 = {2, 3, 9, 6}
- num3 = add(num, num2)
- for i = 1, 4 do
- write(num3[i])
- end
Advertisement
Add Comment
Please, Sign In to add comment