Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if statement in code is not working

Status
Not open for further replies.

momon

Vendor
May 22, 2002
42
0
0
CA
Hi everyone.

This is a very easy question ( or so it seems ).

Here is a short piece of code:

#include <stdio.h>

int main() {

int Phase = 3;
double kva = 1000;

if( kva > (1000 * (Phase / 3.0)) )
printf(&quot;greater than&quot;);
else
printf(&quot;less than&quot;);

return 1;
}

I am compiling this program under Borland C++ 5.02.

The funny thing is that a trace of the program reveals the expression ( kva > (1000 * (Phase / 3.0)) ) to be false. Yet the resulting output is &quot;greater than&quot;.

Can anyone explain why?

Thanks
 
Your code executes as expected for me (I get &quot;less than&quot;) with hpux-cc.
 
haha... i love when this kind of thing happens. thanks for running the code.
 
hi,
why do you not declare a var as

double x ;

x=1000 * (Phase / 3.0) ;

PRINT x
and see if is <=> to 1000

Compilator is not a man that knows fractions; I'ld prefere write :

x = ( (double)Phase * 1000.0 ) / 3.0

(probably you'll obtain more floating precision)

bye
 
hi.
thanks for reading the post victorv.

The compiler does know about fractions and type conversions in expressions.

For example,
int / int results in an int. e.g. 1/3 = 0
double / int results in a double e.g. 1.0/3 = 0.333...
int / double also results in double e.g. 1/3.0 = 0.333...

but to be safe we can always cast Phase to a double as you did.

anyone else have any ideas what is causing this problem?
 
hi,

if you use an intermediate var x

x = ( (double)Phase * 1000.0 ) / 3.0

PRINT or debug it here, an then make the test

if( kva >= x )
printf(&quot;greater or equal than&quot;);
else
printf(&quot;less than&quot;);


which is the result ?


bye
 
The result is &quot;greater than or equal&quot;. However, it is in fact equal since replacing the condition ( kva >= x ) with ( kva > x ) results in &quot;less than&quot; being printed.

So the use of an intermediate variable does solve the problem. Thanks.

So to sum up.

The following code:

int main() {

int Phase = 3;
double kva = 1000, x;

x = 1000 * Phase/3.0;

if( kva > x )
printf(&quot;greater than&quot;);
else
printf(&quot;less than or equal&quot;);

return 1;
}

returns the correct result of &quot;less than or equal&quot;.

However, the following logically identical code

int main() {

int Phase = 3;
double kva = 1000;

if( kva > (1000 * Phase/3.0) )
printf(&quot;greater than&quot;);
else
printf(&quot;less than or equal&quot;);

return 1;
}

returns &quot;greater than&quot;.

In conclusion: There is something seriously wrong.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top