This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Optimization and double comparison
- From: Brian Dessent <brian at dessent dot net>
- To: Torquil Macdonald Sørensen <torquil at gmail dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Mon, 04 Aug 2008 15:20:27 -0700
- Subject: Re: Optimization and double comparison
- References: <200808042334.05408.torquil@gmail.com>
- Reply-to: gcc-help at gcc dot gnu dot org
Torquil Macdonald Sørensen wrote:
> In short, how can it be that an if-test of the following form can print two
> exactly equal numbers?:
>
> if( a != b) {
> cout << setprecision(70);
> cout << a << " " << b << endl;
> }
You didn't mention what target this is. If it's x86 then this is the
classic issue of excess precision, wherein operations within the 387
unit occur in extended (80 bit) precision but when transferred out of
the FP unit and stored to memory are truncated to double precision (64
bit.) If you compare a value that has just been computed with one that
has been previously computed and then stored to memory, they won't
necessarily be equal since one has been truncated while the other still
has its excess precision. But by passing it on the stack to cout you
essentially force a memory store which discards the excess precision so
they both appear the same.
Much has already been written on this topic, so I suggest just reading
PR323 or googling. There are numerous workarounds: use sse2 instead of
387, set the 387 to double precision (e.g. -mpc64 or _FP_{GET,SET}CW),
use -ffloat-store, don't compare for absolute equality but against some
small delta, etc.
Brian