This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

c/7437: Identical unsigned int calculations differ by 1, depending on number of steps in calculation.


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

}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]