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