Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with raising to a power...

Status
Not open for further replies.

dvst8ive

Programmer
Sep 29, 2003
3
US
this seems to be an elementary problem, but one i don't think i've ever had to deal with. i'm making a class that deals with polynomial terms, adding, multiplying and evaluating a given polynomial based on its terms.

i'm writing a method to determine if a term, evaluated at 'X,' is greater than zero.

EX: term 6X^2 for X=2 > 0

my coefficient (Double) is stored and can be retrieved as can the exponent (Int) - i'm having trouble evaluating the expression because the carat won't evaluate an expression that is double^int - any tips?

public boolean isPositive(double x){
if (this.coeff * (x^(this.exp)) > 0){
return true;
}
return false;
}
 
hey guys, i kind of answered my own question here, but here's what i came up with. it's a recursive method to compute x^y (x raised to the power of y), where x & y are integers and y > 0.

public static double raiseToPower(double base, int exponent)
{
if (exponent == 1)
{ return base; }
if (exponent == 0)
{ return 1; }
else
{ return base * raiseToPower(base, exponent-1); }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top