home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

calculation off by 1 :(

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1471
Discussions: 200,953, Posts: 2,379,478, Members: 246,316
Old February 12th, 2007, 12:37 AM   Digg it!   #1 (permalink)
Mobile Member
 
ShuckyD's Avatar
 
Join Date: Apr 2005
Location: S. Central PA
Posts: 3,601
calculation off by 1 :(

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
ShuckyD is offline   Reply With Quote
Old February 12th, 2007, 12:58 AM     #2 (permalink)
Vyx
Ultimate Member
 
Vyx's Avatar
 
Join Date: May 2003
Location: Cheyenne, WY
Posts: 1,087
Send a message via AIM to Vyx Send a message via MSN to Vyx Send a message via Skype™ to Vyx
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
Vyx is offline   Reply With Quote
Old 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....
Max_Confusion is offline   Reply With Quote
Old February 12th, 2007, 10:25 AM     #4 (permalink)
Real gangstas sip on Yacc
 
jkrohn's Avatar
 
Join Date: Oct 2001
Location: Suckas-ville
Posts: 4,552
Send a message via ICQ to jkrohn Send a message via AIM to jkrohn Send a message via Yahoo to jkrohn
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.
jkrohn is offline   Reply With Quote
Old February 12th, 2007, 11:24 AM     #5 (permalink)
Mobile Member
 
ShuckyD's Avatar
 
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
ShuckyD is offline   Reply With Quote
Old February 12th, 2007, 05:40 PM     #6 (permalink)
Senior Member
 
Join Date: May 2003
Location: Aus, Gold Coast :)
Posts: 802
Send a message via ICQ to exally
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
exally is offline   Reply With Quote
Old February 15th, 2007, 07:40 PM     #7 (permalink)
Member
 
Bootlegger's Avatar
 
Join Date: Aug 2003
Posts: 91
Where can I find this account that pays 15% interest?
Bootlegger is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
Asteroid Calculation Program phenious IMO Community 0 April 13th, 2004 04:38 PM
best processor for calculation? fcitori Processors, Memory, and Overclocking 3 March 16th, 2004 10:40 AM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
The disrespect of Obama by Russian .. (41)
Is It Just Me? (2943)
Making Health Care Worse (178)
Wireless Televisions. (12)
CPU fan stops spinning randomly (8)
windows 7 problem (7)
Regular Build (11)
Is the PSU I received dead? (12)
radeon x850xt platinum & shader.. (6)
Print spooler problem (15)
windows vista security holes (10)
HIS HD5770 graphic card question (15)
Install XP pro and a Vista laptop ?.. (11)
Foreign voltage (10)
Recent Discussions
How to Enjoy Your Favorite Videos on .. (0)
EVGA 9800 gtx help with finding a goo.. (11)
Regular Build (11)
Help with onclick and buttons (0)
Modern Warfare 2: Who Bought It? (63)
Virus advise (8)
My monitor won't turn on after instal.. (1)
Laptop with wireless problem. (3)
Internet Lost (3)
Dept. of HS: NSA 'Helped' Develop Vis.. (16)
windows vista security holes (10)
Point and Shoot Camera Suggestions. (4)
Multiple Restarts Required at Boot (2)
Ideal cheap graph card for PC-Gaming? (18)
radeon x850xt platinum & shader 3 (6)
Graphics Card Upgrade Question (4)
For Sale BFG GTX285 OC2 with 10 year .. (3)
How to convert MP3's (4)
Wireless Televisions. (12)
Hp Artist Edition + Matching Bag (0)
Asus P4G8X Mobo (6)
Xbox 360 GTA: SA disk error (1)
Is the PSU I received dead? (12)
windows 7 internet problem (5)
BSOD On Startup (ntoskrnl.exe) (2)


All times are GMT -4. The time now is 02:59 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28