|
From: Jose G. <jg...@ho...> - 2002-06-15 15:29:49
|
<html><div style='background-color:'><DIV><SPAN lang=EN-US style="mso-ansi-language: EN-US">Hi!</SPAN></DIV>
<DIV><SPAN lang=EN-US style="mso-ansi-language: EN-US"></SPAN>Sorry if it is too obvious!</DIV>
<DIV>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"> I am new in C++. I got 2^30= Power(2,30)= - 2147483648 in stead of + 2147483648 in my program.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US">I must have done something wrong. But I can figure it out. Can somebody help me out?<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><o:p> </o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US">#include <iostream><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US">#include <cstdlib></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><o:p> </o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US">using namespace std;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><o:p> </o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US">int Power(int a, int n);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><o:p> </o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US">int main()<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US">{<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><SPAN style="mso-spacerun: yes"> </SPAN>int a=2, n=30;<SPAN style="mso-spacerun: yes"> </SPAN>//int limit == 4294967295;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><SPAN style="mso-spacerun: yes"> </SPAN><SPAN style="mso-spacerun: yes"> </SPAN><SPAN style="mso-spacerun: yes"> </SPAN>// Power(2,30)==1073741824<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><SPAN style="mso-spacerun: yes"> </SPAN><SPAN style="mso-spacerun: yes"> </SPAN>// Power(2,31)==2147483648<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><SPAN style="mso-spacerun: yes"> </SPAN><SPAN style="mso-spacerun: yes"> </SPAN><SPAN style="mso-spacerun: yes"> </SPAN>// Power(2,32)==4294967296 out of limit => Power(2,32)==0<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><o:p> </o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><SPAN style="mso-spacerun: yes"> </SPAN>cout << "Power(" << a << "," << n << ")= " << Power(a,n) << endl;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><SPAN style="mso-spacerun: yes"> </SPAN>cout << "Power(" << a << "," << n+1 << ")= " << Power(a,n+1) << endl;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><SPAN style="mso-spacerun: yes"> </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><SPAN style="mso-spacerun: yes"> </SPAN>system("PAUSE");<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><SPAN style="mso-spacerun: yes"> </SPAN>return 0;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US">}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><o:p> </o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US">int Power(int a, int n)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US">{<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><SPAN style="mso-spacerun: yes"> </SPAN>int i=1, r=1;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><SPAN style="mso-spacerun: yes"> </SPAN>for (i=1;i<=n;i++)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><SPAN style="mso-spacerun: yes"> </SPAN>{<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><SPAN style="mso-spacerun: yes"> </SPAN>r=r*a;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><SPAN style="mso-spacerun: yes"> </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><SPAN style="mso-spacerun: yes"> </SPAN>return r;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US">}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><o:p> </o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none"><SPAN lang=EN-US style="mso-ansi-language: EN-US">I am using Dev-C++ version 4.01. in windows XP<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US">Thank you for your help to this matter.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US"><o:p> </o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="mso-ansi-language: EN-US">Jose Gana<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:place><st1:City><SPAN lang=EN-US style="mso-ansi-language: EN-US">Santiago</SPAN></st1:City><SPAN lang=EN-US style="mso-ansi-language: EN-US">, </SPAN><st1:country-region><SPAN lang=EN-US style="mso-ansi-language: EN-US">Chile</SPAN></st1:country-region></st1:place><SPAN lang=EN-US style="mso-ansi-language: EN-US">.<o:p></o:p></SPAN></P></DIV></div><br clear=all><hr>Join the worlds largest e-mail service with MSN Hotmail. <a href='http://g.msn.com/1HM505401/47'>Click Here</a><br></html>
|
|
From: <h.r...@gm...> - 2002-06-15 16:10:48
|
Hello, please no HTML mail. I never read it. regards Horst -- Horst Rößner Postf. 1802 52205 Stolberg ICQ ID #74808448 h.r...@gm... h.g...@t-... GMX - Die Kommunikationsplattform im Internet. http://www.gmx.net |
|
From: Abhijit S. <mu...@gm...> - 2002-06-15 17:18:28
|
int is, by default, signed, so one bit of your (signed) int variable (of
size 32 bits) is used to denote sign (+ve means 0, -ve means 1). 31 bits are
left, so, in effect, your int can hold from -((2**31) + 1) to +(2**31) where
** denotes exponentiation... in normal numbers, from -2147483648 to
+2147483647. The value of +2147483648 cannot be stored in the (signed) int.
It wraps around, and becomes the negative extreme. Dissect the bit-codes for
the numbers for more clarity.
The problem can be solved by using an unsigned int, which can hold a value
from 0 to +((2**32) - 1), i.e., 4294967295.
Changed code:
-----------------------------Start Code---------
#include <iostream>
#include <cstdlib>
using namespace std;
typedef unsigned int uint;
uint Power(int a, int n);
int main()
{
int a=2, n=30; // int limit == 4294967295;
// Power(2,30)==1073741824
// Power(2,31)==2147483648
// Power(2,32)==4294967296 out of limit =>
Power(2,32)==0
cout << "Power(" << a << "," << n << ")= " << Power(a,n) << endl;
cout << "Power(" << a << "," << n+1 << ")= " << Power(a,n+1) << endl;
system("PAUSE");
return 0;
}
uint Power(int a, int n)
{
int i=1;
uint r=1;
for (i=1;i<=n;i++)
{
r=r*a;
}
return r;
}
-------------------------------End Code---------
Hope this helps.
--------------------------
http://mudeth.tripod.com
----- Original Message -----
From: "Jose Gana" <jg...@ho...>
To: <dev...@li...>
Sent: Saturday, June 15, 2002 8:59 PM
Subject: [Dev-C++] weird result
> Hi!
> Sorry if it is too obvious!
> I am new in C++. I got 2^30= Power(2,30)= - 2147483648 in stead of +
2147483648 in my program.
>
> I must have done something wrong. But I can figure it out. Can somebody
help me out?
> [program follows]
|
|
From: Ioannis V. <no...@ho...> - 2002-06-15 18:00:24
|
----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Jose
Gana
Sent: Saturday, June 15, 2002 6:30 PM
To: dev...@li...
Subject: [Dev-C++] weird result
Hi!
Sorry if it is too obvious!
I am new in C++. I got 2^30= Power(2,30)= - 2147483648 in stead of +
2147483648 in my program.
I must have done something wrong. But I can figure it out. Can somebody
help me out?
#include <iostream>
#include <cstdlib>
using namespace std;
int Power(int a, int n);
int main()
{
int a=2, n=30; //int limit == 4294967295;
Because the int limit isn't that but 2147483647
// Power(2,30)==1073741824
// Power(2,31)==2147483648
// Power(2,32)==4294967296 out of
limit => Power(2,32)==0
cout << "Power(" << a << "," << n << ")= " << Power(a,n) << endl;
cout << "Power(" << a << "," << n+1 << ")= " << Power(a,n+1) << endl;
system("PAUSE");
return 0;
}
int Power(int a, int n)
{
int i=1, r=1;
for (i=1;i<=n;i++)
{
r=r*a;
}
return r;
}
I am using Dev-C++ version 4.01. in windows XP
Thank you for your help to this matter.
#include <iostream>
#include <cstdlib>
double Power(const double a, const double n);
int main()
{
using namespace std;
int a=2, n=30;
cout << "Power(" << a << "," << n << ")= " << Power(a,n) << endl;
cout << "Power(" << a << "," << n+1 << ")= " << Power(a,n+1) << endl;
system("PAUSE");
return 0;
}
double Power(const double a, const double n)
{
unsigned long i;
double r;
for (r=i=1;i<=n;i++)
r=r*a;
return r;
}
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
<http://www.noicys.cjb.net/>
* Alternative URL: http://run.to/noicys
|
|
From: Ioannis V. <no...@ho...> - 2002-06-15 18:15:46
|
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Ioannis
Vranos
Sent: Saturday, June 15, 2002 9:00 PM
To: Dev-C++ Mailing List
Subject: RE: [Dev-C++] weird result
#include <iostream>
#include <cstdlib>
double Power(const double a, const double n);
Better do that:
#include <iostream>
#include <cstdlib>
double Power(const int a, const int n);
int main()
{
using namespace std;
int a=2, n=30;
cout << "Power(" << a << "," << n << ")= " << Power(a,n) << endl;
cout << "Power(" << a << "," << n+1 << ")= " << Power(a,n+1) << endl;
system("PAUSE");
return 0;
}
double Power(const int a, const int n)
{
int i;
double r;
for (r=i=1;i<=n;i++)
r=r*a;
return r;
}
I am too tired to think something better.
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
<http://www.noicys.cjb.net/>
* Alternative URL: http://run.to/noicys
|
|
From: Ramana K. <ra...@my...> - 2002-06-16 01:40:28
|
Well, that is very weird indeed. I have played around a bit with your code
and have found a solution, but a better explaination would be welcome. I
tried
cout << 2147483648;
and the complier gave me a warning that the integer was so large it would
have to be unsigned. so i decided to make the result of the Power(int,int)
function unsigned.
Thus:
instead of
cout << "Power(" << a << "," << n+1 << ")= " << Power(a,n+1) << endl;"
you have this:
cout << "Power(" << a << "," << n+1 << ")= " << (unsigned) Power(a,n+1)
<< endl;
and it works fine.
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...]On Behalf Of Jose Gana
Sent: Sunday, 16 June 2002 1:30 AM
To: dev...@li...
Subject: [Dev-C++] weird result
Hi!
Sorry if it is too obvious!
I am new in C++. I got 2^30= Power(2,30)= - 2147483648 in stead of +
2147483648 in my program.
I must have done something wrong. But I can figure it out. Can somebody
help me out?
#include <iostream>
#include <cstdlib>
using namespace std;
int Power(int a, int n);
int main()
{
int a=2, n=30; //int limit == 4294967295;
// Power(2,30)==1073741824
// Power(2,31)==2147483648
// Power(2,32)==4294967296 out of limit
=> Power(2,32)==0
cout << "Power(" << a << "," << n << ")= " << Power(a,n) << endl;
cout << "Power(" << a << "," << n+1 << ")= " << Power(a,n+1) << endl;
system("PAUSE");
return 0;
}
int Power(int a, int n)
{
int i=1, r=1;
for (i=1;i<=n;i++)
{
r=r*a;
}
return r;
}
I am using Dev-C++ version 4.01. in windows XP
Thank you for your help to this matter.
Jose Gana
Santiago, Chile.
----------------------------------------------------------------------------
--
Join the worlds largest e-mail service with MSN Hotmail. Click Here
_______________________________________________________________ Don't miss
the 2002 Sprint PCS Application Developer's Conference August 25-28 in Las
Vegas - http://devcon.sprintpcs.com/adp/index.cfm?source=osdntextlink
_______________________________________________ Dev-cpp-users mailing list
Dev...@li...
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|