%?

Started by
8 comments, last by UeberBobo 20 years, 6 months ago
what do i use the % operator(?) for? i think its something to do with checking if a division leaves a rest or something. what are the uses of this?
//---------//Ueberbobo//*********
Advertisement
% = MOD => MOD(a,b)= a%b = a-(int(a/b)*b) = rest


T2k
Example when you have a variable containing the time in seconds and want to output it to the user as minutes and seconds.
time_t totalTimeInSeconds = 143;time_t timeInMinutes = totalTimeInSeconds / 60;time_t timeInSeconds = totalTimeInSeconds % 60;


[How To Ask Questions|STL Programmer''s Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
See http://www.gamedev.net/community/forums/topic.asp?topic_id=185261.

As it happens, the % operator does the exact same thing in C++ and Java.
it''s the modulus <sp?> operator. the most common use is for creating a loop out of an endless stream of increasing numbers:

int i = 0;while (1) {    std::cout << i%3 << "\n";    i++;} 


that code will print:
0,1,2,0,1,2,0,1,2,0,1,2.....

a more practical example is for use in generating random numbers. if you want a number between 0 -> sumNumber, you call:

rand() % someNumber;

-me
Thats the modulus operator, and you are correct that it gives you the remainder of a division equation. I find it very, very useful! I used it in a very cool instance while working on my current project which is a drop block style game. I was trying to come up with an algorithm to blit the bitmaps for my gameboard, which is a grid of spaces that I represented with an array of 187 integers. In order to determine the x coordinate of where to begin my blitting operations for each square I simply took the number of spaces (187) and took the modulus to see how many spaces in I needed to start my blitting. here''s a little snippet of the code to see if you can make some sense out of it:
	SetRect(&rectSource, 0,0,640,480);	SetRect(&rectDest, 0,0,640,480);	pGfxEngine->BlitOffScreenSurface(pGfxEngine->lpDDS_FPBG,					rectSource, rectDest);	int x, y;	for (int i = 0; i<187; i++)	{	x = 183+((i%11)*25);	y = 28+(424 -(((i/11)*25)+24));         ....blitting here        } 


Hope that wasnt too confusing :-p Maybe some one could come up with a simpler example.
Yep, it basically just gives the remainder of a division.

I used it in a couple calculators I made way back in the day to convert decimal to hex and binary, although I''m sure that there''s probably a function in the c files that will do that.
You can also use modulus to prove theorems such as "2 divides 5^n + 1, for all natural n". But that is obviously not programming...
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
If you're finding A % B and B is a power of 2, it's much faster to say A & (B-1). EDIT: Faster in terms of execution time, I mean.

~CGameProgrammer( );

-- Post screenshots of your projects. 100+ posts already in the archives.

[edited by - CGameProgrammer on October 24, 2003 11:26:04 PM]
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Another use. Let''s say you have an array storing thousands of numbers that you want to print in a loop. If you print them one after the other without line breaks, the page will zoom off to the right. If you add line breaks after each, the page will stretch downward. Instead it would be nice to print the numbers in a block, with a line break after printing every Nth element of the array. A modulus can help you do that.

for ( int i = 0; i < length; i++) {
printf("%d ", i);
if ( !(i + 1 % N) )
printf("\n");
}

Two things to note - the % in the printf is not a modulus. In that situation the % symbol is used as a formatting escape marker for printf.

In the condition "if ( !(i + 1 % N) )", 1 is added to i to prevent taking the modulus of 0, which is always 0. What that condition says is that if the modulus is 0, that is, if there is no remainder, then print the newline. The boolean negation is needed to change 0 in to true. If 1 isn''t added to i, then a newline will be printed after the first element.

Another use. Say you have a ticker of some kind - a clock that ''beats'' at a constant rate and you want to perform some routine every 10th tick or every 8th tick or every Nth tick. You can use a modulus to check if it''s the Nth tick and if so then call the "Nth tick" routine.

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement