public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Wragg <dpw@doc.ic.ac.uk>
To: help-gcc@gnu.org
Subject: Re: What the hell is going on? Just a simple division...
Date: Fri, 31 Dec 1999 22:24:00 -0000	[thread overview]
Message-ID: <jaaen3hjhf.fsf@gatsby.u-net.com> (raw)
Message-ID: <19991231222400.nwDFYmlBCTWmpUENLVNnD0HvfqOinN2_j-aXlxwiAT0@z> (raw)
In-Reply-To: <83oqfc$2q4$1@dinkel.civ.utwente.nl>

"Paul E.C. Melis" <melis@cs.utwente.nl> writes:
> Using gcc-2.95.2 and linux 2.0.35 (libc5) the
> following program gives as output
>     0
>     1
> instead of the obviously correct output
>     1
>     1
> Is this a compiler bug?

No. Just a pitfall of floating point on x86.

>     printf ("%d\n", (int) ( log(2.0) / log(2.0) ));

Look at the generated assembly. One "log(2.0)" gets evaluated to a
double held in an 80-bit FP register. This is then transfered to a
64-bit double in memory, causing it to be rounded. The other
"log(2.0)" is then evaluated to a double held in an 80-bit bit FP
register. The division is then performed. Since only one value was
rounded, the result will not be 1.0, and so on conversion to int may
be truncated to 0.


David Wragg

  reply	other threads:[~1999-12-31 22:24 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-12-21 13:14 Paul E.C. Melis
1999-12-24 11:00 ` David Wragg [this message]
1999-12-31 22:24   ` David Wragg
1999-12-31 22:24 ` Paul E.C. Melis

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=jaaen3hjhf.fsf@gatsby.u-net.com \
    --to=dpw@doc.ic.ac.uk \
    --cc=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).