Connect with Facebook Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Visual Basic Programming

Visual Basic Programming Discussion forum for Visual Basic, an event driven programming language and associated development environment from Microsoft for its COM programming model.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-14-2008, 07:36 PM
Newbie
 
Join Date: Nov 2008
Age: 27
Posts: 7
Rep Power: 0
Subgenius is an unknown quantity at this point
Question Any reason I cannot get correct math?

VB6: Forex Margin Calculator
oubles:
Creating a math prog that is trying to change the value of a text box.
Balance = 19.00
Leverage = 50 (INTEGER)
Lots = 1.0

If Balance = 19 then a for statement must decrease Lots until Lots x Leverage equals Balance. What is happening is for statement is decreasing Lots by 0.01 and Lot of 1.0 should reach .38 and change text box value to .38 and exit sub. What is happening is when lots equals .43 its saying .42222222223.
Code:
Dim Balance, Lots, Margin As Double
Dim Leverage As Integer
Const Mini = 50 '50.00
Const Standard = 100 '100.00

Private Sub cmdCalculate1_Click()
Dim i As Integer
'--- Memory ---
Lots = txtLots1.Text
Balance = txtBal1.Text
If optMini1.Value = True Then Leverage = 50
If optStandard1.Value = True Then Leverage = 100
'--- FindLots ---
If chkFindLots.Value = 1 Then
Dim Fbal As Integer: Fbal = Balance
For i = 1 To 100
    If Lots * Leverage = Fbal Then
        txtLots1.Text = Lots
        Exit Sub
    End If
    Lots = Lots - 0.01
Next i
End If
'If Balance < Leverage Then 'Margins
'    MsgBox "Leverage of $" & Leverage & ".00 is not met": Exit Sub
'Else
'    Margins = Balance / Leverage: lblMargins.Caption = Margins
'End If
End Sub

Private Sub Form_Load()
'Reclaim memory
Lots = 0: ExtLots = 0: Leeway = 0: Balance = 0
'txtBal1 = "": txtLots1 = "": lblLeeway = "": lblMargins = ""
End Sub
email Subgenius at inbox dot com Subject "VB6" .

Brian
Attached Files
File Type: zip Margin.zip (96.6 KB, 4 views)

