This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Craftplorer's profile picture

Arithmetic Error

Started by Craftplorer, 25 November 2013 - 05:14 AM
Craftplorer #1
Posted 25 November 2013 - 06:14 AM
My code dont work :(/>



rednet.open("right")
local mapperComID = 20
local x = nil
local y = nil
local z = nil
local curx, cury, curz, dir
function getLocation()
returnVal = nil
while true do
  os.clock(10)
  rednet.send(mapperComID,"Need location")
  myEvent = {os.pullEvent()}
  if myEvent[1] == "rednet_message" and myEvent[2] == mapperComID then
   returnVal = myEvent[3]
   break
  end
end
return returnVal
end
function getPos()
   return gps.locate(3)
end
function getDir()
   local dir, x, y, z
   x, y, z = getPos()
   while not turtle.forward() do
  while not turtle.up() do
    turtle.digUp()
  end
   end
   nx, ny, nz = getPos()
   if (x == nx) then
  if (nz > z) then
    dir = 2
  else
    dir = 0
  end
   else
  if (nx > x) then
	 dir = 3
  else
	 dir = 1
  end
   end
   return dir
end

function setDir(toDir)
   while toDir ~= dir do
  turtle.turnLeft()
  if dir == 3 then
	 dir = 0
  else
	 dir = dir+1
  end
   end
end
function moveX()
   distxx = x - curx
   if (x > curx) then
   setDir(3)
   else
   setDir(1)
   end
   distx = math.abs(distx)
   for i = 1, distx do
  while not turtle.forward() do
    while not turtle.up() do
   turtle.digUp()
    end
  end
   end
end
function moveZ()
   distz = z - curz
   if (z < curz) then
   setDir(0)
   else
   setDir(2)
   end
   distz = math.abs(distz)
   for i = 1, distz do
  while not turtle.forward() do
    while not turtle.up() do
   turtle.digUp()
    end
  end
   end
end
function moveY()
   disty = y - cury
   disty = math.abs(disty)
   if (y < cury) then
  for i = 1, disty do
    while not turtle.down() do
   turtle.digDown()
    end
  end
   else
  for i = 1, disty do
    while not turtle.up() do
   turtle.digUp()
    end
  end
   end
end
function goto()
dir = getDir()
bcurx, bcury, bcurz = gps.locate(3)
curx = tonumber(bcurx)
cury = tonumber(bcury)
curz = tonumber(bcurz)

nx = tonumber(x)
ny = tonumber(y)
nz = tonumber(z)
distx = x - curx
disty = y - cury
distz = z - curz
moveX()
curx, cury, curz = getPos()
moveZ()
curx, cury, curz = getPos()
moveY()
curx, cury, curz = getPos()
end
while true do
if (fs.exists("startup_bak") == false) then
  x,y,z = getLocation()
  goto()
end
shell.run("miner")
end

Error: attempt to perform arithmetic __sub on table and number
Can u help me i start already with trying thing like nx, ny,nz etc. but i dont get it. Where is the error?
LBPHacker #2
Posted 25 November 2013 - 11:06 AM
The error is on the line that the error message shows. Be so kind and provide us the whole error message. Yeah, what I want is that weird number at the beginning of the error message…
Craftplorer #3
Posted 25 November 2013 - 11:14 AM

startup: 129 : attempt to perform arithmetic __sub on table and number
MKlegoman357 #4
Posted 25 November 2013 - 11:22 AM
Line 129:
x,y,z = getLocation()

There is no mathematical operations around here. Could you please upload your full code to pastebin?

EDIT:

Always read and analyze errors you get:

You get this error:
startup: 129 : attempt to perform arithmetic __sub on table and number

What information can we get from it?

  • The program in which that error occurred: startup.
  • The line at which that error occurred: 129.
  • What failed: attempt to perform arithmetic __sub on table and number

We look at that file, in that line. If we don't see any misspellings or errors we continue analyzing the error:

We see a word arithmetic. Basically it means mathematics.

We then see some strange __sub. But what does it mean? What sub could mean in mathematics? sub - is a short for subtract!

So now we know where to look for an error - somewhere where you subtract one number from another (ex.: 5 - 2).

Still don't see the problem? Lets read the rest of the error: on table and number.

Now we know what happened! On line 129, in file called 'startup', we tried to subtract a number from a table!
Edited on 25 November 2013 - 10:34 AM
Craftplorer #5
Posted 25 November 2013 - 11:29 AM
pastebin/0xmkCeJF


		distx = x - curx


EDIT:
Well. I try already to convert so i get number - number but somehow i still get a error. Im new to Lua and dont really understand how the variable handel…
Edited on 25 November 2013 - 10:40 AM
Bomb Bloke #6
Posted 25 November 2013 - 04:54 PM
Pastebin.com has no paste with that ID: http://pastebin.com/0xmkCeJF

Say you make a table:

myTable = {234,345,345,345,2}

There are other ways of defining the things, but that'll straight up give you one where myTable[1] = 234, myTable[2] = 345, and so on.

You can then do things like this:

myTable = {234,345,345,345,2}
x=1000
distx = x - myTable[4]

What you cannot do is this:

distx = x - myTable

This is because although eg myTable[4] is a number, myTable is the table that number is in. You can typically only perform maths on numerals.

What you also cannot do is this:

x = tonumber(myTable)

How can Lua turn a table of numbers into a single number? They're like apples and oranges; two completely different things.

Anyway, your problem is really in your getLocation() function. You have it return like so:

returnVal = myEvent[3]
.
.
.
return returnVal

Best I can make out, the "mapper computer" is sending back a table. Meaning when you pull this:

x,y,z = getLocation()

… x gets set to a table, y and z both get set to nil.

So, instead return it like this:

return myEvent[3][1], myEvent[3][2], myEvent[3][3]

Note that you can have a function return pretty much whenever you like, and that function will stop at that point. You don't have to rig things so returning is done last if there's really nothing more for that function to do.
VaNnOrus #7
Posted 26 November 2013 - 12:20 AM
Im new to Lua and dont really understand how the variable handel…
Why not just read the manual, if you do not know the language?
Craftplorer #8
Posted 26 November 2013 - 04:26 AM
I read it and then i got this error and i dont understand why i get this error. So i write i dont understand how they handel variable caus i didnt get the error.

I also found the error i forgot to rename x,y,z to nx,ny,nz. So a very stupit error. And the rednet message was wrong.

nx = tonumber(x)
ny = tonumber(y)
nz = tonumber(z)
distx = x - curx
disty = y - cury
distz = z - curz

btw: sorry for my bad english im from germany

EDIT: Thank you Bomb Bloke i rewrote everything in a better way.
Edited on 26 November 2013 - 03:28 AM
VaNnOrus #9
Posted 26 November 2013 - 09:07 AM
I also found the error i forgot to rename x,y,z to nx,ny,nz. So a very stupit error.
It often happens it's not scary. Need to be more attentive :)/>