Thread: Problem with pow()

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    10

    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

  2. #2
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    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?

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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().
    Last edited by anon; 11-15-2009 at 08:45 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    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

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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();
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    Thanks a lot
    Works like a charm

    Onii

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM