Thread: problem with float

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    14

    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");

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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
    My best code is written with the delete key.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.
    So floating point math can sometimes be faster than integer math.
    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    2

    try it

    This type of programs, beter to use double and
    in printf use like
    printf(" %5.2lf");

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >printf(" %5.2lf");
    %lf is a double for scanf, with printf this flag means long double.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >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]
    Last edited by Dave_Sinkula; 10-04-2002 at 12:33 PM.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  3. Class won't call
    By Aalmaron in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2006, 04:57 PM
  4. Pointer
    By silicon in forum C Programming
    Replies: 2
    Last Post: 03-25-2004, 02:34 PM
  5. Why does it work in Visual Studio and Not Borland
    By MonteMan in forum C++ Programming
    Replies: 14
    Last Post: 10-20-2002, 09:36 PM