0

I am having trouble with the evaluate class. An error message is coming back for the pow(x, degree). I have included the cmath library and have cast x and degree into double. Any hints or tips would be greatly appreciated. I have tested the program without the pow(x, degree) and it works.

int Poly::evaluate(int x){
    int sum=0;
    static_cast<double>(x);
    static_cast<double>(degree);
    for (int i=0; i<degree; i++) {
        sum+=coefs[i]*(pow(x, degree));//here is where I am getting the error
    }
    return sum;

}

Edited by mike_2000_17: Fixed formatting

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by sfuo
1

is degree declared elsewhere? Also, you aren't assinging the static cast anywhere. x is still int, and degree is still whatever it is wherever you declared it. You would need

double value = static_cast<double>(x);
double deg = static_cast<double>(degree);
0

I'm not sure what coefs is an array of (I'm guessing ints) but the pow() function returns a double so you would run into more casting problems there.

I would just use the c-style casting for this problem unless you need to use C++ style type-casting.

sum += coefs[i]*(int)pow((double)x, (double)degree);

Edited by sfuo: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.