Sign in to follow this  

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

This topic is 3477 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
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
thank you you guys.

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

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:
[i]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
Quote:
Original post by DarkHorizon
Using rand()%1000 however, would return a random number between 1 and 1000.

Keep in mind, though, that can hurt the distribution and random-ness. You'd be best off scaling the range [0, RAND_MAX] to [0, N], if N happens to be small then the modulo operation can be have satisfactory results however.

Quote:
Original post by freeworld
curious off the tops of your heads, when else could the '%' operator come in handy?


If you wanted to simulate continuous space that wraps around the screen - like in Asteriods - then you could use the modulo operator to make entities appear on the opposite side of the screen whenever they fly off the edge.

Ex:

ship_x = (ship_x + velocity_x * time) % screen_width;
ship_y = (ship_y + velocity_y * time) % screen_height;

// N.B doesn't handle what happens if ship_x or ship_y are less than zero.

Share this post


Link to post
Share on other sites
Quote:
Original post by dmatter
ship_x = (ship_x + velocity_x * time) % screen_width;

That doesn't work if the ships cross the left edge of the screen, because a negative number modulo a positive one is a negative one.

Share this post


Link to post
Share on other sites
It would be correct if the modulo operator was correct for two's-complement integers. The mathematical definition of modulo is not the same as the C, C++, or Java definitions of modulo on signed integers.

Traditional mathematical and C/C++/Java unsigned integers: a % b = x where b*y + x = a for some integer y, where 0 <= x < b.

C/C++/Java for signed integers: a % b = x*s where b*y + x*s = a for some integer y, where 0 <= x < b and s is -1 if a < 0, +1 if a >= 0, IIRC.

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Quote:
Original post by dmatter
ship_x = (ship_x + velocity_x * time) % screen_width;

That doesn't work if the ships cross the left edge of the screen, because a negative number modulo a positive one is a negative one.


In several languages the result is not defined. For instance, in the current C++ standard it is not defined but it is defined in C# (the result will have the same sign as the dividend). In Lua the result has the same sign as the divisor. So it's not portable across different languages and it isn't even portable across different compiles in the same language in some cases. See this article on Wikipedia.

Share this post


Link to post
Share on other sites
Quote:
Original post by nobodynews
Quote:
Original post by DevFred
Quote:
Original post by dmatter
ship_x = (ship_x + velocity_x * time) % screen_width;

That doesn't work if the ships cross the left edge of the screen, because a negative number modulo a positive one is a negative one.


In several languages the result is not defined. For instance, in the current C++ standard it is not defined but it is defined in C# (the result will have the same sign as the dividend). ...

To clarify: in C++ this is not undefined, it's implementation-defined.

Share this post


Link to post
Share on other sites
Um, are you all just making re-affirmations for the OP or did you miss my comment?:

Quote:
Original post by dmatter
// N.B doesn't handle what happens if ship_x or ship_y are less than zero.

Share this post


Link to post
Share on other sites
Quote:
Original post by dmatter
Um, are you all just making re-affirmations for the OP or did you miss my comment?:

Quote:
Original post by dmatter
// N.B doesn't handle what happens if ship_x or ship_y are less than zero.

I believe we're correcting DevFred.

Share this post


Link to post
Share on other sites
Sorry dmatter, i didn't read the comments (I rarely do, too often comments refer to code that doesn't exist anymore).

Share this post


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