Optimization and double comparison
Torquil Macdonald Sørensen
torquil@gmail.com
Tue Aug 5 08:19:00 GMT 2008
On Tuesday 5. August 2008 00:20, Brian Dessent wrote:
> 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
Hi Brian, thank you very much. Yes the target is x86. Your answer was very
interesting. The comparison was just to make sure that a certain quantity
still had the value that it is supposed to have, so it was only for debugging
my algorithm. It should be allright if I instead allow a small error in the
comparison, so I think that I don't have to worry about this problem then.
Thanks again,
Torquil Sørensen
More information about the Gcc-help
mailing list