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