Strange bool output

Jun 25, 2011 at 10:00am
Hi Guys,

I'm going nuts trying to figure this out, any help much appreciated. I have the following comparison, all data types are double:

cout<<(d->getLow(0)==llv->getLlv(j,d,0) && rsi->getRsi(j,d,0)<5)<<endl;

The above returns bool values that do not confirm the data.

When I output each piece of data individually such as:

cout<<d->getLow(0)<<" "<<llv->getLlv(j,d,0)<<" "<<rsi->getRsi(j,d,0)<<endl;

I see that the comparison should yield the correct bool output. Instead I get output that makes no sense.

If I instead initialize seperate variables and do the comparison such as:

double low(d->getLow(0));
double llv(llv->getLlv(j,d,0));
double rsi(rsi->getRsi(j,d,0));

cout<<(low==llv && rsi<5)<<endl;

I get the correct bool results.

Also if I do a bitwise comparison such as:

cout<<(d->getLow(0)==llv->getLlv(j,d,0) & rsi->getRsi(j,d,0)<5)<<endl;

the output is correct. Surely bitwise bool and logical bool should render the same results.

I've been through my code a number of times and can't make sense of this.

Anyone have any ideas as to why this would happen. Thanks in advance!
Jun 25, 2011 at 11:03am
Interesting, could you post the results of each test and what you expected from them?
Jun 25, 2011 at 1:42pm
Surely bitwise bool and logical bool should render the same results.

Unfortunately, no. Bitwise operators don't do short-circuit evaluation like logical operators do, so you make be having a short-circuit evaluation problem. How? I don't know, my best guess would be if your code depended on rsi->getRsi() being called...does it?

Of course, I'm assuming that floating point inaccuracies aren't the problem...
Last edited on Jun 25, 2011 at 1:43pm
Jun 27, 2011 at 6:13am
Thanks LBEaston for your response:

Test1:

cout<<d->getLow(0)<<" "<<llv->getLlv(j,d,0)<<" "<<rsi->getRsi(j,d,0)<<endl;

Output: 85.23 85.23 3.65

Test2:

cout<<(d->getLow(0)==llv->getLlv(j,d,0))<< " "<<(rsi->getRsi(j,d,0)<5)<<endl;

Output: 1 1

Test 3 - logical and:

cout<<(d->getLow(0)==llv->getLlv(j,d,0) && rsi->getRsi(j,d,0)<5)<<endl;

Output 0

Test 4 - bitwise and:

cout<<(d->getLow(0)==llv->getLlv(j,d,0) & rsi->getRsi(j,d,0)<5)<<endl;

Output 1

Test 5 - idividual initialization:

double low(d->getLow(0));
double llv(llv->getLlv(j,d,0));
double rsi(rsi->getRsi(j,d,0));

cout<<(low==llv && rsi<5)<<endl;

Output 1

These results are the same across thousands of entries. Logical "and" seems to return the correct bool result in test 2, but the result for bool when combining the two individual tests in test 3 returns false?? When individually initialized as in test 5 the result is correct.


Last edited on Jun 27, 2011 at 6:25am
Jun 27, 2011 at 6:22am
Thanks L B for the response.

Of course, I'm assuming that floating point inaccuracies aren't the problem...

Don't think there are any problems there. The data is read from the same data file for all comparisons, meaning the same data is being compared.

my best guess would be if your code depended on rsi->getRsi() being called...does it?

If by that you mean the ouput depends on the function rsi->getRsi() being called, then that would be correct. I have displayed a number of test results in my reply to LBEaston above. Perhaps that will clarify.
Jun 27, 2011 at 9:34am
LB, it seems even though I was reading from the same file floating point inaccuracies did creep in. Weird since I was using the same function to format the data in both calls. I have found a solution. Thanks for your help guys!
Topic archived. No new replies allowed.