Guest User

giantmath

a guest
Mar 3rd, 2014
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.82 KB | None | 0 0
  1. --[[This API is solely built for performing with
  2. numbers beyond normal levels (where floating-point
  3. precision errors, the thing that so happens to be
  4. the cause of the Far Lands begin to occur). It is
  5. not advised to use this for small math problems.]]
  6.  
  7. --[[All math is done in tables, so it is
  8. impossible to work with normal numbers.]]
  9.  
  10. --[[giantmath.convert() turns normal numbers to
  11. table numbers.]]
  12.  
  13. function convert(n)
  14.  
  15.   ntable = {}
  16.   d = math.floor(math.log10(n)) + 1
  17.   for i = 1, d do
  18.     set = d-(i-1)
  19.     enzyme = (n % 10^(set) - n % 10^(set-1)) / 10^(set-1)
  20.     ntable[i] = enzyme
  21.   end
  22.   return ntable
  23.  
  24. end
  25.    
  26.  
  27. --[[giantmath.move() is essential for keeping the
  28. table system stable.]]
  29.  
  30. function move(ntable)
  31.  
  32.   if ntable[0] ~= nil then
  33.     table.insert(ntable, 0, "nil")
  34.   end
  35.  
  36.   if ntable[1] == 0 then
  37.     repeat
  38.       table.remove(ntable, 1)
  39.     until ntable[1] ~= 0
  40.   end
  41.   return ntable
  42. end
  43.        
  44. --[[giantmath.tablen() is used to check the amount
  45. of variables in a table.]]
  46.  
  47. function tablen(ntable)
  48.   local i = 0
  49.   while true do
  50.     i = i + 1
  51.     if ntable[i] == nil then
  52.       return i-1
  53.     end
  54.   end
  55. end
  56.  
  57. --[[giantmath.check() is required for checking
  58. carry-outs and borrows.]]
  59.  
  60. function check(ntable)
  61.  
  62.   --Carryout
  63.   local i = 0
  64.   repeat
  65.     i = i + 1
  66.     local itst = tablen(ntable) - (i - 1)
  67.     if ntable[itst] >= 10 then
  68.      
  69.       local ndivide = math.floor(ntable[itst] / 10)
  70.       local nmod = ntable[itst] % 10
  71.      
  72.       ntable[itst] = nmod
  73.       ntable[itst-1] = ntable[itst-1] + ndivide
  74.       move(ntable)
  75.      
  76.       --Can also be used for transforming whole
  77.       --numbers into segmented table numbers
  78.     end
  79.   until i == tablen(ntable)
  80.  
  81.   --Borrow
  82.   for i = 1, tablen(ntable) do
  83.     itst = tablen(ntable) - (i - 1)
  84.     if ntable[itst] < 0 then
  85.      
  86.       ntable[itst-1] = ntable[itst-1] - 1
  87.       ntable[itst] = ntable[itst] + 10
  88.    
  89.     end
  90.   end
  91.   return ntable
  92. end
  93.  
  94. --[[giantmath.align() is a base function used to
  95. align two tables to be prepared for
  96. manipulation.]]
  97.  
  98. function align(ntable1, ntable2)
  99.  
  100.   local x = tablen(ntable1)
  101.   local y = tablen(ntable2)
  102.   local state = 0
  103.  
  104.   if x == y then
  105.     state = 0
  106.   elseif x > y then
  107.     state = 1
  108.   elseif x < y then
  109.     state = 2
  110.   end
  111.  
  112.   local count = x - y
  113.   if state == 1 then
  114.     repeat
  115.       table.insert(ntable2, 1, "0")
  116.     until ntable2[x] ~= nil
  117.   elseif state == 2 then
  118.     repeat
  119.       table.insert(ntable1, 1, "0")
  120.     until ntable1[y] ~= nil
  121.   elseif state == 0 then
  122.     return ntable1, ntable2
  123.   end
  124.   return ntable1, ntable2
  125. end        
  126.  
  127.        
  128. --[[giantmath.add() well... adds up two GiantMath
  129. numbers.]]
  130.    
  131. function add(num, num2)
  132.  
  133.   local num, num2 = align(num, num2)
  134.   local numb = tablen(num)
  135.   num3 = {}
  136.  
  137.   for i = 1, numb do
  138.     num3[i] = num[i] + num2[i]
  139.   end
  140.   check(num3)
  141.   return num3
  142. end
  143.  
  144. function sub(num, num2)
  145.  
  146.   local num, num2 = align(num, num2)
  147.   local numb = tablen(num)
  148.   num3 = {}
  149.  
  150.   for i = 1, numb do
  151.     num3[i] = num[i] - num2[i]
  152.   end
  153.   check(num3)
  154.   return num3
  155. end
  156.  
  157. --giantmath.mul() multiplies.
  158.  
  159. function mul(num, num2)  
  160.   num3 = {}
  161.   for i1 = 1, tablen(num2) do
  162.     calc = {}
  163.     --Multiplication
  164.     for i21 = 1, tablen(num) do
  165.       calc[i21] = num[i21] * num2[i1]
  166.       print(i21)
  167.       check(calc)
  168.     end
  169.    
  170.     --Appending
  171.     app = tablen(calc)
  172.     for i22 = 1, tablen(num2)-i1 do
  173.       calc[app+i22] = 0
  174.     end
  175.    
  176.     --Adding to num3
  177.     if num3[1] == nil then
  178.       local dig = tablen(calc)
  179.       for i23 = 1, dig do
  180.         num3[i23] = calc[i23]
  181.       end
  182.     else
  183.       num3 = add(calc, num3)
  184.     end
  185.   end
  186.   return(num3)
  187. end
  188.  
  189. num = convert(4321)
  190. num2 = convert(2396)
  191. num3 = mul(num, num2)
  192.  
  193. for i = 1, 8 do
  194.   write(num3[i])
  195. end
Advertisement
Add Comment
Please, Sign In to add comment