public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* Re: c/8825: optimization causes incorrect math
@ 2002-12-05 11:19 bangerth
  0 siblings, 0 replies; 2+ messages in thread
From: bangerth @ 2002-12-05 11:19 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, joeytsai, nobody

Synopsis: optimization causes incorrect math

State-Changed-From-To: open->closed
State-Changed-By: bangerth
State-Changed-When: Thu Dec  5 11:19:58 2002
State-Changed-Why:
    You don't want to use 
      point.x = (int) move.x;
    but want to round it to the next integer. With optimization,
    move.x is about 6e-16 smaller than 112, without it is equal
    to 112 (which is actually representable exactly as a 
    floating point number). Such differences at the level of
    round-off are to be expected with optimization.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=8825


^ permalink raw reply	[flat|nested] 2+ messages in thread

* c/8825: optimization causes incorrect math
@ 2002-12-05 10:36 joeytsai
  0 siblings, 0 replies; 2+ messages in thread
From: joeytsai @ 2002-12-05 10:36 UTC (permalink / raw)
  To: gcc-gnats


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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-12-05 19:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-05 11:19 c/8825: optimization causes incorrect math bangerth
  -- strict thread matches above, loose matches on Subject: below --
2002-12-05 10:36 joeytsai

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