Jump to content
  • Advertisement
Sign in to follow this  
freeworld

Best way to parse an integer and what exactly is the mod operator

This topic is 3756 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need a quick way to parse an integer into its separate numbers. I know I can do this the robust way, turn it into a string parse the string and reconvert those back into integers, but I thought I read somewhere that doing 13 % 10, will return the 1 and (13 * 10) % 10 will return the 3 or something like that, but I can't find the post, and I not sure what exactly the '%' operator is doing to get those results?

Share this post


Link to post
Share on other sites
Advertisement
The modulo operator returns the remainder after whole number division:
2 modulo 3 returns 2
26 modulo 13 returns 0
9 module 2 returns 1

Integer division discards the remainder, so you can parse a number with a given radix by applying division and modulo:
143 % 10 → 3
(143 / 10 → 14) % 10 → 4
(14 / 10 → 1) % 10 → 1

That should get you on your way.

Share this post


Link to post
Share on other sites
Quote:
Original post by freeworld
thank you you guys.

curious off the tops of your heads, when else could the '%' operator come in handy?


Finding out if a number is odd or even is a common use.

Share this post


Link to post
Share on other sites
The modulus operator can come in handy for hashing key values in a hash table.

It maps a larger range of number to some smaller number of values, between 0 and some N, where N is the size of the hash table. Read up on hash tables for more information on how this works.

Share this post


Link to post
Share on other sites
Also, if you ever want to 'clamp' an angle to [0-360] or [0-2PI]
540 % 360 = 180
fmod (8.343, M_2_PI) = 2.06 // % doesn't work on floating point use fmod()

Share this post


Link to post
Share on other sites
Quote:
Original post by freeworld
thank you you guys.

curious off the tops of your heads, when else could the '%' operator come in handy?


It's used extensively in cryptography, especially public or asymmetric key encryption. You can use the mod operator to set max and min values of a function (rand() for example) cause the maximum value the mod operator will produce for a given value is 1 minus the divisor.

Share this post


Link to post
Share on other sites
Quote:
Original post by freeworld
thank you you guys.

curious off the tops of your heads, when else could the '%' operator come in handy?


It's used extensively in cryptography, especially public or asymmetric key encryption. You can use the mod operator to set max and min values of a function (rand() for example) cause the maximum value the mod operator will produce for a given value is 1 minus the divisor.

Share this post


Link to post
Share on other sites
Quote:
curious off the tops of your heads, when else could the '%' operator come in handy?


When using rand() (don't forget to seed the random number generator using srand!) to generate a random number, you can use the modulus operator to return a number between 1 and a maximum.

For example, rand() by itself will return a random number governed by whatever RAND_MAX has been defined as (found in stdlib.h). Using rand()%1000 however, would return a random number between 1 and 1000.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!