Floating Point comparison problem

P

Pete Becker


No. It's from the superstitious school of floating-point math.

Its first sentence is "Floating point math is not exact." It presents
as evidence the example of converting the text "0.2" to floating-point,
which has nothing to do with floating-point math. And, of course, on a
system that uses decimal floating-point, this conversion is exact. And,
in contrast, converting "0.2" to an int is never exact. But nobody
advises using "approximately equal" for int values.

There are three kinds of programmers when it comes to floating-point math.

1. Those who don't know what its limitations are. They are surprised
when they don't get the results they expect, and they respond by making
up techniques (such as approximate comparisons) to fudge the results.

2. Those who know its limitations but aren't experts (I put myself in
this category). They can often get good results, and they know when
they need to call in someone with more expertise.

3. Those who know how floating-point math works. They tend to have PhDs. <g>

Java went the route of requiring exactness in floating-point math, and
it was a disaster. The language imposed IEEE 64-bit floating-point
math, with no extra bits allowed, for doubles. On PCs, this meant
setting the Intel math processor to 64-bit mode instead of its native
80-bit mode, and that makes it much slower. That speed loss was not
acceptable to people who do serious number crunching, so now Java has
two floating-point systems, one that's "strict" and one that's useful.
 
M

Martin

No.

It's from the superstitious school of floating-point math.

Its first sentence is "Floating point math is not exact."

Perhaps badly worded, I read this as "Floating point math on everyday
PC's is not exactly the same as it is on paper". :)

It presents
as evidence the example of converting the text "0.2" to floating-point,
which has nothing to do with floating-point math.

Er, no it doesn't say that... He's saying that a declaration such as,

float f = 0.2f;

will give an underlying value, which is very close to but not
precisely 0.2.

e.g.

#include <iomanip>
#include <iostream>

int main()
{
float f = 0.2f;

std::cout << std::setprecision(9) << f << std::endl;
}

gives a result of:

0.200000003

or at least is does on my Wintel PC

And, of course, on a
system that uses decimal floating-point, this conversion is exact.

Yes, but I think decimal floating-point numbers are a bit off-topic
for the OP.

And,
in contrast, converting "0.2" to an int is never exact. But nobody
advises using "approximately equal" for int values.

But the techniques in the paper are not about converting a float to an
int but rather using the underlying representation of the (IEEE)
float.


[snip - Java related stuff]

The OP is developing something that will "heavily depend on Numerical-
Computations" - he may well need the exactness...
 
P

Pete Becker

Perhaps badly worded, I read this as "Floating point math on everyday
PC's is not exactly the same as it is on paper". :)

That's a generous reading.
Er, no it doesn't say that... He's saying that a declaration such as,

float f = 0.2f;

will give an underlying value, which is very close to but not
precisely 0.2.

Yes, that's a conversion from text (the 0.2f in the soruce code) to
floating-point. It has nothing to do with floating-point math, which
operates on on the result of that conversion.
The OP is developing something that will "heavily depend on Numerical-
Computations" - he may well need the exactness...

If so, then this entire thread, about approximate equality, is misguided.
 
M

Martin

That's a generous reading.

Maybe but it isn't written as a peer reviewable paper...

Yes, that's a conversion from text (the 0.2f in the soruce code) to
floating-point. It has nothing to do with floating-point math, which
operates on on the result of that conversion.

Ah, I thought you were talking about converting "string" values, e.g.
from a file.

<tongueFirmlyInCheck>

Again, I may be being 'generous' but I don't find the example of 0.2
as an introduction, to be /that/ bad. If you want to get some
surprising math then insert a simple addition, e.g.

f = f + 0.0;

If so, then this entire thread, about approximate equality, is misguided.

Yes, that may be true but only OP knows...
 
P

Pete Becker

[snip]

<tongueFirmlyInCheck>

Again, I may be being 'generous' but I don't find the example of 0.2
as an introduction, to be /that/ bad. If you want to get some
surprising math then insert a simple addition, e.g.

f = f + 0.0;

</tongueFirmlyInCheck>

The point is that nobody claims that integer math is "not exact"
despite the fact that 0.2 cannot be represented exactly as in int
value. The difference is that programmers understand (mostly) how
integer math works on computers, and they don't understand how
floating-point math works. The problem is not floating-point math, but
ignorance. The solution is not hacked approximations, but understanding.
 
R

Richard Herring

In message
<tongueFirmlyInCheck>

Again, I may be being 'generous' but I don't find the example of 0.2
as an introduction, to be /that/ bad. If you want to get some
surprising math then insert a simple addition, e.g.

f = f + 0.0;

Go on, surprise me.
 

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
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top