Results 1 to 10 of 10
  1. #1
    AprilFlowers is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    nope u can do it for int too i think

  4. #4
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Default

    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.

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    Gothru the api.Don't do with assumptions.

    Java Platform SE 6
    Ramya:cool:

  6. #6
    rclausing is offline Member
    Join Date
    Oct 2009
    Location
    Oregon
    Posts
    22
    Rep Power
    0

    Default

    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;
    	}
    }
    Thanks!

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

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

    kind regards,

    Jos

  8. #8
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    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();
    	}
    
    }

  9. #9
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    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. #10
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Quote Originally Posted by rdtindsm View Post
    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;
    }
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

Similar Threads

  1. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 09:39 AM
  2. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 07:24 PM
  3. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 11:43 AM
  4. Replies: 2
    Last Post: 11-16-2007, 02:24 PM
  5. Help with convert a double type number
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 09:48 AM

Posting Permissions

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