Search: | |||||

| ||||

Home » Forum » Java | RSS Feeds |

I have to write a static method to compute x^n for an integer n,

WITHOUT using Math.pow(). they gave these formulas: when n < 0, x^n

is 1/x^-n (although, shouldn't it be 1/x^n?), n>0 and even, x^n = (x^

(n/2)^2), n>0 and odd, x^n = x^(n-1) * x.

anyhow..my problem is these error messages:

MyMath.java:46: operator ^ cannot be applied to double,double

answer = 1.0/(x^(-n));

^

C:\Documents and Settings\Chris\My Documents\MyMath.java:52:

operator ^ cannot be applied to double,double

answer = (x^(n/2))^2;

^

C:\Documents and Settings\Chris\My Documents\MyMath.java:56:

operator ^ cannot be applied to double,double

answer = (x^(n-1)) * x;

^

Here is my method:

public static double intPower(double x, double n)

{

if(n<0)

answer = 1.0/(x^(-n));

else if(n>0)

{

if(n%2 == 0) // if n is even

{

answer = (x^(n/2))^2;

}

else // n must be odd

{

answer = (x^(n-1)) * x;

}

}

return answer;

}

The method name and parameter variables are given. answer is a

static instance variable.

Didn't find what you were looking for?
Find more on operator ^ cannot be applied to double
Or get search suggestion and latest updates.

Related Topics:

- Operator '!=' cannot be applied to operands of type 'System.DateTim
- problem with Double class, strings, and primate double type
- Double Buffer
- Double-checked locking
- Double escaping problem
- Converting String to double
- write a program that enters double data into an ArrayList and calculate the total
- double manipulation
- Dynamic double
- Java getting wrong results when subtracting doubles
- Rouning with double or float
- problem of converting a number into words using double
- converting numbers into words using double
- How to run *.class with double click
- Double click pivot to get details using macro
- Single Click & Double Click on Shapes
- how to include double quote in string
- Assign "abcd to string including double quotes
- Double Quotation on String Value
- Double Boot how to upgrade The windoz?
- double connection - photography
- pls casting operator with example
- CQ Ham Radio Operators
- == operator problem
- Relation between "new" operator and "ClassLoader" class ?