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

Calculating Refuel function/ removing remainders expression

Started by Rhinehart_, 01 October 2012 - 05:34 AM
Rhinehart_ #1
Posted 01 October 2012 - 07:34 AM
hello im Rhinehart_ im new to Lua and programming in general and like any newbie i started a project and then halfway realized i may have bit off more than i should fit in my mouth, i started a multiple turtle quarry project and ive nearly finished the program but i havent done the automatic refueling,

how i would like the refueling process to work is that when i first run the program the turtle calculates how much fuel it will need to complete the task, (i already have the turtle ask how far forward and how deep to go so the calculations would be simple i.e (length *depth)) so i define a line 20 long and 30 deep and it calculates
20 * 30 = 600
so it knows that it will need a minimum of 600 fuel, and then divide that number by 96, 144, 192 depending on which fuel i decide to use (probably charcoal so 96) and then it will prompt "i need X amount of Fuel" the problem is that i can take the 600 and divide by 96 or whatever fuel value but i dont know how to remove the remainder of it for example:
600/96 = 6 Remainder 24
im not certain if this is possible in lua but it doesnt really make sense for their not to be a command to do so,
im open to other ideas of how to calculate the amount of fuel needed, but i would prefer not to have to have it check its fuel level and then come back and get more when it needs fuel like in the excavate program, its just plain wasteful,

thank you for taking the time to read this post :)/>/>
Orwell #2
Posted 01 October 2012 - 07:47 AM
This function gives you both the result of an integer division and the remainder of that division.


local function intDiv(x,y)
  return math.floor(x/y), x%y
end

print( intDiv(600,96) == 6, 24 )
MrBarry #3
Posted 01 October 2012 - 07:55 AM
math.ceil(x/y) would probably work better for this application, since it rounds up to the nearest integer. If you need 5.2 fuel, then you'd better pack 6.
Orwell #4
Posted 01 October 2012 - 08:00 AM
math.ceil(x/y) would probably work better for this application, since it rounds up to the nearest integer. If you need 5.2 fuel, then you'd better pack 6.

Indeed, in this specific case it would be more suitable. I just gave the code for a correct integer division. : )
Rhinehart_ #5
Posted 01 October 2012 - 05:43 PM
ok i think ill use the math.ceil(var1/var2) the first expression gave me the remainder instead of removing it,

Thanks again! :)/>/>
Orwell #6
Posted 02 October 2012 - 12:06 AM
ok i think ill use the math.ceil(var1/var2) the first expression gave me the remainder instead of removing it,

Thanks again! :)/>/>

The first 'expression' (function) gave you both… But anyways, glad you've been helped