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

Question - Lua - Dividing two numbers and doing something if whole number

Started by Parmacoy, 01 July 2012 - 10:34 AM
Parmacoy #1
Posted 01 July 2012 - 12:34 PM
Hey, im trying to write a fairly basic mining turtle program which allows me to set how many blocks it mines and how many rows it should do. I'm stuck on an issue as I can't seem to find the code I am after anywhere. What I am trying to do is

I have to variables, one is static, the other changes

len=len+1 (example)
x=5

what I am trying to do is, every block the turtle moves, len will increase by 1, and I want to divide the two like so
torch=len/x

and if the resulting number that is returned is a multiple of 5 and is a whole number, i want it to place a torch, and if it isnt, it will loop through the turtles mining commands again till it will.

The only long way i found to be able to do this at the moment is to use code like so

if len == 5 or len == 10 or len == 15 or len == 20 etc etc etc

I want to simplify it, so if anyone can help, that would be great :P/>/>
Xtansia #2
Posted 01 July 2012 - 02:27 PM

--Mining Loop ....
torch = len / x

if torch - math.floor(torch) == 0 and torch % 5 == 0 then --Check if torch is a whole number and torch is a multiple of 5
    --Place Torch
end 

--Mining Loop ....
Lyqyd #3
Posted 01 July 2012 - 06:18 PM
Really?


if len % 5 == 0 then
    --place torch
end
Parmacoy #4
Posted 01 July 2012 - 10:30 PM
Thanks people, much appreciated, but what does the "%" mean in the lua, what does it signify?
Lyqyd #5
Posted 01 July 2012 - 10:51 PM
The % is the modulus operator, which gives the remainder of the division of two numbers.
MysticT #6
Posted 01 July 2012 - 10:53 PM
It's the modulo operator. From the lua manual:
Modulo is defined as
a % b == a - math.floor(a / b ) * b

That is, it is the remainder of a division that rounds the quotient towards minus infinity.