Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Any reason I cannot get correct math?

  1. #1
    Subgenius is offline Newbie
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

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

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    Re: Any reason I cannot get correct math?

    Have you run your program in debug mode to watch the values?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Subgenius is offline Newbie
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    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

  5. #4
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

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

  6. #5
    Subgenius is offline Newbie
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    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

  7. #6
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

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

  8. #7
    Subgenius is offline Newbie
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    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

  9. #8
    Subgenius is offline Newbie
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    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

  10. #9
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

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

  11. #10
    Subgenius is offline Newbie
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    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

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. SEO For a Different Reason
    By Hunter100 in forum Website Design
    Replies: 5
    Last Post: 05-25-2011, 03:50 AM
  2. the reason for errors in this simple read function
    By jackson6612 in forum C and C++
    Replies: 3
    Last Post: 05-13-2011, 02:53 PM
  3. Replies: 31
    Last Post: 01-06-2011, 05:08 PM
  4. INHERITANCE: reason behind that??
    By himanshu in forum Java Help
    Replies: 3
    Last Post: 01-17-2010, 09:15 AM
  5. SnowFlakes floating up, for some reason
    By Logan in forum JavaScript and CSS
    Replies: 10
    Last Post: 01-18-2009, 04:06 AM

Tags for this Thread

Bookmarks

Posting Permissions

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