# is Math.pow only used with type double?

• 10-21-2009, 06:23 AM
AprilFlowers
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?
• 10-21-2009, 06:50 AM
Fubarable
why are you restricted to using only ints???
• 10-21-2009, 07:20 AM
[RaIdEn]
nope u can do it for int too i think
• 10-21-2009, 12:10 PM
Arnold
Try this:
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.
• 10-21-2009, 12:14 PM
RamyaSivakanth
Gothru the api.Don't do with assumptions.

Java Platform SE 6
• 10-22-2009, 08:27 PM
rclausing
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!

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);         return answer;         } }```
Thanks!
• 10-22-2009, 08:38 PM
JosAH
Instead of all that pow( ... ) stuff simply do num1*num1*num1.

kind regards,

Jos
• 10-22-2009, 11:36 PM
[RaIdEn]
ok since here is another way to do it

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);                 return answer;         } } /**inner class main*/  class cubeprint1 {         public static void main(String[] args)         {                        new cube();         } }```
• 10-23-2009, 01:34 AM
rdtindsm
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.
• 10-23-2009, 04:38 AM
CodesAway
Quote:

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?

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; }```