-
Problem with pow()
Heey,
I just learned about arrays so i wanted to create a power table.
This is my current code:
Code:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int x;
int y;
double x_double;
double y_double;
int power_table[11][11]; //[x]
for(x = 1; x < 11; x++)
{
for(y = 1; y < 11; y++)
{
y_double = y;
x_double = x;
power_table[x][y] = pow(y_double,x_double);
}
}
for(x = 1; x < 11; x++)
{
cout<<"Power Table of "<< x << endl;
for( y = 1; y < 11; y++)
{
cout<<" "<< y <<"^"<< x <<"\t=\t"<< power_table[x][y] <<"\n";
}
}
cin.get();
}
Right now it's working only my compiler gives me the following warning:
Code:
\main.cpp(21) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
(If it makes a difference, im using visual studio 2008 pro)
I created doubles to equal the integers because the pow function can't use integers
as far as i got from the research on the web. Besides, it gave me this error message:
Code:
Compiler Error C2108 - subscript is not of integral type.
I tried float before, but there were 2 downsides, it gave the same warning as double (only now from float to int and back) and it took significantly longer than double. In fact, it took 4s.
Pointers don't seem to work either.
my question is, how should this be done? Is the double fine or should it be done different altogether? Hope someone could help me out,
Onii
-
Before i forget,
I Also got this:
Code:
9^10 = -2147483648
9^10 = -2147483648
I thought this occures because it surpasses the double value limit?
Is there a way to fix this?
-
That is exactly what the warning is talking about. Your int type is not large enough to store values that large. (Another part of the data loss being losing any decimal part, should there be any.)
You could store the values as an array of doubles. Or you could use a larger integer type (e.g to-be-standard unsigned long long).
As to pow expecting doubles and indexing expecting integers, you can just cast those ints to doubles when calling pow().
-
The unsigned long long doesnt work.
if i change:
Code:
double x_double;
double y_double;
into:
Code:
unsigned long long x_double;
unsigned long long y_double;
I get this error message:
Code:
c:\...\main.cpp(20) : error C2668: 'pow' : ambiguous call to overloaded function
c:\program files\microsoft visual studio 9.0\vc\include\math.h(575): could be 'long double pow(long double,int)'
c:\program files\microsoft visual studio 9.0\vc\include\math.h(573): or 'long double pow(long double,long double)'
c:\program files\microsoft visual studio 9.0\vc\include\math.h(527): or 'float pow(float,int)'
c:\program files\microsoft visual studio 9.0\vc\include\math.h(525): or 'float pow(float,float)'
c:\program files\microsoft visual studio 9.0\vc\include\math.h(489): or 'double pow(double,int)'
c:\program files\microsoft visual studio 9.0\vc\include\math.h(123): or 'double pow(double,double)'
while trying to match the argument list '(unsigned __int64, unsigned __int64)'
With casting those ints to doubles do you mean this? (The thing im doing right now?)
Code:
y_double = y;
x_double = x;
power_table[x][y] = pow(y_double,x_double);
Sorry, my english lets down on me from time to time.
Thnx a lot for your help,
Onii
-
There are two problems. Where you store the results (power_table) and getting the arguments right for pow().
Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int x;
int y;
double power_table[11][11]; //or you can use not-yet-standard unsigned long long
for(x = 1; x < 11; x++)
{
for(y = 1; y < 11; y++)
{
//you can cast variables to suitable type
power_table[x][y] = pow(static_cast<double>(y),static_cast<double>(x));
}
}
std::cout << std::fixed << std::setprecision(0); //formating for displaying doubles
for(x = 1; x < 11; x++)
{
cout<<"Power Table of "<< x << endl;
for( y = 1; y < 11; y++)
{
cout<<" "<< y <<"^"<< x <<"\t=\t"<< power_table[x][y] <<"\n";
}
}
cin.get();
}
-
Thanks a lot :D
Works like a charm
Onii