Help math.pow

1. Senior Member
Join Date
Dec 1969
Posts
359

## Help math.pow

Ok I have An initial amount 10,000.00 with an interest rate of 10% on a 3 year term wich comes to a total of 13,310.00 compounded at the end of 3 years now I need to calculate the agent commision witch is 2%, so basically first year he makes 220.00 2nd year 242.00 3rd year 266.20 total 728.20.bassically at the end of the year I need to calculate the 2% commision of the initial maount with the interest. also the terms are selected by user 1,2 or 3 years how do I calculate this if 2 years selected show commission 242.00 etc<BR><BR>I have some code but can&#039;t seem to get those figures can anybody please help me out<BR><BR>Thanx<BR>AJ<BR>intrate = 10%<BR>comm = 2%<BR>invamnt = 10,000.00<BR><BR><BR>for (i=0;i&#060;years;i++) {<BR> var inttotal = Math.round((invamnt*(Math.pow((1+intrate/100),years))-invamnt)*100)/100;<BR> var invmattotal = invamnt + inttotal;<BR> var commtotal = Math.round((invmattotal*(Math.pow((1+comm/100),years))-invmattotal)*100)/100;<BR>}

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## You don't need Math.pow

Since you want to use a loop to get the annual commisions, you *SHOULDN&#039;T* use Math.pow. That formula you are playing with is how you *avoid* using a loop.<BR><BR>var intratePct = 10;<BR>var commPct = 2;<BR>var invamnt = 10000.0;<BR>var years = 3;<BR><BR>var totalamnt = invamnt;<BR>var totalcomm = 0;<BR><BR>var intrate = intratePct / 100.0;<BR>var comm = commPct / 100.0;<BR><BR>for ( yrnum = 1; yrnum &#060;= years; ++yrnum ) <BR>{<BR> var yrint = totalamnt * intrate;<BR> totalamnt += yrint;<BR> var yrcomm = Math.round( totalamnt * comm );<BR> alert("Commission for year #" + yrnum + " is " + yrcomm);<BR> totalcomm += yrcomm;<BR>}<BR>

3. Senior Member
Join Date
Dec 1969
Posts
359

## RE: You don't need Math.pow

well Not that I really want to use the loop I just thought I had to. If you don&#039;t mind How can I do this with the math.pow()<BR><BR>Thanx<BR>AJ

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## I don't see a way...

