# problem with float

Printable View

• 10-02-2002
aqua
problem with float
could somebody explain why the following code gives "invalid" as the output?

// code

float w=2.3;
if(w==2.3)
puts("valid");
else
puts("invalid");

//********

when I tried the same with double datatype, it worked!!!
It also worked when I changed the if statement to

if(w==2.3f)
puts("valid");
else
puts("invalid");
• 10-03-2002
Salem
> float w=2.3;
> if(w==2.3)
Because what you have here is

if ( (double)w == 2.3 )
The conversion from float to double introduces a minute error (compared to the result of calculating 2.3 as a double from scratch), resulting in an inequality

Your other two experiments did not have an implicit cast from float to double.

Rule: NEVER compare floats/doubles for equality

See here
In particular, the David Goldberg article down the page
• 10-03-2002
master5001
Salem is right on the money here. You should also try and use floats sparingly. There are a few exceptions but overall it is safe to say that if want something to run quickly on even old computers floats can take a toll on speed.
• 10-03-2002
Prelude
>it is safe to say that if want something to run quickly on even old computers floats can take a toll on speed.
Bzzzzt! Wrong answer. ;) This used to be true for older computers, but now math co-processors are pretty much on every new machine. So floating point math can sometimes be faster than integer math. Forget about performance until there is a real need and just use what best suits the problem. But be aware of the problems that floating point calculations can create.

-Prelude
• 10-03-2002
Dave_Sinkula
It may be better to avoid testing floating point values for equality and instead check against a suitably small tolerance.

http://www.eskimo.com/~scs/C-faq/q14.5.html
• 10-03-2002
Salem
> Bzzzzt! Wrong answer. This used to be true for older computers
And is still true for a number of processors which are either RISC architecture, or aimed at the embedded market.
• 10-03-2002
Prelude
>And is still true for a number of processors which are either RISC architecture, or aimed at the embedded market.
Which is why I was careful with my wording. ;)
Quote:

So floating point math can sometimes be faster than integer math.
-Prelude
• 10-04-2002
kottapalli
try it
This type of programs, beter to use double and
in printf use like
printf(" %5.2lf");
• 10-04-2002
Prelude
>printf(" %5.2lf");
%lf is a double for scanf, with printf this flag means long double.

-Prelude
• 10-04-2002
Dave_Sinkula
Quote:

>printf(" %5.2lf");
%lf is a double for scanf, with printf this flag means long double.

-Prelude
No. With regard to printf, the l in the %lf is not necessary -- %f works for both a double and a float. For a long double, use %Lf.

[EDIT]
Dinkumware Print Conversion Specifiers
[/EDIT]
• 10-04-2002
Prelude
>No. With regard to printf, the l in the %lf is not necessary -- %f works for both a double and a float. For a long double, use %Lf.
That's what I said.

-Prelude