View Full Version : What does %something mean in the Math object?
04-13-2003, 11:48 PM
What does %something mean in the Math object
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
04-14-2003, 04:53 AM
That's kind of odd though don't you think?
what could that be used for?
04-14-2003, 06:08 AM
it could be used for many porpuses, it could find if a number is odd or even
in c it is used to shorten a random number, like Math.round();
test = Math.random();
test %= 4;
//test is now 3 digits long
04-14-2003, 05: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:
04-14-2003, 05: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.
Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.