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/8825: optimization causes incorrect math


>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 <stdio.h>
#include <math.h>

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:


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