Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Exponents in JavaScript without Math.pow

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 04-05-2009, 08:10 PM PM User | #1 autokustomizer New Coder   Join Date: Apr 2009 Posts: 18 Thanks: 12 Thanked 0 Times in 0 Posts Exponents in JavaScript without Math.pow 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 08:18 PM.. Reason: Trying to get the code to come up correctly.
04-05-2009, 08:27 PM   PM User | #2
jmrker
Senior Coder

Join Date: Aug 2006
Location: FL
Posts: 2,935
Thanks: 36
Thanked 477 Times in 471 Posts

Quote:
 Originally Posted by autokustomizer ... 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?

 Users who have thanked jmrker for this post: autokustomizer (04-05-2009)
 04-05-2009, 08:30 PM PM User | #3 autokustomizer New Coder   Join Date: Apr 2009 Posts: 18 Thanks: 12 Thanked 0 Times in 0 Posts 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.
 04-05-2009, 10:13 PM PM User | #4 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,938 Thanks: 75 Thanked 4,305 Times in 4,272 Posts And can you tell us WHY you can't use Math.pow( ) ???? Without Math.pow, your only choice is to use a loop and multiply the value times itself 44 times. That is, of course, what "power" means. That is, Math.pow( 3, 4 ) ==>> 3 * 3 * 3 * 3 and so on. The one thing you do *NOT* want to do is *EVER* ANY PLACE IN THERE multiply by 44 !!! The proper formula for compound interest is: Code: `futureValue = presentValue * ( ( 1 + interestRate ) ^ numberOfPeriods )` or, in JavaScript notation: Code: `futureValue = presentValue * Math.pow( 1 + interestRate, numberOfPeriods ) )` So what is stopping you from just using that formula??? Don't know why you ever thought you should multiply by 44, because you clearly SHOULD NOT.
 Users who have thanked Old Pedant for this post: autokustomizer (04-05-2009)
 04-05-2009, 10:18 PM PM User | #5 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,938 Thanks: 75 Thanked 4,305 Times in 4,272 Posts Oh, what the hell... 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.
 Users who have thanked Old Pedant for this post: autokustomizer (04-05-2009)
 04-05-2009, 11:31 PM PM User | #6 autokustomizer New Coder   Join Date: Apr 2009 Posts: 18 Thanks: 12 Thanked 0 Times in 0 Posts 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 11:47 PM..
 04-06-2009, 12:10 AM PM User | #7 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,938 Thanks: 75 Thanked 4,305 Times in 4,272 Posts 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.
 Users who have thanked Old Pedant for this post: autokustomizer (04-06-2009)
 04-06-2009, 12:39 AM PM User | #8 autokustomizer New Coder   Join Date: Apr 2009 Posts: 18 Thanks: 12 Thanked 0 Times in 0 Posts I apologize, sorry for the misinterpitation, but I did learn something new today! Thank you for your time and help. It was greatly appreciated!! CB
 04-06-2009, 12:56 AM PM User | #9 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,938 Thanks: 75 Thanked 4,305 Times in 4,272 Posts 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.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 02:25 AM.