@Valgo Do you mean you want to use tables as numbers (for example 405
is {4,0,5}
) and you want to be able to add two of these tables together like {3,0,8} + {4,0,5} == {7,1,3}
? If so you can add tables together with metatables so this is possible to make.
Edit: I mean that tables can be added, multiplied, compared, divided, subtracted, ect. with metatables, not just added.
@Valgo Here’s a routine to multiple 2 strings of numbers. The strings are converted to tables and then multiplied together. I tried it with 2 strings of 2600 digits each. I’m not sure if there’s a limit other than time.
function setup()
a="2112357909875357864211356809766864234689976"
b="4664458987522345790875422312234680097656777"
str=mult(a,b)
print(str)
end
function mult(str1,str2)
local r,n1,n2={},{},{}
for z=1,string.len(str1) do
table.insert(n1,tonumber(string.sub(str1,z,z)))
table.insert(r,0)
end
for z=1,string.len(str2) do
table.insert(n2,tonumber(string.sub(str2,z,z)))
table.insert(r,0)
end
for x=#n2,1,-1 do
local carry=0
for y=#n1,1,-1 do
local v=r[x+y]+n2[x]*n1[y]+carry
r[x+y]=v%10
carry=math.floor(v/10)
end
r[x]=carry
end
if r[1]==0 then
table.remove(r,1)
end
return(table.concat(r))
end