operator== (float, float)

Discussion in 'C++' started by Jukka Lehtonen, Aug 4, 2004.

  1. Hi all,

    I was watching g++ to spurt out warnings during compilation,
    and if you enable most warning flags as I have occasionally
    done that means a lot of text, most of it from library code.
    Nothing special, but then I started thinking about the
    occasional 'comparing floats for equality' lines.

    I admit that those warnings are an indicator of my own
    lazyness. The technical reasons for a call

    float A = ...; float B = ...; if ( A == B ) ...

    to be suspicious are a FAQ, as is the solution

    if ( fabs( A - B ) < epsilon ) ...

    The question that formed in my mind was:

    Why do I have to do that?

    What is the purpose of operator== (float, float)
    within the standard if it does not perform the action
    that my intuition expects? Granted, the epsilon would have
    to be specified somewhere.

    JVL
    --
    Be reading you.

    mailto:
    Jukka Lehtonen, Aug 4, 2004
    #1
    1. Advertising

  2. "Jukka Lehtonen" <> wrote in message
    news:...
    > Hi all,
    >
    > I was watching g++ to spurt out warnings during compilation,
    > and if you enable most warning flags as I have occasionally
    > done that means a lot of text, most of it from library code.
    > Nothing special, but then I started thinking about the
    > occasional 'comparing floats for equality' lines.
    >
    > I admit that those warnings are an indicator of my own
    > lazyness. The technical reasons for a call
    >
    > float A = ...; float B = ...; if ( A == B ) ...
    >
    > to be suspicious are a FAQ, as is the solution
    >
    > if ( fabs( A - B ) < epsilon ) ...
    >
    > The question that formed in my mind was:
    >
    > Why do I have to do that?
    >
    > What is the purpose of operator== (float, float)
    > within the standard if it does not perform the action
    > that my intuition expects? Granted, the epsilon would have
    > to be specified somewhere.
    >


    Well I'm not sure what you expect but operator== (float, float) does exactly
    what I would expect, it compares two floats for equality. The problem is
    that floating point arithmetic is often inexact, not that equality between
    floating point numbers is ill defined.

    There are occasions when exact floating point equality is useful. For
    instance the following tests if a double has an integral value

    double x = func();
    if (x == floor(x))
    cout << "is integral\n";
    else
    cout << "is not integral\n";

    If operator== was redefined in the way you suggest code like that would no
    longer work.

    john
    John Harrison, Aug 4, 2004
    #2
    1. Advertising

  3. Jukka Lehtonen wrote in news: in comp.lang.c++:

    > Hi all,
    >
    > I was watching g++ to spurt out warnings during compilation,
    > and if you enable most warning flags as I have occasionally
    > done that means a lot of text, most of it from library code.
    > Nothing special, but then I started thinking about the
    > occasional 'comparing floats for equality' lines.
    >
    > I admit that those warnings are an indicator of my own
    > lazyness. The technical reasons for a call
    >
    > float A = ...; float B = ...; if ( A == B ) ...
    >
    > to be suspicious are a FAQ, as is the solution
    >
    > if ( fabs( A - B ) < epsilon ) ...
    >
    > The question that formed in my mind was:
    >
    > Why do I have to do that?
    >
    > What is the purpose of operator== (float, float)
    > within the standard if it does not perform the action
    > that my intuition expects? Granted, the epsilon would have
    > to be specified somewhere.


    Alas there is no "One True Way" when it comes to doing any kind
    of floating-point.

    if ( fabs( A - B ) < epsilon ) ...

    Also has a problem, say epsilon is 1.0, but A and B > 1E20 (*). The
    only way the epsilon-equality form can be true is if the exact-equality
    form is true.

    So your left with exact equality being the only meaningfull, consistant
    and general purpose meaning you can give to operator ==.

    *) 1e20, I'm assuming float has a max precision of 19 decimals here.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Aug 4, 2004
    #3
  4. Jukka Lehtonen wrote:
    >
    > Hi all,
    >
    > I was watching g++ to spurt out warnings during compilation,
    > and if you enable most warning flags as I have occasionally
    > done that means a lot of text, most of it from library code.
    > Nothing special, but then I started thinking about the
    > occasional 'comparing floats for equality' lines.
    >
    > I admit that those warnings are an indicator of my own
    > lazyness. The technical reasons for a call
    >
    > float A = ...; float B = ...; if ( A == B ) ...
    >
    > to be suspicious are a FAQ, as is the solution
    >
    > if ( fabs( A - B ) < epsilon ) ...
    >
    > The question that formed in my mind was:
    >
    > Why do I have to do that?


    You might want to read

    http://www.petebecker.com/js200006.html
    http://docs.sun.com/source/806-3568/ncg_goldberg.html

    Oh, and by the way:
    Don't use float until you know exactly what you do and have a very good
    reason to do it and know how to fight that beast.
    Use double instead. The problems are not gone away but are a little
    bit smaller. Often small enough that for practical purposes it is
    the difference between a tough fight and a smooth walk.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Aug 4, 2004
    #4
  5. Jukka Lehtonen wrote:

    >
    > if ( fabs( A - B ) < epsilon ) ...
    >
    > The question that formed in my mind was:
    >
    > Why do I have to do that?
    >
    > What is the purpose of operator== (float, float)
    > within the standard if it does not perform the action
    > that my intuition expects? Granted, the epsilon would have
    > to be specified somewhere.


    The problem is, that epsilon varies..It depends on A and B *and* the
    rounding error you are ready to expect at a certain state of
    computation. Refer to numeric_limits<T>::epsilon(). This returns the
    smallest number, such that 1.0 + epsilon == 1.0. But this also means
    (usually...), that for any other number r, epsilon is r*epsilon!

    Now consider something like x*x. if x has an epsilon eps(x), then x*x
    has a maximal error of something like[1]:

    error(x*x)=|(x+eps(x))^2-x^2|=|2*x*eps(x)+eps(x)^2|

    and for (x*x)/x it's |error(x^2)/(x+eps)-error(x^2)/x|

    So therefore you need an idea for you epsilon or make a good guess.

    Marco

    [1] Disclaimer: These formulas a incorrect and irrelevant and are meant
    to give nothing than a rough idea of the magnitude of expectable
    trouble.
    Marco Manfredini, Aug 4, 2004
    #5
  6. Thank you for the answers. I was aware of the rounding
    as binary values can not represent all decimal values
    exactly, but the relation of magnitude of values to the
    errors induced by operations was new.

    Back to the drawing board...

    JVL
    --
    Be reading you.

    mailto:
    Jukka Lehtonen, Aug 5, 2004
    #6
    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. Andy
    Replies:
    7
    Views:
    6,239
    Roedy Green
    May 10, 2004
  2. Jim West
    Replies:
    4
    Views:
    1,402
    Jeff Schwab
    Jan 16, 2004
  3. daniel
    Replies:
    1
    Views:
    2,087
    Tom St Denis
    Jun 24, 2003
  4. bd
    Replies:
    0
    Views:
    605
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,505
    James Kanze
    Oct 8, 2009
Loading...

Share This Page