Jump to content




Math stuff


  • You cannot reply to this topic
7 replies to this topic

#1 grand_mind1

  • Members
  • 207 posts

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

#2 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

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.

#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 07 February 2013 - 03:09 PM

View PostBubba, on 07 February 2013 - 01:32 PM, said:

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

#4 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 07 February 2013 - 03:26 PM

View PostTheOriginalBIT, on 07 February 2013 - 03:09 PM, said:

View PostBubba, on 07 February 2013 - 01:32 PM, said:

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

#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 07 February 2013 - 03:29 PM

View PostPharap, on 07 February 2013 - 03:26 PM, said:

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

#6 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 07 February 2013 - 03:42 PM

View PostTheOriginalBIT, on 07 February 2013 - 03:29 PM, said:

View PostPharap, on 07 February 2013 - 03:26 PM, said:

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.

Quote

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.


#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 07 February 2013 - 03:46 PM

View PostBubba, on 07 February 2013 - 03:42 PM, said:

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

#8 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 09 February 2013 - 12:58 PM

View PostTheOriginalBIT, on 07 February 2013 - 03:29 PM, said:

View PostPharap, on 07 February 2013 - 03:26 PM, said:

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users