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

Started by
17 comments, last by dmatter 15 years, 9 months ago
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.
Advertisement
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.
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.
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
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.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

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.
Ra
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.
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.
Ra
Sorry dmatter, i didn't read the comments (I rarely do, too often comments refer to code that doesn't exist anymore).
No worries, I was just a bit confuzzled that's all [smile]

It does highlight that I may have not chosen quite the best example, but no matter.

This topic is closed to new replies.

Advertisement