Floating point non-exactness

Andrew Koenig ark at alice.UUCP
Wed Aug 8 08:47:41 AEST 1990


In article <26691 at nigel.ee.udel.edu>, gdtltr at freezer.it.udel.edu (Gary Duzan) writes:

>    All practical applications aside, isn't it philosophically unnatural to
> apply an eqivalence comparison to two floating point (presumably real)
> numbers?

Not if you're doing appropriate operations on a machine with IEEE
floating point or some other good floating point system.

For example, IEEE guarantees that if I read the decimal representation
of a number that can be represented exactly in floating point,
that's what I'll get, period.  It also guarantees exact results
for primitive operations including addition, subtraction, multiplication,
division, and square root.

Suppose, then that I want to do calculations on integers that are
too big to fit in `int' values but small enough to be represented
exactly in floating point.  I see no reason not to do any comparison
on floating point numbers that I would do on integers.

As a simple example,

	double d;
	for (d = 0; d < 1e10; d++) {
		/* stuff */
	}

under IEEE floating point can be counted on to execute precisely 1e10
(ten billion) times.

Yes, such techniques are not portable to machines with lousy floating
point, but it might be that your program wouldn't run there anyway
for some reason.  As good floating point systems become more widespread,
this becomes less of an issue.
-- 
				--Andrew Koenig
				  ark at europa.att.com



More information about the Comp.lang.c mailing list