shlagish
04132003, 11:48 PM
What does %something mean in the Math object
For example:
c%16
What would be the result of this equastion?
For example:
c%16
What would be the result of this equastion?


View Full Version : What does %something mean in the Math object? shlagish 04132003, 11:48 PM What does %something mean in the Math object For example: c%16 What would be the result of this equastion? jkd 04142003, 12:09 AM 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 04142003, 04:53 AM Thanks! That's kind of odd though don't you think? what could that be used for? chrismiceli 04142003, 06: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 04142003, 05:42 PM One use is to keep loop variables in a certain range. If I wanted to keep a variable within the range 09, for example: counter=++counter%10 beetle 04142003, 05:55 PM Or for determining a leapyear var isLeap = ( year % 400 == 0 ) ? true : ( year % 4 == 0 && year % 100 != 0 ); I've also used it for creditcard checksums, which use mod 10. 
 
EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum