public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
From: joeytsai@yahoo.com
To: gcc-gnats@gcc.gnu.org
Subject: c/8825: optimization causes incorrect math
Date: Thu, 05 Dec 2002 10:36:00 -0000	[thread overview]
Message-ID: <20021205183537.18136.qmail@sources.redhat.com> (raw)


>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:


             reply	other threads:[~2002-12-05 18:36 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-12-05 10:36 joeytsai [this message]
2002-12-05 11:19 bangerth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20021205183537.18136.qmail@sources.redhat.com \
    --to=joeytsai@yahoo.com \
    --cc=gcc-gnats@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).