Results 1 to 12 of 12

Thread: Formula Problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    4

    Formula Problem

    I was making a program to loop a certain formula until the two values are equal. So I made it a do while loop and the final x destination increases by 1 every failed attempt.I thought this in essence would cause it to stop at the exact time that a has a chance to be not less than c.But what it does is it keeps going. at 83 it works, both display 332, but at 174(next lvl) a=1031 and c=696...those two have to equal for my program to function properly...any help please (I made tons of variables for my program not all are used, I usually just make a few extra ahead of time) the code is posted below.


    Code:
    Private Sub Command1_Click()
    Dim a As Long
    Dim b As Long
    Dim c As Long
    Dim d As Single
    Dim e As Long
    Dim f As Long
    Dim x As Single
      
    b = Text1.Text
    c = b * 4
    
    d = 1
    
      
      
      
    Do while e < c
      For x = 1 To d
      a = a + Int(x + 300 * (2 ^ (x / 7)))
    
    e = Fix(a)
    
    d = d + 1
      Next x
    Loop
    Label1.Caption = "a=" & a
    Label2.Caption = "b=" & b
    Label3.Caption = "c=" & c
    Label4.Caption = "d=" & d
    Label5.Caption = "e=" & e
    Label6.Caption = "f=" & f
    Label7.Caption = "x=" & x
    
    End Sub

  2. #2
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Formula Problem

    This modification to your code seems to not lock up:
    Code:
    Private Sub Command1_Click()
    Dim a As Long
    Dim b As Long
    Dim c As Long
    Dim d As Single
    Dim e As Long
    Dim f As Long
    Dim x As Single
      
    b = Text1.Text
    c = b * 4
    
    While e < c
        d = d + 1
        For x = 1 To d
            a = a + Int(x + 300 * (2 ^ (x / 7)))
            e = Fix(a)
            If e < c Then Exit For
        Next
    Wend
    Label1.Caption = "a = " & a
    Label2.Caption = "b = " & b
    Label3.Caption = "c = " & c
    Label4.Caption = "d = " & d
    Label5.Caption = "e = " & e
    Label6.Caption = "f = " & f
    Label7.Caption = "x = " & x
    
    End Sub
    
    Private Sub Form_Load()
    Text1.Text = ""
    End Sub
    Doctor Ed

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    4

    Re: Formula Problem

    One thing wrong with that is at 90,000 it overflows a...

  4. #4

  5. #5
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Formula Problem

    This code never work:
    Code:
    While e < c
        d = d + 1
        For x = 1 To d
            a = a + Int(x + 300 * (2 ^ (x / 7)))
            e = Fix(a)
            If e < c Then Exit For
        Next
    Wend
    The condition of Exit For is seems to be contradic with the condition of While Loop.
    Try this:
    Code:
       Do While e < c
          d = d + 1
          For x = 1 To d
             a = a + Int(x + 300 * (2 ^ (x / 7)))
             e = Fix(a) '<--- e always equals a beacause a is always an integer
             If e >= c Then Exit Do
          Next
       Loop
    I cannot understand what you want to do with the routine. The logic does not make sense to me.

    Are you trying to solve an equation with a given value of "b"?

    Is that you want to solve the equation
    x + 300 * (2 ^ (x / 7)) = 4*b
    with the nearest integer x that makes the LHS >= the RHS ????
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    4

    Re: Formula Problem

    yes i want a to equal b*4...this is the experience formula for a game (RuneScape) this is how they find out how much experience is required to get to the next level. The image of the formula is



    I am trying to reverse the formula, basically instead of finding experience, I want the user to input experience and it displays their level.

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Formula Problem

    what does x and L represent?

  8. #8
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Formula Problem

    Based on the formula image, this is what you can do:
    With a given "experience" e, you can find the "level" L of that "experience".
    e1 is the minimum "experience" required for next level.

    Code:
    Private Sub Command1_Click()
       Dim e As Double, e1 As Double, S As Double, L As Long
       
       e = Val(Text1.Text)
    
       Do While e1 <= e
          L = L + 1
          S = S + Int(L + 300 * 2 ^ (L / 7))
          e1 = Int(S / 4)
       Loop
    
       Label1.Caption = "Experience = " & e
       Label2.Caption = "Level = " & L
       Label3.Caption = "Minimum Experience for Next Level = " & e1
       
    End Sub
    Edit: Code has been simplified.
    Last edited by anhn; Feb 26th, 2008 at 11:12 PM.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    4

    Re: Formula Problem

    That code works well and all but there is only one thing...in RuneScape you reach level 2 when you hit 83 exp...not before...and lvl 3 is at 174...not from 84-174....thats lvl 2...It's not that I don't appreciate you helping me...I'm really glad someone finally helped me, but can you show me how else to write the code that does what I explained? I'm not asking you to "DO" it for me, simply just teach me.

  10. #10
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Formula Problem

    Please see edited code above.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  11. #11
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Formula Problem

    Quote Originally Posted by anhn
    This code never work:
    Code:
    While e < c
        d = d + 1
        For x = 1 To d
            a = a + Int(x + 300 * (2 ^ (x / 7)))
            e = Fix(a)
            If e < c Then Exit For
        Next
    Wend
    The condition of Exit For is seems to be contradic with the condition of While Loop.
    Not true. Once the condition, e < c, is met within the For ... Next loop, then the While ... Wend conditon is also met and allows the exit. No lock up.

    And, if Exit For is not included, the For ... Next loop may not catch the first occurrence of the condition being sought.
    Doctor Ed

  12. #12
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Formula Problem

    Quote Originally Posted by anhn
    This code never work:
    Code:
    While e < c
        d = d + 1
        For x = 1 To d
            a = a + Int(x + 300 * (2 ^ (x / 7)))
            e = Fix(a)
            If e < c Then Exit For
        Next
    Wend
    The condition of Exit For is seems to be contradic with the condition of While Loop.
    Quote Originally Posted by Code Doc
    Not true. Once the condition, e < c, is met within the For ... Next loop, then the While ... Wend conditon is also met and allows the exit. No lock up.

    And, if Exit For is not included, the For ... Next loop may not catch the first occurrence of the condition being sought.
    That's Not True!
    The code will not be locked-up but it will cause overflow when d large enough.

    When e<c is met, the code will jump out of the For...Next loop,
    but that e<c is also met the condition of While..Wend,
    that means it will seat within While...Wend, with each round of While...Wend after that d will increase by 1, the For...Next loop will run again at least one loop, e will increase. When e>=c, the For...Next loop will run till x = d, after that the While...Wend will finish.
    Because d increased, x may be large enough to cause 2^(x/7) or the whole a + Int(x + 300 * (2 ^ (x / 7))) overflow.
    Last edited by anhn; Feb 27th, 2008 at 04:48 PM.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

Posting Permissions

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



Click Here to Expand Forum to Full Width