Re: Problem in Float Arithmetic
On Thu, 3 Jul 2008 18:31:42 +0100, "David Webber"
<dave@musical-dot-demon-dot-co.uk> wrote:
Floating point arithmetic on integer values that produces integer values
is
exact over a wide range, something like +/- 2^53 for double.
That is a very specific (though imprecise) definition of "exact", which can
of course be useful in very specific circumstances. But it is highly
dangerous.
<snip>
You really like to make this complicated, don't you? :) It is guaranteed
that the following does not assert:
double x = 1;
double y = 1;
double z = x+y;
assert(z == 2);
No "danger" there, nor is there any danger for millions and millions of
other values. Of course, if you compute x by accumulating .1, it won't
hold, but then that's not what I was talking about, is it?
But numerical maths teaches one NEVER to use == on floating
point values. It can work sometimes if you are testing equality with 0.0,
but that is the thin end of a very nasty wedge!
If you include a tolerance in the above assert, which compares to 2, I
guess you must also feel obligated to do a rain dance during a drought.
Thinking of floating point
numbers as "exact" is going to land you in trouble at some point - usually
sooner rather than later.
You really are determined to make that point, even if you have to go to
extremes to misrepresent what I said. I simply corrected a false
categorical statement that you made, like I did in this thread over a year
ago:
http://groups.google.com/group/microsoft.public.vc.language/browse_frm/thread/68eefdc4b9e453f4/8667ab19207534d3?#8667ab19207534d3
If you stop making false categorical statements, I'll stop correcting them,
and then it should follow that you'll stop trying to edumacate me about
things I just might already know that are irrelevant to what I said. Deal?
<g>
P.S. Can you name an example or two in which storing integers in doubles is
an important application, such that it might be worthwhile to understand
how they behave?
--
Doug Harrison
Visual C++ MVP