public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* floating point bug -- or misunderstanding?
@ 1998-08-14  9:07 Derrick Bass
  1998-08-14 11:30 ` Alexandre Oliva
  0 siblings, 1 reply; 2+ messages in thread
From: Derrick Bass @ 1998-08-14  9:07 UTC (permalink / raw)
  To: egcs-bugs

I may have found a bug in egcs, but more likely I just don't
understand floating point math.

Can someone tell me enough about floating point (or point me to an
explanation) to explain the output of the following program?

#include <stdio.h>
main() {
  float a,b,c,d;
  
  a = 1;
  b = 3;
  c = a/b;
  d = c * b;

  printf("%g\n", a - c * b);	
  printf("%g\n", a - d);
}

I compiled the program with egcs-19980803 on a pentium pro linux
machine with no optimization and the output was
-2.98023e-08
0

Shouldn't these numbers be equal?

Derrick Bass


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: floating point bug -- or misunderstanding?
  1998-08-14  9:07 floating point bug -- or misunderstanding? Derrick Bass
@ 1998-08-14 11:30 ` Alexandre Oliva
  0 siblings, 0 replies; 2+ messages in thread
From: Alexandre Oliva @ 1998-08-14 11:30 UTC (permalink / raw)
  To: Derrick Bass; +Cc: egcs-bugs

Derrick Bass <derrick@tapir.Caltech.EDU> writes:

> I may have found a bug in egcs, but more likely I just don't
> understand floating point math.

The x86 FPU works with an internal precision higher than the one
offered by type double.  Thus, when it performs calculations
internally, without storing temporary values in memory, it usually
gets more accurate results than if it stores and loads values in
memory.  Your program is just getting rounding errors.

-- 
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1998-08-14 11:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-08-14  9:07 floating point bug -- or misunderstanding? Derrick Bass
1998-08-14 11:30 ` Alexandre Oliva

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).