public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
From: sewell@dramail.com
To: gcc-gnats@gcc.gnu.org
Subject: c/7437: Identical unsigned int calculations differ by 1, depending on number of steps in calculation.
Date: Mon, 29 Jul 2002 12:46:00 -0000	[thread overview]
Message-ID: <20020729193642.14659.qmail@sources.redhat.com> (raw)


>Number:         7437
>Category:       c
>Synopsis:       Identical unsigned int calculations differ by 1, depending on number of steps in calculation.
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Jul 29 12:46:01 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     Ken Sewell
>Release:        gcc version 2.95.3 20010315 (SuSE)
>Organization:
>Environment:
AMD Athlon(tm) Processor
SuSE linux 8.0
>Description:
a simple calculation of the format

a = b / (c / d);
e = (unsigned int)a;

(where a-d are double and e is an unsigned int) is correct.

the calculation

e = (unsigned int)(b / (c/d));

is incorrect.

if the -O or -O2 flag is given at compile time, the code executes properly.

I have also tried this on a Pentium II and with gcc3.0.  It does not happen my Sun or SGI systems.
>How-To-Repeat:
gcc test.c
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: text/plain; name="test.c"
Content-Disposition: inline; filename="test.c"

#include <stdio.h>

int main ()
{
    double x;
    double dsecs = 3600.0;
    double step1;
    unsigned int step2, step1n2;

    x = 2147483647.0;

    printf("    x: %f %X\n", x, x);

    // Do calculations in 2 steps...
    step1 = dsecs / (3600.0 / x);
    printf("  step1 = %f / (3600.0 / %f) =  %f = %X\n", 
	   dsecs, x, step1, step1);

    step2 = (unsigned int)step1;
    printf("  step2 = (unsigned int)%f = %u = %X\n\n", 
	   step1, step2, step2);

    // Do calculations in 1 step...
    step1n2 = (unsigned int)(dsecs / (3600.0 / x));
    printf("step1n2 = (unsigned int)(%f / (3600.0 / %f)) = %u = %X\n", 
	   dsecs, x, step1n2, step1n2);

}


             reply	other threads:[~2002-07-29 19:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-29 12:46 sewell [this message]
2002-07-29 15:36 Geoff Keating

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=20020729193642.14659.qmail@sources.redhat.com \
    --to=sewell@dramail.com \
    --cc=gcc-gnats@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).