%

Started by
8 comments, last by Exellon 21 years, 8 months ago
What is it? What''s it used for? No matter how much stuff I read about it and stuff, it just doesn''t make sense to me.
Advertisement
Modulos. Say, if I have a the number 3 and I put 3%2, the output would be one. Think of it this way, it's basically, 3/2, BUT instead of using the normal answer, the answer is the difference. For example, if there was 5%4, the answer would be one.

Say, for example, if there was an asteroids game, and the y axis was 4, and the guy suddenly went to 5 on the y axis, using a modulo, he'd appear at one instead of going off the screen.

5%4=1 See?

[edited by - Andrew Nguyen on August 12, 2002 12:11:54 AM]
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
Assuming you mean the % operator in C++. It is the modulus operator ( or in english the remainder operator ). It will return the remainder. So for example:


  int six = 6;int four = 4;int remain = six % four;// remain is now 2 since 6/4 = 1 Remainder 2  


--------
Andrew
thanks. it seems like there may be other uses for it too, like, for instance, what would this do?

(rand() % 360)
rand() % 360... it would return a number, but the number would NEVER be over 360 because modulos don't allow that

[edited by - Andrew Nguyen on August 12, 2002 12:15:42 AM]
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
Exellon:

(rand()%360) would give you a random integer between 0 and 359. Here's a quick table to help you understand:
           n  |  n % 6          -------------           0  |    0  <-  0/6 = 0, with a remainder of 0           1  |    1      1/6 = 0, with a remainder of 1           2  |    2      2/6 = 0,  "   "    "       " 2           3  |    3                    .           4  |    4                    .           5  |    5                    .           6  |    0  <-  6/6 = 1, with a remainder of 0           7  |    1 

The important thing to remember is that this is integer division, not floating point.

John.

[edited by - JohnAD on August 13, 2002 1:09:17 AM]

[edited by - JohnAD on August 13, 2002 1:09:48 AM]
yeah except its bad to use modulo in rand
---DirectX gives me a headache.
"yeah except its bad to use modulo in rand "

Why?
Especially when you''re mod-ing by a power of two (%256, for instance), you''re simply masking out the higher bits and using the lower ones, which are less random. It''s not quite as bad for mod-ing by other numbers, but still not a good idea. The correct way would be: (rand () * 360) / RAND_MAX

Kippesoep
Modulos are neat, but they are slow. In cases where the modulo is a power of two - like 256, a bit mask can be used instead
x & 0xFF - 0xFF is 255 ~~ x % 256
This works because of the properties of binary numbers - but Kippesoep''s division approach is probably better given the power of today''s cpus.
"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