+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 13

Thread: Any reason I cannot get correct math?

  1. #1
    Newbie Subgenius is an unknown quantity at this point
    Join Date
    Nov 2008
    Age
    28
    Posts
    7

    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
    Last edited by WingedPanther; 11-14-2008 at 07:41 PM. Reason: add code tags (the # button)

  2. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,668
    Blog Entries
    57

    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
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  3. #3
    Newbie Subgenius is an unknown quantity at this point
    Join Date
    Nov 2008
    Age
    28
    Posts
    7

    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

  4. #4
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,668
    Blog Entries
    57

    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
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #5
    Newbie Subgenius is an unknown quantity at this point
    Join Date
    Nov 2008
    Age
    28
    Posts
    7

    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

  6. #6
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,668
    Blog Entries
    57

    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
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #7
    Newbie Subgenius is an unknown quantity at this point
    Join Date
    Nov 2008
    Age
    28
    Posts
    7

    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

  8. #8
    Newbie Subgenius is an unknown quantity at this point
    Join Date
    Nov 2008
    Age
    28
    Posts
    7

    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

  9. #9
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,668
    Blog Entries
    57

    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
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  10. #10
    Newbie Subgenius is an unknown quantity at this point
    Join Date
    Nov 2008
    Age
    28
    Posts
    7

    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

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Simple Math
    By Egz0N in forum The Lounge
    Replies: 39
    Last Post: 11-01-2008, 09:19 AM
  2. Help! Rlink32 Error
    By Sparky in forum Pascal/Delphi
    Replies: 6
    Last Post: 10-16-2008, 10:31 AM
  3. Is my recursion correct?
    By Gear2d in forum C and C++
    Replies: 4
    Last Post: 09-19-2008, 01:56 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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