Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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?
    Shawn

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    It's the modulus operator. It essentially returns the remainder after performing a division.

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

    etc

  • #3
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks!

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

  • #4
    Regular Coder
    Join Date
    Sep 2002
    Location
    Louisiana
    Posts
    576
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    New Coder
    Join Date
    Mar 2003
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”


  •  

    Posting Permissions

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