public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* Re: c/7437: Identical unsigned int calculations differ by 1, depending on number of steps in calculation.
@ 2002-07-29 15:36 Geoff Keating
  0 siblings, 0 replies; 2+ messages in thread
From: Geoff Keating @ 2002-07-29 15:36 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c/7437; it has been noted by GNATS.

From: Geoff Keating <geoffk@geoffk.org>
To: sewell@dramail.com
Cc: gcc-gnats@gcc.gnu.org
Subject: Re: c/7437: Identical unsigned int calculations differ by 1, depending on number of steps in calculation.
Date: 29 Jul 2002 15:33:55 -0700

 This is covered in the manual:
 
    * On 68000 and x86 systems, for instance, you can get paradoxical
      results if you test the precise values of floating point numbers.
      For example, you can find that a floating point value which is not
      a NaN is not equal to itself.  This results from the fact that the
      floating point registers hold a few more bits of precision than
      fit in a `double' in memory.  Compiled code moves values between
      memory and floating point registers at its convenience, and moving
      them into memory truncates them.
 
      You can partially avoid this problem by using the `-ffloat-store'
      option (*note Optimize Options::).
 
 -- 
 - Geoffrey Keating <geoffk@geoffk.org> <geoffk@redhat.com>


^ permalink raw reply	[flat|nested] 2+ messages in thread

* c/7437: Identical unsigned int calculations differ by 1, depending on number of steps in calculation.
@ 2002-07-29 12:46 sewell
  0 siblings, 0 replies; 2+ messages in thread
From: sewell @ 2002-07-29 12:46 UTC (permalink / raw)
  To: gcc-gnats


>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);

}


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-07-29 22:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-29 15:36 c/7437: Identical unsigned int calculations differ by 1, depending on number of steps in calculation Geoff Keating
  -- strict thread matches above, loose matches on Subject: below --
2002-07-29 12:46 sewell

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).