From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18234 invoked by alias); 5 Dec 2002 18:36:03 -0000 Mailing-List: contact gcc-prs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-prs-owner@gcc.gnu.org Received: (qmail 18206 invoked by uid 71); 5 Dec 2002 18:36:02 -0000 Resent-Date: 5 Dec 2002 18:36:02 -0000 Resent-Message-ID: <20021205183602.18205.qmail@sources.redhat.com> Resent-From: gcc-gnats@gcc.gnu.org (GNATS Filer) Resent-Cc: gcc-prs@gcc.gnu.org, gcc-bugs@gcc.gnu.org Resent-Reply-To: gcc-gnats@gcc.gnu.org, joeytsai@yahoo.com Received: (qmail 18137 invoked by uid 61); 5 Dec 2002 18:35:37 -0000 Message-Id: <20021205183537.18136.qmail@sources.redhat.com> Date: Thu, 05 Dec 2002 10:36:00 -0000 From: joeytsai@yahoo.com Reply-To: joeytsai@yahoo.com To: gcc-gnats@gcc.gnu.org X-Send-Pr-Version: gnatsweb-2.9.3 (1.1.1.1.2.31) Subject: c/8825: optimization causes incorrect math X-SW-Source: 2002-12/txt/msg00286.txt.bz2 List-Id: >Number: 8825 >Category: c >Synopsis: optimization causes incorrect math >Confidential: no >Severity: serious >Priority: medium >Responsible: unassigned >State: open >Class: sw-bug >Submitter-Id: net >Arrival-Date: Thu Dec 05 10:36:02 PST 2002 >Closed-Date: >Last-Modified: >Originator: Joey Tsai >Release: gcc version 2.95.4 20011002 (Debian prerelease) >Organization: >Environment: Debian Unstable >Description: /* It appears that using optimization causes some weird math to happen. In this example below, there is a point which is being updated by another structure which has more precision. But when using optimization, an unexpected result occurs, specifically 112+0=111. */ #include #include struct _move { double x, y; double dx, dy; } move; struct _point { int x, y; } point; void setup_move( struct _move * m, int angle, int x, int y ) { double d; d = (double) angle * (double) M_PI / (double) 180.0; m->dx = cos(d) * (double) -10.0; m->dy = sin(d) * (double) -10.0; m->x = (double) x; m->y = (double) y; } int main( int argc, char * argv[] ) { point.x = 112; point.y = 400; setup_move( &move, 90, point.x, point.y ); move.x += move.dx; move.y += move.dy; point.x = (int) move.x; point.y = (int) move.y; printf( "point x=%d, y=%d\n", point.x, point.y ); return 0; } >How-To-Repeat: This is incorrect: $ gcc -O1 -o go bob.c -lm $ ./go point x=111, y=390 This is correct: $ gcc -o go bob.c -lm $ ./go point x=112, y=390 >Fix: Please let me know if there's a work-around, I'd like to turn on optimization. >Release-Note: >Audit-Trail: >Unformatted: