Results 1 to 10 of 10
- 10-21-2009, 06:23 AM #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?
-
why are you restricted to using only ints???
- 10-21-2009, 07:20 AM #3
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
nope u can do it for int too i think
- 10-21-2009, 12:10 PM #4
Member
- Join Date
- Oct 2009
- Location
- Rotterdam
- Posts
- 52
- Rep Power
- 0
Try this:
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.Java Code:MyResultInt = (int)Math.pow((double)MyInt, (double)MyOtherInt);
- 10-21-2009, 12:14 PM #5
- 10-22-2009, 08:27 PM #6
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!
Thanks!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; } }
- 10-22-2009, 08:38 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Instead of all that pow( ... ) stuff simply do num1*num1*num1.
kind regards,
Jos
- 10-22-2009, 11:36 PM #8
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
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(); } }
- 10-23-2009, 01:34 AM #9
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.
- 10-23-2009, 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: 01-12-2009, 08:39 AM -
using instanceof to get Object type and parent type?
By xcallmejudasx in forum New To JavaReplies: 2Last Post: 11-06-2008, 06:24 PM -
[SOLVED] Cast string type to int type
By GilaMonster in forum New To JavaReplies: 9Last Post: 09-17-2008, 10:43 AM -
type mismatch: cannot convert from double to float
By bugger in forum New To JavaReplies: 2Last Post: 11-16-2007, 01:24 PM -
Help with convert a double type number
By trill in forum New To JavaReplies: 1Last Post: 08-06-2007, 08:48 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks