I am working on a problem that is simulating an IRA account. Someone enters their initial deposit, and then that number needs to be calculated out to a certain number of years. Is there any way to take a number to a power of itself without using the Math.pow?? I realized that by using this statement,
{CODE} whenS = (44 * (initialDep * (1 + interest))); {CODE}, I am not getting the correct answer, because after 44 years the compounded interest isn't calculated that way, but I cannot use the pow statement. I appreciate any ideas....I was thinking to use a loop to calculate it out...but I am still at the delima of not knowing how to use exponents without the pow. Thank you
Last edited by autokustomizer; 04-05-2009 at 07:18 PM..
Reason: Trying to get the code to come up correctly.
...
I am not getting the correct answer, because after 44 years the compounded interest isn't calculated that way, but I cannot use the pow statement. ...
So what is the correct formula to use after 44 years?
What is the answer you are getting
and what is the correct answer you expect to get?
I am trying to get whenS = (44 * (initialDep * (1 + interest)^44)); instead of multiplying it by the first 44, I am using the compound interest formula.
If you really think you shouldn't use Math.pow -- and I *still* don't see why you think you shouldn't -- you could code that as:
Code:
function futureValueOf( presentValue, periodicInterestRate, numberOfPeriods )
{
var periodicGrowth = 1 + periodicInterestRate;
var factor = 1;
for ( var period = 1; period <= numberOfPeriods; ++period )
{
factor = factor * periodicGrowth;
}
return presentValue * factor;
}
But sure seems silly.
NOTE: Interest rate MUST be expressed as a decimal (not as a percent...so 1/100th of the percentage rate) and represent the rate per period. So if your periods are actually months, then the rate must be per month.
The pow statement would make it too easy, lol. My periods are once a year at a firm 10.00%. I am new to programming, I am trying to learn Java on my own. I went to Barnes and Noble and bought a Java Programming book, and I am working problems out of the book. The code you wrote does not appear to be completely in Java, so I am having difficulty understanding all of it...but I could be wrong, again I am just learning. Thank you.
Last edited by autokustomizer; 04-05-2009 at 10:47 PM..
You are posting in a JAVASCRIPT forum, *NOT* a JAVA forum.
The names are almost the only things the two languages have in common. Java is closer to C# and, in many ways, to C++ than it is to JavaSCRIPT.
Syntax and some standard library object names and methods are similar, but that's about as far as it goes. JavaSCRIPT does *NOT* have data types. The var declaration declares a variable that can hold *ANY* type, from a boolean to an integer to a floating point value to a string to an object reference to a function reference.
If you want to ask Java questions, try the Java forum, please.
I'll get you started with the Java version of my code, but then it's time to change forums, okay?
Code:
double futureValueOf( double presentValue, double periodicInterestRate, int numberOfPeriods )
{
double periodicGrowth = 1.0 + periodicInterestRate;
double factor = 1.0;
for ( int period = 1; period <= numberOfPeriods; ++period )
{
factor = factor * periodicGrowth;
}
return presentValue * factor;
}
Untested, and my Java is slightly rusty, but I think that's right.
Well, neither JMerker nor I noticed that you said "Java", so it's clearly not all your fault! Not that there's any "fault" to be assigned, really. It's a pretty common mistake, albeit usually it's made the other way around, with people calling JavaScript "Java coding." So...sorry about my misleading you, too.