Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Thread: What does %something mean in the Math object?

1. What does %something mean in the Math object?

What does %something mean in the Math object

For example:

c%16

What would be the result of this equastion?

• It's the modulus operator. It essentially returns the remainder after performing a division.

12 % 5 = 2
6 % 3 = 0
21 % 8 = 5

etc

• Thanks!

That's kind of odd though don't you think?
what could that be used for?

• it could be used for many porpuses, it could find if a number is odd or even

if(num%2==1) {
//odd
}
if(num%2==0) {
//even
}
else
//odd

in c it is used to shorten a random number, like Math.round();

test = Math.random();
test %= 4;
//test is now 3 digits long

• One use is to keep loop variables in a certain range. If I wanted to keep a variable within the range 0-9, for example:

counter=++counter%10

• Or for determining a leap-year

var isLeap = ( year % 400 == 0 ) ? true : ( year % 4 == 0 && year % 100 != 0 );

I've also used it for credit-card checksums, which use mod 10.

•

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•