public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "exponent-bias at yandex dot ru" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/51441] New: Incorrect FP rounding on addition of doubles
Date: Tue, 06 Dec 2011 20:03:00 -0000	[thread overview]
Message-ID: <bug-51441-4@http.gcc.gnu.org/bugzilla/> (raw)

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51441

             Bug #: 51441
           Summary: Incorrect FP rounding on addition of doubles
    Classification: Unclassified
           Product: gcc
           Version: 4.4.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: exponent-bias@yandex.ru


A well-known sentence of IEEE 754 reads: "Each of the computational operations
that return a numeric result specified by this standard shall be performed as
if it first produced an intermediate result correct to infinite precision and
with unbounded range, and then rounded that intermediate result, if necessary,
to fit in the destination's format..."

Given two operands in IEEE's binary32 format:
    0x1.002p0  (C99 hexadecimal notation)
and
    1.6331239353195372e+16
I believe the result of addition of the operands under the to-nearest rounding
mode shall be:
    1.6331239353195374e+16
whereas GCC yields:
    1.6331239353195372e+16
(note the difference in the last sign of the mantissa).

The analysis is this: the difference of exponents of the operands is 53 bits
which is also the width of mantissa in the binary32 format, including the
implied most significant raised bit. Thus, mantissa of the intermediate result
of the addition should be result of concatenation of mantissas of the operands:
mantissa of 1.6331239353195372e+16 followed by mantissa of 0x1.002p0. That is,
the most significant mantissa's bit of 0x1.002p0 is the first bit that shall be
rounded away to fit the destination's format. We know that first bit is raised
and we know there is one more bit raised in the mantissa, so under the
to-nearest rounding mode the intermediate result shall be rounded to the next
representable number that is not less than the intermediate result, which is
1.6331239353195374e+16.

The test code:
----
#include <stdlib.h>
#include <stdio.h>

double a, b, r;

int main(void)
{
    a = 0x1.002p0;
    b = 1.6331239353195372e+16;

    r = a + b;

    printf("%+.16e + %+.16e = %+.16e\n", a, b, r);

    return EXIT_SUCCESS;
}
----

Compilation flags:
gcc --float-store -g -O0 -o test test.c

Actual output:
+1.0004882812500000e+00 + +1.6331239353195372e+16 = +1.6331239353195372e+16

Expected output:
+1.0004882812500000e+00 + +1.6331239353195372e+16 = +1.6331239353195374e+16

Compiler version:
gcc-4.4.real (Ubuntu 4.4.3-4ubuntu5) 4.4.3


             reply	other threads:[~2011-12-06 20:03 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-06 20:03 exponent-bias at yandex dot ru [this message]
2011-12-06 20:05 ` [Bug c/51441] " exponent-bias at yandex dot ru
2011-12-06 20:16 ` [Bug target/51441] " pinskia at gcc dot gnu.org
2011-12-06 20:18 ` joseph at codesourcery dot com
2011-12-07  7:20 ` [Bug c/51441] " exponent-bias at yandex dot ru
2011-12-07  9:35 ` jakub at gcc dot gnu.org

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=bug-51441-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).