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

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

  reply	other threads:[~2008-08-04 22:21 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 [this message]
2008-08-05  8:19   ` Torquil Macdonald Sørensen
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=489780AB.8852489@dessent.net \
    --to=brian@dessent.net \
    --cc=gcc-help@gcc.gnu.org \
    --cc=torquil@gmail.com \
    /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).