445,778 Members | 1,947 Online
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

# operator== (float, float)

 P: n/a 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:Ju************@abo.fi Jul 22 '05 #1
5 Replies

 P: n/a "Jukka Lehtonen" wrote in message news:t0*************@olvi.abo.fi... 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 Jul 22 '05 #2

 P: n/a Jukka Lehtonen wrote in news:t0*************@olvi.abo.fi 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/ Jul 22 '05 #3

 P: n/a 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 kb******@gascad.at Jul 22 '05 #4

 P: n/a 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::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. Jul 22 '05 #5

 P: n/a 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:Ju************@abo.fi Jul 22 '05 #6

### This discussion thread is closed

Replies have been disabled for this discussion.