C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

the usual conversions

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Marco Manfredini
Guest





PostPosted: Tue Jul 27, 2004 4:32 pm    Post subject: the usual conversions Reply with quote



I found this issue here with gcc 3.4.1 and I'm unsure about it:

#include <iostream>
using namespace std;

int f0(int x)
{
int k=1000000;
return (x*1000000)/1000000;
}
int f1(int x)
{
const int k=1000000;
return (x*k)/k;
}
int f2(int x)
{
int k=1000000;
return (x*k)/k;
}

int main()
{
cout << f0(10000) << endl;
cout << f1(10000) << endl;
cout << f2(10000) << endl;
}

The output of this is when compiled (even without optimizations) is:

10000
10000
1410

GCC uses the equation (K*x)/K=x to fold the expressions with *constant*
K, giving different output, because the overflow does not occur[1].

Did I overlook anything that could justify this behaviour? Or is it just
dead wrong, as I think, because it omits the "ususal conversions"?

Marco

[1] No problem here, if x was declared as a small type, where x *
1000000 could not possibly overflow.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
tom_usenet
Guest





PostPosted: Wed Jul 28, 2004 2:26 am    Post subject: Re: the usual conversions Reply with quote



On 27 Jul 2004 12:32:56 -0400, Marco Manfredini <nospam (AT) phoyd (DOT) net>
wrote:

Quote:
I found this issue here with gcc 3.4.1 and I'm unsure about it:

#include using namespace std;

int f0(int x)
{
int k=1000000;
return (x*1000000)/1000000;
}
int f1(int x)
{
const int k=1000000;
return (x*k)/k;
}
int f2(int x)
{
int k=1000000;
return (x*k)/k;
}

int main()
{
cout << f0(10000) << endl;
cout << f1(10000) << endl;
cout << f2(10000) << endl;
}

The output of this is when compiled (even without optimizations) is:

10000
10000
1410

That seems correct (as would any other output).

Quote:
GCC uses the equation (K*x)/K=x to fold the expressions with *constant*
K, giving different output, because the overflow does not occur[1].

Did I overlook anything that could justify this behaviour? Or is it just
dead wrong, as I think, because it omits the "ususal conversions"?

All three functions have signed int overflow, therefore all three have
undefined behaviour and can validly output anything at all or just
crash your computer.

Tom

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Hyman Rosen
Guest





PostPosted: Wed Jul 28, 2004 2:54 am    Post subject: Re: the usual conversions Reply with quote



Marco Manfredini wrote:
Quote:
GCC uses the equation (K*x)/K=x to fold the expressions with *constant*
K, giving different output, because the overflow does not occur[1].

Overflow on signed arithmetic is undefined behavior.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.