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. Advertising

  2. kasiyil

    Colander Guest

    kasiyil schreef:

    Hi There,

    > 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?



    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. Advertising

  3. kasiyil

    David Harmon Guest

    On 1 Sep 2006 05:28:22 -0700 in comp.lang.c++, "kasiyil"
    <> wrote,
    >what is the disadvantage of using floating point numbers in boolean
    >comparisons.


    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

    "kasiyil" <> wrote in message
    news:...
    > 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?


    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. Jim Langston wrote:
    > "kasiyil" <> wrote in message
    > news:...
    >> 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?

    >
    > 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.


    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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    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"
    <> wrote,
    >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.


    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. David Harmon wrote:
    > On Fri, 1 Sep 2006 16:08:44 -0400 in comp.lang.c++, "Victor Bazarov"
    > <> wrote,
    >> 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.

    >
    > 1/3 is not so surprising to people who are used to decimal
    > arithmetic. 1.10 us surprising.


    1/3 can be represented precisely in ternary, though... <g>

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 2, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anders K. Jacobsen [DK]

    Problem with floating points in datagrid

    Anders K. Jacobsen [DK], Apr 7, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    316
    Karl Seguin
    Apr 7, 2005
  2. gilgantic
    Replies:
    4
    Views:
    1,001
    Anton Spaans
    Apr 26, 2004
  3. Noam Raphael

    repr of floating points

    Noam Raphael, Jun 6, 2004, in forum: Python
    Replies:
    1
    Views:
    310
    Josiah Carlson
    Jun 6, 2004
  4. pereges
    Replies:
    8
    Views:
    324
    Flash Gordon
    Jul 22, 2008
  5. pereges
    Replies:
    3
    Views:
    343
    ~Glynne
    Jul 23, 2008
Loading...

Share This Page