# Thread: is Math.pow only used with type double?

1. Member
Join Date
Oct 2009
Posts
2
Rep Power
0

## is Math.pow only used with type double?

I'm trying to calculate compound interest only using integers and I keep getting errors messages associated with this formula:

amount = principal * Math.pow( 1.0 + rate, year );

I get stuff like:

InterestTwo.java:20: pow(double,double) in java.lang.Math cannot be applied to (int)

or

InterestTwo.java:20: possible loss of precision
found : double
required: int
amount = principal * Math.pow( 1.0 + rate, year );

is this because I'm trying to only use int? How do I calculate compound interest only using int then?

2. Senior Member
Join Date
Oct 2009
Location
California,US
Posts
201
Rep Power
5
nope u can do it for int too i think

3. Member
Join Date
Oct 2009
Location
Rotterdam
Posts
52
Rep Power
0
Try this:
Java Code:
MyResultInt = (int)Math.pow((double)MyInt, (double)MyOtherInt);
With the (double) in the arguments you say that you want to convert your input integers into doubles. With the (int) you say that you want to convert yout output double back into an integer.

4. Gothru the api.Don't do with assumptions.

Java Platform SE 6

5. Member
Join Date
Oct 2009
Location
Oregon
Posts
22
Rep Power
0
I just ran into this problem this morning. And it's how I found the forum. Anyway, after struggling for a few a while, Arnold showed me the answer. The problem involved writing up a method that accepted a single integer parameter and then returns that value raised to the 3rd power. What I couldn't remember how to do was cast the Math.pow method as an integer. If someone has a moment to look at my code and see if there is a more efficient way of doing this I would be very grateful!

Java Code:
import java.util.Scanner;

public class cube
{
public int num1;
private int num2;

public cube()
{
Scanner getnumber = new Scanner (System.in);

System.out.print ("please enter a whole number:");
num1 = getnumber.nextInt();
num2 = (int)Math.pow(num1, 3);

}

public String toString()
{
String answer = Integer.toString(num2);
}
}
Thanks!

6. Instead of all that pow( ... ) stuff simply do num1*num1*num1.

kind regards,

Jos

7. Senior Member
Join Date
Oct 2009
Location
California,US
Posts
201
Rep Power
5
ok since here is another way to do it

Java Code:
import java.util.Scanner;

public class cube
{
public int num1;
private int num2;

public cube()
{
Scanner getnumber = new Scanner (System.in);

System.out.println ("please enter a whole number:");
num1 = getnumber.nextInt();
num2 = power(num1);
System.out.println(num2);
}

public int power(int x)
{
return x*x*x;
}

public String toString()
{
String answer = Integer.toString(num2);
}
}

/**inner class main*/
class cubeprint1
{
public static void main(String[] args)
{
new cube();
}

}

8. Member
Join Date
Feb 2009
Posts
92
Rep Power
0
Power(x,n):
input: a number x and integer n >= 0
output: x^n
if n = 0 then
return 1
if n is odd then
y = Power(x, (n-1)/2)
return x.y.y
else
y = Power(x,n/2)
return y * y

recursive method for any n
consider x^5 = x*x*x*x*x 4 multiply
x^5
Y= X*X
Y = Y*Y*X

Sure that this is more than you want to do but:
Implements any power;
Short code;
Running time grows log(n) rather than linear in n
avoids floating point casts, so the algorithm constants may compensate
for overhead of recursive calls for small n
strongly suspect that integer multiply is faster than floating point, don't know for sure

To understand recursion, first you have to understand recursion.

9. Originally Posted by rdtindsm
To understand recursion, first you have to understand recursion.
And second, you have to understand how recursion ends :D

However, why are you using a recursive method, instead of an iterative one?

Java Code:
public static int pow(int num, int exponent)
{
if (exponent < 0)
throw new ArithmeticException("Negative exponent");
if (num==0)
return (exponent==0 ? 1 : 0);

// Perform exponentiation using repeated squaring trick

int result = 1;
int multiplier = num;

while (exponent != 0) {
if ((exponent & 1) == 1) {
// if odd, multiply result
// x^5 = (x)*(x^2)^4
result *= multiplier;
}
if ((exponent >>>= 1) != 0) {
multiplier *= multiplier;
}
}

return result;
}

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•