Inherent imprecision of floating point variables

Karl Heuer karl at haddock.ima.isc.com
Fri Jun 29 08:27:51 AEST 1990


In article <4169 at jato.Jpl.Nasa.Gov> kaleb at mars.UUCP (Kaleb Keithley) writes:
>I once wrote a rational number package...  All numbers are held in
>numerator/denominator form in a struct.

Certain operations, addition in particular, will quickly cause the denominator
to exceed the range of an integral type.  To avoid this, you have to combine
this with an expandable-int package.  And you still have a problem if you
apply a math function: sqrt(2) can only be approximated, not represented
exactly, by this mechanism.

Several people have posted to say that the exact-representation problem is
"unsolvable" because of this.  This is not necessarily true.  You could use
continued fraction representation, for instance.  sqrt(2) still has no finite
representation, but by using lazy evaluation you don't *need* to have the
whole thing in hand at once: it suffices to have a generator object that will
emit the next term on request.

There's a good discussion of this in HAKMEM, item #101.

Karl W. Z. Heuer (karl at kelp.ima.isc.com or ima!kelp!karl), The Walking Lint



More information about the Comp.lang.c mailing list