Serious bug
Michael Poole
poole@graviton.res.cmu.edu
Wed Sep 30 12:52:00 GMT 1998
Richard Hadsell <hadsell@blueskystudios.com> writes:
> Jeffrey A Law wrote:
> >
> > In message < 360F905D.4B1@blueskystudios.com >you write:
> > > (1) If you subtract a number from itself, you get 0.
> > Not for ieee754 math.
> >
> > > (2) If you compare a number with itself, you get equality.
> > Not for ieee754 math.
> >
> > > (3) If you assign a number to another of the same type, you get the same
> > > number.
> > I don't know about this one.
>
> This is not a matter of standards. It's math. Most of us have been
> dealing with both integer and floating-point math in various forms long
> enough to realize that it's possible to get these basics right. I
> wouldn't fall back on any standard to excuse the compiler. I would,
> however, blame the processor architecture or implementation, if it turns
> out to be costly to implement.
Those three points are "correct", but you can try to arrive at a number
in different ways and get different answers. To illustrate, consider this
short program:
#include <stdio.h>
int main(int argc, char *argv) {
float a, b, c, d;
a = 0.1;
b = 3.0;
c = 0.31;
d = 3.1;
printf("%.10f ?= %.10f ?= %.10f\n", a+b, c/a, d);
}
and its output when compiled as 'gcc test.c' (with or without -lieee)
on my i686-pc-linux-gnu box:
3.1000000015 ?= 3.0999999776 ?= 3.0999999046
Now, to the casual observer, it would seem pretty odd that it doesn't say
"3.1000000000 ?= 3.1000000000 ?= 3.1000000000". However, the closest
FP representation to each of those values (0.1, 3.0, 0.31, 3.1) is not
exactly the number in the source code. When the math is done, these
differences in representation (as well as losing some precision in the
LSBs of the sum or quotient) cause the three values of "3.1" to be
different. If you compared any two of those, what determines whether or
not they are the same? For some applications, the difference between
them may be significant; for others, you may want to test them as equal.
The compiler cannot impose arbitrary judgments on which are equal, or
it would break the latter application.
-- Michael
More information about the Gcc
mailing list