Results 1 to 10 of 10
 10212009, 06:23 AM #1Member
 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?

why are you restricted to using only ints???
 10212009, 07:20 AM #3Senior Member
 Join Date
 Oct 2009
 Location
 California,US
 Posts
 201
 Rep Power
 12
nope u can do it for int too i think
 10212009, 12:10 PM #4Member
 Join Date
 Oct 2009
 Location
 Rotterdam
 Posts
 52
 Rep Power
 0
Try this:
Java Code:MyResultInt = (int)Math.pow((double)MyInt, (double)MyOtherInt);
 10212009, 12:14 PM #5
 10222009, 08:27 PM #6Member
 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); return answer; } }
 10222009, 08:38 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 29
Instead of all that pow( ... ) stuff simply do num1*num1*num1.
kind regards,
Jos
 10222009, 11:36 PM #8Senior Member
 Join Date
 Oct 2009
 Location
 California,US
 Posts
 201
 Rep Power
 12
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); return answer; } } /**inner class main*/ class cubeprint1 { public static void main(String[] args) { new cube(); } }
 10232009, 01:34 AM #9Member
 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, (n1)/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.
 10232009, 04:38 AM #10
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; }
CodesAway  codesaway.info
writing tools that make writing code a little easier
Similar Threads

Double.valueOf() vs Double.parseDouble()
By greenbean in forum New To JavaReplies: 10Last Post: 01122009, 08:39 AM 
using instanceof to get Object type and parent type?
By xcallmejudasx in forum New To JavaReplies: 2Last Post: 11062008, 06:24 PM 
[SOLVED] Cast string type to int type
By GilaMonster in forum New To JavaReplies: 9Last Post: 09172008, 10:43 AM 
type mismatch: cannot convert from double to float
By bugger in forum New To JavaReplies: 2Last Post: 11162007, 01:24 PM 
Help with convert a double type number
By trill in forum New To JavaReplies: 1Last Post: 08062007, 08:48 AM
Bookmarks