This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Optimization and double comparison


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]