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

Math stuff

Started by grand_mind1, 07 February 2013 - 12:16 PM
grand_mind1 #1
Posted 07 February 2013 - 01:16 PM
So this is a pretty basic question that probably has a very basic answer. I would just like to to know how I can check if a number is divisible by another number. Very short and quick question…
Help is appreciated!
Thanks! :D/>
Bubba #2
Posted 07 February 2013 - 01:32 PM
Use the modulus operator (%)

Example: if 8%4==0 then print("It is divisible") else print("It is not divisible") end

What this operator does is return the remainder of a division. So for example, 4%3 will return 1/3.
theoriginalbit #3
Posted 07 February 2013 - 03:09 PM
So for example, 4%3 will return 1/3.
Uhhh 4%3 will return 1 … the math: 4 / 3 = 1 remainder 1. the only time 1/3 will be returned is if you do 3.33%3…
Pharap #4
Posted 07 February 2013 - 03:26 PM
So for example, 4%3 will return 1/3.
Uhhh 4%3 will return 1 … the math: 4 / 3 = 1 remainder 1. the only time 1/3 will be returned is if you do 3.33%3…
More precisely any time the integer components divide together into 1 (eg 5%5.33, 6%6.33).
theoriginalbit #5
Posted 07 February 2013 - 03:29 PM
eg 5%5.33, 6%6.33
test them, they return 5 and 6, not 0.33. The left hand integer needs to be the number on the left + 0.33 … thats the only way to get a remainder of 0.33
Bubba #6
Posted 07 February 2013 - 03:42 PM
eg 5%5.33, 6%6.33
test them, they return 5 and 6, not 0.33. The left hand integer needs to be the number on the left + 0.33 … thats the only way to get a remainder of 0.33

Yeah my apologies. I was doing that on my phone and didn't test them or really think it through at all. The principle still applies though. Here is the definition from the Lua Reference 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.
theoriginalbit #7
Posted 07 February 2013 - 03:46 PM
Yeah my apologies. I was doing that on my phone and didn't test them or really think it through at all.
It's ok we all make mistakes from time to time (esp when we are on our phones) ;)/>
Pharap #8
Posted 09 February 2013 - 12:58 PM
eg 5%5.33, 6%6.33
test them, they return 5 and 6, not 0.33. The left hand integer needs to be the number on the left + 0.33 … thats the only way to get a remainder of 0.33
Point taken, to be fair, it was 2am at the time lol
At least it shows some of the great uses of modulus.