View Full Version : What does %something mean in the Math object?
shlagish
04-13-2003, 10:48 PM
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
shlagish
04-14-2003, 03:53 AM
Thanks!
That's kind of odd though don't you think?
what could that be used for?
chrismiceli
04-14-2003, 05:08 AM
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
eggman
04-14-2003, 04:42 PM
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
beetle
04-14-2003, 04:55 PM
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.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.