Re: i can't understand behaviour of if statement

From: Chris Torek (nospam_at_torek.net)
Date: 03/28/04


Date: 28 Mar 2004 17:51:33 GMT

In article <news:7f1e3c62.0403280850.77dca66a@posting.google.com>
Paula <pcoelho@usp.br> writes:
>printf("z= %f, alfa= %f",&z,&alfafe);
>if (z == 0.008) {printf("==== here z"); <...>}
>if (alfafe == 0.5) {printf("**** here alfa"); <...>}

Others have already answered the most likely source of the problem
(floating point rounding -- incidentally, you *might* have more
success using 0.008f instead of 0.008, but this is still not a good
idea in general).

I will add that this is probably *not* the actual code you are
using, because the unary "&" operators in the printf() call are
not appropriate here and would most likely have resulted in bizarre
values for "z" and "alfa". (It also seems a bit odd to name the
variable "alfafe" in the code and "alfa" in the printf text, but
this, at least, is not something that would produce bizarre behavior.)

Bugs can be caused by niggly little single-character typos, so it
is often unwise to post something other than an actual program.
Of course, if the program is huge, posting it unchanged may *also*
be unwise. :-) ("... advice is a dangerous gift, even from the
wise to the wise, and all courses may run ill.") In this case,
probably there was no loss, but in general, posting a small program
that shows the problem, or a cut-and-paste -- as opposed to retyping
-- of the code that exhibits the problem, is usually your best bet.

-- 
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: forget about it   http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.


Relevant Pages

  • Re: Responding to a challenge
    ... Mr. Wise was not attributing statements to Mr. Zobby, ... >> out in my response to his first posting. ... > Where is the misinterpretation? ...
    (sci.lang)
  • Re: diabetic friendly shoes?
    ... Just because you have been lucky so far does not make your actions ... wise nor does it mean that you won't regret your actions in time. ... President, or that we are to stand by the President ... If you don't know how to ignore a posting, ...
    (alt.support.diabetes)
  • Re: "generic VB.Net response" and the like
    ... it might be wise to defuse that ammo before anyone could use it again. ... were somehow "unworthy" because of posting here. ... But IMO we have not properly considered the *whole* audience, ... just the presumable "vast majority". ...
    (microsoft.public.vb.general.discussion)
  • Re: Newbie Q: Fraction to double
    ... >> unwise to go on to pass it to fgets. ... without understanding the theory of finite state automata, ... Chris Torek, Wind River Systems ... Reading email is like searching for food in the garbage, ...
    (comp.lang.c)
  • Re: Custom Quota Message
    ... It's wise to note however that this is not supported by Microsoft. ... Mark Fugatt ... This posting is provided "AS IS" with no warranties, ...
    (microsoft.public.exchange2000.admin)