..to get the commission correct and use Math.pow().<BR><BR>[Well, you *could* use Math.pow() just to get the ending total amount, but what&#039;s the point? Since you&#039;ll need to do the loop to get the correct commissions? It&#039;s pointless.]<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
359

## RE: I don't see a way...

I just finished with what you gave me and it works beatifully Thanx for the help I owe you a nice cold one.And for two cold ones maybe you can tell me how I could get the interest amount of the initial investment to date.Bassically if user logs in and looks at his account show him how much interest he has accumulated to that day.<BR><BR>Thanx<BR>AJ

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## To the day?

Didn&#039;t we have this discussion a week or two ago??<BR><BR>How accurate does it need to be?<BR><BR>Let&#039;s see...<BR><BR>You can get pretty close by using the annual interest rate divided by 365.25 and then using the Math.pow formula for the number of days instead of number of years.<BR><BR>You can figure the number of days by using Date.getTime() method, which gives # of milliseconds since some start time. Subtract the results of using getTime() with today and using getTime() with the start date of the investment. Divide by number of milliseconds in one day to get # of days.<BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
359

## RE: To the day?

Yeah you did explain it to me roughly but I wasn&#039;t able to get it to work.But I&#039;ll keep on trying. Maybe with this more detailed explanation I&#039;ll get it.<BR>Thanx alot for your help.<BR>AJ

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## Oh, nuts!

I just realized!<BR><BR>If you do as I suggested, then you will get a much higher interest paid than you suggest. <BR><BR>That&#039;s because that formula you are using with Math.pow( ) *assumes* that you are compounding at a rate that matches the length of the period!<BR><BR>So if you use days, then it assumes you are compounding DAILY!<BR><BR>And (just to pick an example) 10% annual rate, when compounded daily, works out to an "Annual Percentage Yield" of about 10.5%!!!!<BR><BR>Ever noticed how some banks will advertise two rates on savings deposits? One an annual rate and the other an annual *YIELD*? That is what this is all about!<BR><BR>So if this investment doesn&#039;t do that--if it really *DOES* pay a simple annual interest rate--then you don&#039;t need to go to that much trouble. You can simply calculate the annual amount for the last year that includes today&#039;s date and pro-rate the interest based on the number of days.<BR><BR>Well...that&#039;s actually *harder* to calculate! But it&#039;s logically simpler.<BR><BR>

9. Senior Member
Join Date
Dec 1969
Posts
359

## I'm Confused

yoou&#039;re getting me even more confused. I knew I should have payed attention in math class.<BR>Thanx for the advice I&#039;ll figure it out some how.

10. Senior Member
Join Date
Dec 1969
Posts
96,118

## Here...

&#060;HTML&#062;&#060;BODY&#062;<BR><BR>&#060;FORM &#062;<BR>What was the investment amount? &#060;INPUT Name="Amt"&#062;<BR>&#060;P&#062;<BR>What day did investment start? &#060;INPUT Name="Start"&#062;<BR>&#060;P&#062;<BR>What was interest rate? &#060;INPUT Name="Rate"&#062;<BR>&#060;P&#062;<BR>&#060;INPUT Type=Button Value="Calculate interest to date" onClick="calc(this.form);"&#062;<BR>&#060;P&#062;< BR>Interest to date &#060;INPUT Name="IntToDate" ReadOnly Value="0"&#062;<BR>&#060;P&#062;<BR>Total to date &#060;INPUT Name="TotalToDate" ReadOnly Value="0"&#062;<BR>&#060;/FORM&#062;<BR><BR>&#060;SCRIPT&#062;<BR>function calc(frm)<BR>{<BR> var amt = parseFloat(frm.Amt.value);<BR> var start = new Date( frm.Start.value);<BR> var rate = parseFloat(frm.Rate.value);<BR> if ( isNaN(amt) &#124&#124 isNaN(Date.parse(frm.Start.value)) &#124&#124 isNaN(rate) )<BR> {<BR> alert("Bad value(s)");<BR> return;<BR> }<BR> var today = new Date();<BR> today.setHours(0);<BR> today.setMinutes(0);<BR> today.setSeconds(0);<BR> today.setMilliseconds(0);<BR><BR> // fix rate<BR> rate /= 100.0;<BR> <BR> // diff between two dates in ms:<BR> var ms = today.getTime() - start.getTime();<BR> // and in days<BR> var days = ms / ( 24 * 60 * 60 * 1000 );<BR> var yrs = Math.floor( days / 365.25 );<BR> days -= yrs * 365.25; // days remaining in last year<BR><BR> var tty = amt; // total to this year<BR> if ( yrs &#062; 0 ) tty = amt * Math.pow( 1 + rate, yrs );<BR><BR> var iy = rate * tty; // interest that would be earned this year<BR> // add whole years&#039; interest to proportionate for this year<BR> var itd = (tty-amt) + ( iy * ( days / 365.25 ) ); <BR><BR> frm.IntToDate.value = itd;<BR> frm.TotalToDate.value = amt + itd;<BR>}<BR>&#060;/SCRIPT&#062;<BR><BR>&#060;/BODY&#062;&#060;/HTML&#062;<BR><BR>*****************<BR><BR>It&#039 ;s not exact (doesn&#039;t take into account actual # of days per year), but it&#039;s really close. Within pennies on a \$1000 investment, for example.<BR><BR>

#### Posting Permissions

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