Jump to content




Arithmetic Error


8 replies to this topic

#1 Craftplorer

  • Members
  • 6 posts

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?

#2 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

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...

#3 Craftplorer

  • Members
  • 6 posts

Posted 25 November 2013 - 11:14 AM

startup: 129 : attempt to perform arithmetic __sub on table and number


#4 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

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 by MKlegoman357, 25 November 2013 - 11:34 AM.


#5 Craftplorer

  • Members
  • 6 posts

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 by Craftplorer, 25 November 2013 - 11:40 AM.


#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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.

#7 VaNnOrus

  • Members
  • 13 posts

Posted 26 November 2013 - 12:20 AM

View PostCraftplorer, on 25 November 2013 - 11:29 AM, said:

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?

#8 Craftplorer

  • Members
  • 6 posts

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 by Craftplorer, 26 November 2013 - 04:28 AM.


#9 VaNnOrus

  • Members
  • 13 posts

Posted 26 November 2013 - 09:07 AM

View PostCraftplorer, on 26 November 2013 - 04:26 AM, said:

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 :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users