• Create Account

### #ActualKhatharr

Posted 29 April 2013 - 01:12 AM

In the first case the 'if' relies on the fact that the only "true" condition is when something is zero.

'true' is typically nonzero, isn't it? In c/c++ it would be "if(!count) {stuff}".
#include <iostream>

int main() {
//print multiples of 5 up to 100
for(int n = 1; n <= 100; ++n) {
if(!(n % 5)) {std::cout << n << std::endl;}
}
}

Is zero true in java or something?

Also, addressing the general topic, if you're wanting both the quotient and the remainder then check to see if there's an intrinsic that can give you both results without having to do the division twice. When the CPU does integer division it actually produces both results, regardless of whether you asked for division or modulus. It's not a huge issue, but division is the heaviest integer math operation for the cpu by a wide margin, so in anything where performance matters it can be good to know if there's an intrinsic. (Don't prematurely optimize, but don't prematurely pessimize either.)

Edit: Apparently there's one in std::div, which is convenient, but poking around for a minute I see that it's common for modern compilers to optimize this problem away in most cases.

### #3Khatharr

Posted 29 April 2013 - 01:06 AM

In the first case the 'if' relies on the fact that the only "true" condition is when something is zero.

'true' is typically nonzero, isn't it? In c/c++ it would be "if(!count) {stuff}".
#include <iostream>

int main() {
//print multiples of 5 up to 100
for(int n = 1; n <= 100; ++n) {
if(!(n % 5)) {std::cout << n << std::endl;}
}
}

Is zero true in java or something?

Also, addressing the general topic, if you're wanting both the quotient and the remainder then check to see if there's an intrinsic that can give you both results without having to do the division twice. When the CPU does integer division it actually produces both results, regardless of whether you asked for division or modulus. It's not a huge issue, but division is the heaviest integer math operation for the cpu by a wide margin, so in anything where performance matters it can be good to know if there's an intrinsic. (Don't prematurely optimize, but don't prematurely pessimize either.)

### #2Khatharr

Posted 29 April 2013 - 12:55 AM

In the first case the 'if' relies on the fact that the only "true" condition is when something is zero.

'true' is typically nonzero, isn't it? In c/c++ it would be "if(!count) {stuff}".
#include <iostream>

int main() {
//print multiples of 5 up to 100
for(int n = 1; n <= 100; ++n) {
if(!(n % 5)) {std::cout << n << std::endl;}
}
}

Is zero true in java or something?

### #1Khatharr

Posted 29 April 2013 - 12:46 AM

Some practical everyday uses:

// I want to trigger something every 7 executions:
static int count = 0;
count = ++count % 7;
if( count ) {do something}

// I want to cycle 0-6 repeatedly.
static int count = 0;
count = ++count % 7;

In the first case the 'if' relies on the fact that the only "true" condition is when something is zero.  In the second case a value modded by any higher value results in the same value, it goes to zero when modded with itself.  Basically a variable in both cases is going to step from 0 to mod value minus one, or 0-6 in this case and keep repeating.

I think I use the first case with the 'if' statement more often but I know I use the second case on occasion also.

'true' is typically nonzero, isn't it? In c/c++ it would be "if(!count) {stuff}".

Is zero true in java or something?

PARTNERS