public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Torquil Macdonald Sørensen" <torquil@gmail.com>
To: gcc-help@gcc.gnu.org
Subject: Re: Optimization and double comparison
Date: Tue, 05 Aug 2008 08:19:00 -0000	[thread overview]
Message-ID: <200808051019.13789.torquil@gmail.com> (raw)
In-Reply-To: <489780AB.8852489@dessent.net>

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

  reply	other threads:[~2008-08-05  8:19 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-04 21:41 Torquil Macdonald Sørensen
2008-08-04 22:21 ` Brian Dessent
2008-08-05  8:19   ` Torquil Macdonald Sørensen [this message]
2008-08-05 15:22     ` Bob Plantz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200808051019.13789.torquil@gmail.com \
    --to=torquil@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).