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?

For example:

c%16

What would be the result of this equastion?

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?

For example:

c%16

What would be the result of this equastion?

jkd

04-13-2003, 11:09 PM

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

12 % 5 = 2

6 % 3 = 0

21 % 8 = 5

etc

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?

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

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

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.

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 © 2017 vBulletin Solutions, Inc. All rights reserved.