problem with float

This is a discussion on problem with float within the C Programming forums, part of the General Programming Boards category; 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"); ...

  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
    32,852
    > 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,796
    >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,006
    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
    32,852
    > 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,796
    >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,796
    >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,006
    >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 01:33 PM.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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, 03:51 PM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-24-2007, 12:05 AM
  3. Class won't call
    By Aalmaron in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2006, 05: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, 10:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21