calculation off by 1 :(  | |
February 12th, 2007, 12:37 AM
|
#1 (permalink)
| | Mobile Member
Join Date: Apr 2005 Location: S. Central PA
Posts: 3,601
|
this is a simple application i have to do for class, its to be done in PHP but i wanted to do it on VB first to make sure it comes out right, i did it on a calculator so i knew what my answer should be. The question: "Suppose you have 10,000, calculate how long it will take for that to grow to 1 million dollars assuming a 15% return." Nowhere in the question does it say about how i should compound it etc.. so I did a simple 15% of 10,000 after the first year is 1,500 so then the next year is 11,500 and then 15% of that, etc..
Heres the code that i used to make it work, but what i end up with is one extra year tacked on at the end. You can see by the attached screenshot that it, for whatever reason, didnt stop at 33 years which it should have, but when to 34. Its not a huge deal if i cant figure it out, i'll write in there something like years -1 = years  but I would like to know what is causing it if theres a logical explanation for it, and im sure there is. Code: Private Sub btn_Calc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Calc.Click
Dim Principal, Rate, tempAmount, Interest, FinalAmount, Years As Double
Principal = Me.tb_Investment.Text
Rate = 0.15
FinalAmount = 1000000
While (tempAmount < FinalAmount)
Interest = Principal * Rate
Principal = Principal + Interest
tempAmount = Interest + tempAmount
Years = Years + 1
Me.lb_History.Items.Add(Format(Principal, "n") _
& " at " & Years & " years")
End While
Me.tb_Total.Text = Years.ToString()
End Sub
__________________ Thinkpad T61 14.1" wide | WinXP Pro | C2D T8300 CPU | 3GB DDR2 | 160GB HDD | AGN & WWAN
Lenovo S10 10.2" LED display | 1.6Ghz Atom CPU | 1GB DDR2 | 1.3mp webcam | B/G WiFi | 160GB HDD |
| |
February 12th, 2007, 12:58 AM
|
#2 (permalink)
| | Ultimate Member
Join Date: May 2003 Location: Cheyenne, WY
Posts: 1,087
|
I've not yet worked in vb, and I'm still pretty new in c++ but shouldn't you initalize years to 0 before the loop to make sure that there isn't any leftover garbage in that variables memory location that could be throwing the calculation off?
__________________ Gi | Yuu | Jin | Rei | Makoto | Meiyo | Chuugi |
| |
February 12th, 2007, 09:24 AM
|
#3 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Sheridan Texas
Posts: 619
|
Never programmed in VB either, but will putting it this way work
Private Sub btn_Calc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Calc.Click
Dim Principal, Rate, tempAmount, Interest, FinalAmount, Years As Double
Principal = Me.tb_Investment.Text
Rate = 0.15
FinalAmount = 1000000
While
Interest = Principal * Rate
Principal = Principal + Interest
tempAmount = Interest + tempAmount
Years = Years + 1
Me.lb_History.Items.Add(Format(Principal, "n") _
& " at " & Years & " years")
End While (tempAmount = FinalAmount)
Me.tb_Total.Text = Years.ToString()
End Sub
so you won't cycle through the loop, or possibly a nested loop might work better with Years=Years + 1 outside the inner loop...
Haven't programmed in years and I'm very rusty.... |
| |
February 12th, 2007, 10:25 AM
|
#4 (permalink)
| | Real gangstas sip on Yacc
Join Date: Oct 2001 Location: Suckas-ville
Posts: 4,552
|
The problem is with the following line: tempAmount = Interest + tempAmount
Since you never intialize tempAmount the the value of Principle tempAmount is only the amount of accrued interest not the value you think it is. Initiazlie tempAmount to the initial value of principle and it will work fine.
Also there is no reason to eve USE temp amount. Principle is the amount you are incrementing, why not use that as your loop variable?
Jkrohn
__________________
Signatures blow hard
If your signature contains an ad of any kind, congratulations, you're on my ignore list.
|
| |
February 12th, 2007, 11:24 AM
|
#5 (permalink)
| | Mobile Member
Join Date: Apr 2005 Location: S. Central PA
Posts: 3,601
|
thanks jkrohn, i knew i had tempAmount in there, the first way i wrote it i used it, then i realized there is no need for it since im in a loop and im incrementing principal, i still wasnt able to figure out why it was running an extra year, but i understand what your saying and when i get home im gonna give it another run through with the preceding suggestions..
Thanks  |
| |
February 12th, 2007, 05:40 PM
|
#6 (permalink)
| | Senior Member
Join Date: May 2003 Location: Aus, Gold Coast :)
Posts: 802
|
should of done it straight into PHP - also get rid of tempAccount altogether - u basically have done everything with principle!
or u could cheat and then at the end after the loop u go years = years - 1... doesn't have to be nice, just has to work  my programming life is built on that |
| |
February 15th, 2007, 07:40 PM
|
#7 (permalink)
| | Member
Join Date: Aug 2003
Posts: 91
|
Where can I find this account that pays 15% interest?  |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |