operator== (float, float)

J

Jukka Lehtonen

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
 
J

John Harrison

Jukka Lehtonen said:
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
 
R

Rob Williscroft

Jukka Lehtonen wrote in 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.
 
K

Karl Heinz Buchegger

Jukka said:
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.
 
M

Marco Manfredini

Jukka said:
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.
 
J

Jukka Lehtonen

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top