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.
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]
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]
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:
--------
Andrew
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)
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]
[edited by - Andrew Nguyen on August 12, 2002 12:15:42 AM]
Exellon:
(rand()%360) would give you a random integer between 0 and 359. Here's a quick table to help you understand:
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]
(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]
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
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement