public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/33971]  New: Type promotion in mixed integer / floating point arithmetic and rounding
@ 2007-11-01 14:26 hudson+gcc at osresearch dot net
  2007-11-01 14:28 ` [Bug c/33971] " hudson+gcc at osresearch dot net
  2007-11-01 14:35 ` schwab at suse dot de
  0 siblings, 2 replies; 3+ messages in thread
From: hudson+gcc at osresearch dot net @ 2007-11-01 14:26 UTC (permalink / raw)
  To: gcc-bugs

This may be another addition to bug 323, although it doesn't involve floating
point comparison.  I am integrating the output of (drand48()-0.5)*10 with an
integer and get wildly different results for "x += r" versus "x += (int) r",
where x is an integer and r is a value from drand48.  I would expect a random
walk around the initial value for x in both cases, but in the first case the
integrated value heads directly towards zero and then random walks around zero,
while the second remains around the initial value.

Looking at the assembly output, it appears that the first converts x to a
double, performs the arithmetic as floating point, then converts back to and
integer, while the second converts the floating point random value to an
integer and then does the add as an integer.  The results are the same on
i386-redhat-linux as x86_64.

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
        int iter = 100000;
        int x = 1000;
        int y = 1000;
        double z = 1000;

        while( iter-- > 0 )
        {
                double r = (drand48() - 0.5) * 10;
                x += r;
                y += (int) r;
                z += r;
                printf( "%d %d %.0f\n", x, y, z );
        }

        return 0;
}

A graph of the output showing the x value going straight to zero and the other
two doing their random walk is at http://www.swcp.com/~hudson/rand.png


-- 
           Summary: Type promotion in mixed integer / floating point
                    arithmetic and rounding
           Product: gcc
           Version: 4.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: hudson+gcc at osresearch dot net
  GCC host triplet: x86_64-redhat-linux
GCC target triplet: x86_64-redhat-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33971


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

* [Bug c/33971] Type promotion in mixed integer / floating point arithmetic and rounding
  2007-11-01 14:26 [Bug c/33971] New: Type promotion in mixed integer / floating point arithmetic and rounding hudson+gcc at osresearch dot net
@ 2007-11-01 14:28 ` hudson+gcc at osresearch dot net
  2007-11-01 14:35 ` schwab at suse dot de
  1 sibling, 0 replies; 3+ messages in thread
From: hudson+gcc at osresearch dot net @ 2007-11-01 14:28 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from hudson+gcc at osresearch dot net  2007-11-01 14:28 -------
Created an attachment (id=14452)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=14452&action=view)
Graph showing the integrator output


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33971


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

* [Bug c/33971] Type promotion in mixed integer / floating point arithmetic and rounding
  2007-11-01 14:26 [Bug c/33971] New: Type promotion in mixed integer / floating point arithmetic and rounding hudson+gcc at osresearch dot net
  2007-11-01 14:28 ` [Bug c/33971] " hudson+gcc at osresearch dot net
@ 2007-11-01 14:35 ` schwab at suse dot de
  1 sibling, 0 replies; 3+ messages in thread
From: schwab at suse dot de @ 2007-11-01 14:35 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from schwab at suse dot de  2007-11-01 14:35 -------
That's how C works.  x += r is the same as x = x + r.


-- 

schwab at suse dot de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33971


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

end of thread, other threads:[~2007-11-01 14:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-01 14:26 [Bug c/33971] New: Type promotion in mixed integer / floating point arithmetic and rounding hudson+gcc at osresearch dot net
2007-11-01 14:28 ` [Bug c/33971] " hudson+gcc at osresearch dot net
2007-11-01 14:35 ` schwab at suse dot de

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