Floating points

Discussion in 'C++' started by kasiyil, Sep 1, 2006.

  1. kasiyil

    kasiyil Guest

    Hello,

    what is the disadvantage of using floating point numbers in boolean
    comparisons. For example,

    why using #define FALSE 0.0 will produce error instead of #define FALSE
    0?
     
    kasiyil, Sep 1, 2006
    #1
    1. Advertisements

  2. kasiyil

    Colander Guest

    kasiyil schreef:

    Hi There,

    The
    #define FALSE 0.0
    will not produce an error, it's legal c(++).
    It's better style to write:
    const double FALSE = 0.0;
    but they should work kinda alike.

    There are a lot of disadvantages, allmost to many to start, but the
    main one is that you are mixing types. Bools are used to express
    boolean values, doubles are used to express numbers. The world is far
    more simpler if everybody tries to do this!

    And did you know that c++ had an build-in constant called 'false'? I'd
    use that one if I was you.

    Good luck,
    colander
     
    Colander, Sep 1, 2006
    #2
    1. Advertisements

  3. kasiyil

    David Harmon Guest

    On 1 Sep 2006 05:28:22 -0700 in comp.lang.c++, "kasiyil"
    Floating point numbers are complicated things. Booleans are much
    simpler. It's better to keep things simple.

    Floating point is used where you can tolerate approximate results.
    Boolean true and false are exact. Never expect an exact result from
    floating point.
     
    David Harmon, Sep 1, 2006
    #3
  4. kasiyil

    Jim Langston Guest

    They don't produce errors afaik, although you may not always get what you
    expect. The main caveat is trying to compair a floating point number to an
    exact number.

    if ( MyFloat == 12.5 )
    //...

    MyFloat may appear to be 12.5, but may actually be something like
    12.499999999999999 or such.

    Other than that, I really don't userstand what you are asking.
     
    Jim Langston, Sep 1, 2006
    #4
  5. Curiously enough, you picked a rather bad example. 12.5 is most likely
    represented _precisely_. It's just 25/2. N/2^k (for N fitting in 50 bits
    or so and ||k|| below 1023) can be precisely represented. 1/3 cannot, for
    example.

    V
     
    Victor Bazarov, Sep 1, 2006
    #5
  6. kasiyil

    David Harmon Guest

    On Fri, 1 Sep 2006 16:08:44 -0400 in comp.lang.c++, "Victor Bazarov"
    1/3 is not so surprising to people who are used to decimal
    arithmetic. 1.10 us surprising.
     
    David Harmon, Sep 2, 2006
    #6
  7. 1/3 can be represented precisely in ternary, though... <g>

    V
     
    Victor Bazarov, Sep 2, 2006
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.