|
Wicc Cunningham
Registered User
Join date: 25 Jun 2004
Posts: 52
|
07-28-2006 13:33
I'm horrible with math. I want to be able to know if a for loop is on an even or odd increment. I say even or odd because I'm not sure which is easier to determine. I didn't see anything in the math functions that looked like it would work for this. I had thought about doing a bitwise comparison, but wasn't sure about that method.
Any help is appreciated.
|
|
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
|
07-28-2006 13:41
Well, an even number is divisible by 2, so when you divide by 2, the remainder should be 0. The % operator tells you the remainder of an integer division, so take the loop counter, find the remainder after dividing by 2, and if the remainder is 0, it's even. for (i = start; i < end; i++) { if ((i % 2) == 0) // Or ... if (!(i % 2)) { // It's even } else { // It's odd } }
Or you could keep another variable that you toggle each time. Let's say you know your loop starts from an even number: even = TRUE; for (i = start; i < end; i++) { if (even) { // Do even stuff } else { // Do odd stuff }
even = !even; }
|
|
Wicc Cunningham
Registered User
Join date: 25 Jun 2004
Posts: 52
|
07-31-2006 07:45
Thanks Ziggy, I think the remainder method will work for me. 
|
|
Laukosargas Svarog
Angel ?
Join date: 18 Aug 2004
Posts: 1,304
|
07-31-2006 08:46
// this alternative might be slightly less work for the interpretor ... for ( i = start; i < end; i = i + 1 ) { if ( i & 1 ) { // odd } else { // even } }
tip: In LSL it used to be the case that x = x+1 executes faster than x++ whether this is still true I'm not sure but it probably is. Using & for the odd/even test is probably faster than %, again I've not actually tested it but I'm reasonably certain.
_____________________
Geometry is music frozen...
|
|
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
|
07-31-2006 09:30
That's good advice 
|