Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-2

what's the difference between / and %?

Asked by 9 years ago

Seriously,

I am really confused...

Help me out here. :/ please

1 answer

Log in to vote
1
Answered by
yumtaste 476 Moderation Voter
9 years ago

/ means division. You should know what division is. 5/2 = 2.5.

However, % means modulus. Modulus is like division, but it returns the remainder (thank you Perci1 for pointing out my mistake). If you did 5 % 2, you would get 1. You could then do 5 - 1 = 4, 4/2 = 2, and that's how many times 2 goes into 5 entirely. Here are more examples:

11 % 5 = 1 12 % 6 = 0 14 % 6 = 2 20 % 15 = 5

0
That's not right -- modulus returns the REAMINDER, so 5%2=1. A quick print in studio shows this. Also take a look at the examples on the wiki: 11 % 3 = 2, 23 % 4 = 3. It gives you whatever's left over after dividing. Perci1 4988 — 9y
0
Hm. I tested it in Lua online and it returned 2. Maybe something went wrong? I'll check again: thanks for the heads up. yumtaste 476 — 9y
Ad

Answer this question