calculator language

• 09-27-2012, 01:11 AM
johnsonjp34
calculator language
Greetings all,

I'm attempting to build a compound interest/savings calculator for fun, right? I have defined:

double principal
double annualRate
int periodsPerYear
int years
double savings

I'm trying to figure out the right way to put an exponential in the equation.

The textbook formula would be savings = principal*(1+annualRate/periodsPerYear)^periodsPerYear*year .

How do I write this with the exponent in java/android.

Thanks,

J
• 09-27-2012, 01:22 AM
PhQ
Re: calculator language
I'm thinking it will look something like this:
double savings = Math.pow(principal*(1+annualRate/periodsPerYear), periodsPerYear*year);.
I'm not sure if it works thought. If it doesn't work try swapping the parameters.. Take a look at Math (Java Platform SE 6), double) and you will figure it out :)
• 09-28-2012, 03:16 AM
johnsonjp34
Re: calculator language
Looks reasonable. Thanks.
• 09-28-2012, 05:25 PM
johnsonjp34
Re: calculator language
I tried this double savings = Math.pow(principal*(1+(annualRate/100)/periodsPerYear),periodsPerYear*years); and I keep getting insanely high numbers such as 8.673617379884036E221. Any ideas anyone?
• 09-28-2012, 05:46 PM
rru96
Re: calculator language
Your raising it to the exponent of periodsPerYear*years. So if it was basically a \$200,000 principle over 30, you are raising to the power of 360! That should be fairly large should it not. Math.pow()'s second value is for exponent. In this case that would be 30 years * 12 months. If Math.pow(3,3) = 9. Google showed that 2^360 is 2.348543e+108, so imagine the firse value (principle * (1 +..., to the exponent of 360)