Last edited by WingedPanther; 11-14-2008 at 07:41 PM.. Reason: add code tags (the # button)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 11-14-2008, 07:45 PM
WingedPanther's Avatar   
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 8,079
Blog Entries: 48
Rep Power: 20
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default Re: Any reason I cannot get correct math?

Have you run your program in debug mode to watch the values?
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 11-14-2008, 07:58 PM
Newbie
 
Join Date: Nov 2008
Age: 27
Posts: 7
Rep Power: 0
Subgenius is an unknown quantity at this point
Default Re: Any reason I cannot get correct math?

I have to say that I am in debug mode and seeing where it is not doing subtraction right when subtracting 0.01 from 1.0 where once it should see .38 the if is interupted and closed.

Brian
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 11-14-2008, 09:29 PM
WingedPanther's Avatar   
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 8,079
Blog Entries: 48
Rep Power: 20
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default Re: Any reason I cannot get correct math?

Using a For loop probably isn't the best plan, there. I'm not familiar with VB, but I would expect the exit Sub to be a little too extreme.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 11-14-2008, 09:48 PM
Newbie
 
Join Date: Nov 2008
Age: 27
Posts: 7
Rep Power: 0
Subgenius is an unknown quantity at this point
Default Re: Any reason I cannot get correct math?

Hi again.
I have tried to put the calculation into memory to have within the IF statement a simple comparison of values as it counts down, but that too didnt work. If im mistaken it seems that the if statement is comprimized by having it do the math but what is happening is it gets funky and trips up alot by not showing 43 when it should have, though testing it again it has gotten to 43 but,, Main problem is for this equation once it reached the .38 it should equal the balance text box value and change the text box for lots, weird. I just thought I would post the pictures to show everyone! Ill keep playing around with it, but I thought a for statement would be ideal to do the equation on this.

Brian
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 11-14-2008, 10:11 PM
WingedPanther's Avatar   
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 8,079
Blog Entries: 48
Rep Power: 20
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default Re: Any reason I cannot get correct math?

You may want to post the original assignment. The logic just looks weird, and I suspect it isn't right for what you're supposed to do.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 11-14-2008, 10:26 PM
Newbie
 
Join Date: Nov 2008
Age: 27
Posts: 7
Rep Power: 0
Subgenius is an unknown quantity at this point
Default Re: Any reason I cannot get correct math?

This is trying to find margin from balance -between two margins 50 or 100.
If the balance is 19.00 then it does not meet the margin level of 50($50).
The way that it fits in is by lots. Lots is 1.0 ($1.00).
I want to subtrace one cent at a time, lot size x 50(or 100) = balance (then update text box with this lot) hope this is easy to understand!

Find the Lot size for $19.00 in the 50 margin
lot size x 50 = "balance"
= .38 x 50 = 19.00
This means that it found what the lot should be, .38 cents!

1.00 = 50.00
.50 = 25.00
.38 = 19.00 <----
.37 = 18.50
.36 = 18.00
.25 = 12.50
.24 = 12.00
.23 = 11.50
.22 = 11.00
.21 = 10.50
.20 = 10.00
.19 = 9.50

Brian
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 11-14-2008, 10:56 PM
Newbie
 
Join Date: Nov 2008
Age: 27
Posts: 7
Rep Power: 0
Subgenius is an unknown quantity at this point
Default Re: Any reason I cannot get correct math?

i cannot understand though how at .43 it shows it as .4299999999999999.
All I can say is I wasnt anticipating this but rather the format of the balance for comparison in the if statement.

Brian
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 11-15-2008, 12:16 AM
WingedPanther's Avatar   
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 8,079
Blog Entries: 48
Rep Power: 20
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default Re: Any reason I cannot get correct math?

You will need to use a format to get it to display properly. Because binary and decimal are different representations, binary can't always do the EXACT representation we want. The result is that it will decode slightly off, such as what you listed above. The solution is to apply a format before assigning the value to text.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 11-15-2008, 12:31 AM
Newbie
 
Join Date: Nov 2008
Age: 27
Posts: 7
Rep Power: 0
Subgenius is an unknown quantity at this point
Unhappy Re: Any reason I cannot get correct math?

But, -I didnt think that using the balance as a integer if thats where you mean it is binary would effect anything other than having a decimal to compare to a non decimal which is why I created a memory of balance into an integer so that it would take the balance compared to the leverage. I just cant get that .43 to incriment into the equation and .42999999999 keeps showing.
I cant think of anything else to do right now but what I will end up having to do is write a case sentence or even a page long if statements just to have what I need written in stone, I dont really mind but I didnt anticipate this happening where it simply deincriments by a factor of 1 cent until the desired outcome works into the equation.

Brian
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Math Egz0N The Lounge 39 11-01-2008 09:19 AM
Help! Rlink32 Error Sparky Pascal/Delphi 6 10-16-2008 10:31 AM
Is my recursion correct? Gear2d C and C++ 4 09-19-2008 01:56 PM


All times are GMT -5. The time now is 08:17 PM.

Freelance Jobs

XML/XSL: Need code for Book with Chapers using XML
Create an XML file for a book of your creation, and a basic CSS file that will format it to display ...
Earn: $40.00


C++/C: Simple firework cue sequencer
What I require is a rework of a simple cue sequencer. I have a piece of hardware (an Arduino boar...
Earn: $50.00


HTML/XHTML: Menu Rework - ASCIIBin
I'm placing this in the HTML/XHTML section of the Freelance site but you are not limited to HTML. Wh...
Earn: $20.00



CodeCall Goal

Goal #1: 1,000 Blogs
Goal #2: 1,000 Wiki Pages
Goal #3: 300,000 Posts
Goal #4: 20,000 Threads
Done: 30%, 23%, 55%, 75%

Ads