Pandemonium
Lijsterbeslaan 3
B-2940 Stabroek, Belgium
IBAN: BE78 9794 3692 5286
BIC: ARSPBE22XXX
KBO: 0314.595.348
VAT: BE 0 881.023.185
request blrootsales@pandemonium.be
ID:  Pass:    [ Not logged in. ]

 C++ Modulus 
 Vengenze
Joined 2007-09-20 21:01:01.
(3778 Days old)

C++ Modulus

Posted on: 2011-02-09 12:10:29 
IP: Logged 
Let's start off with a good tip for all C++ coders out there?

Don't try to take the modulus of a negative number lol.

Someone else explain why, I'm busy :P
  Showing entry "C++ Modulus".
Pandemonium 2009

 Remainder 
 Krasula
Joined 2007-05-14 20:59:48.
(3907 Days old)

Remainder

Posted on: 2011-02-09 09:08:09 
IP: Logged 
 There is no modulus operator in C/C++ as I recall.

There is only the "remainder" (%) which is "sort of like a modulus" but not quite.

True enough though, it's not to be used on negative numbers, it just returns the negative value you fed it.

Off the top of my head I think it's easiest to keep wrapping it until you get a positive value to work with.

int x=-20;
int y=x % 200;
// y is now -20

while (x<0) x+=200;
y=x % 200;
// y is now 180

int x=-20;
int y=x % 200;
// y is now 180


  Showing entry "Remainder".
Pandemonium 2009

 C++ Modulus 
 Dirk Myers
Joined 2007-07-22 21:00:26.
(3838 Days old)

C++ Modulus

Posted on: 2011-02-11 11:28:30 
IP: Logged 
Yup wrapping works best for me too, although it isn't necessarily mathematically correct.

Very useful when dealing with 2D buffer iterations when you want to prevent overflows/segfaults.
  Showing entry "C++ Modulus".
Pandemonium 2009


These pages are the intellectual property of the Pandemonium crew.
Welcome to Pandemonium.be - Antwerp based development studio providing development and administration services. Developers and maintainers of the Pyromasaur 3D framework.
Secure
"