This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
c/7857: Different behaviour if compiled with optimization!
- From: jcas at netcabo dot pt
- To: gcc-gnats at gcc dot gnu dot org
- Date: 8 Sep 2002 14:36:46 -0000
- Subject: c/7857: Different behaviour if compiled with optimization!
- Reply-to: jcas at netcabo dot pt
>Number: 7857
>Category: c
>Synopsis: Different behaviour if compiled with optimization!
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Sun Sep 08 07:46:00 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator: Jose Santos
>Release: 3.10
>Organization:
>Environment:
Windows XP
>Description:
If code is compiled with any optimization (-O1 to -O3) it has a different behaviour than with compiled with no optimizations.
The code is related with precision errors.With optimizations there are no precision errors, without optimization there's a precision error!
>How-To-Repeat:
Just run the file with -O3 and without -O3 and see that in first case it runs fine(requires a couple of seconds, eg: 15s), in second case it enters an infinite loop(and just ends after looping all integers back to 0)
>Fix:
Don't known...perhaps don't use the sqrt function ?
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: text/plain; name="bug.c"
Content-Disposition: inline; filename="bug.c"
#include <stdio.h>
#include <math.h>
/*
Compile with -O3 and runs fine
without there's a precision error.
SQ's value has the correct value with -O3 (+1 than without -O3 for n==46611179)
E-mail me at jcas@netcabo.pt
*/
#define NUMPAIRS 10
int main(){
long long n=2, count = 0;
for(n=2; count<NUMPAIRS; n++){
long long n2 =n*n;
//double sq = sqrt(1+8.0*n2);
long long sq = sqrt(1+8*n2);
long long k = -(1 - sq)/2;
long long k2 = k*k;
if(n==46611179)
printf("%lld, %lld\n", sq, k);
if(2*n2==k2+k){
printf("%10lld %10lld\n", n, k);
count++;
}
}
return 0;
}