|
-
Feb 25th, 2008, 11:10 AM
#1
Thread Starter
New Member
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
-
Feb 25th, 2008, 12:42 PM
#2
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
-
Feb 25th, 2008, 05:39 PM
#3
Thread Starter
New Member
Re: Formula Problem
One thing wrong with that is at 90,000 it overflows a...
-
Feb 25th, 2008, 07:03 PM
#4
Re: Formula Problem
Well that's not surprising since, if I interpret the formula correctly, 2 ^ (x / 7) would be 2 ^ 12857 which is a very large number.
-
Feb 25th, 2008, 07:27 PM
#5
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 ????
-
Feb 25th, 2008, 08:05 PM
#6
Thread Starter
New Member
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.
-
Feb 25th, 2008, 08:20 PM
#7
Re: Formula Problem
what does x and L represent?
-
Feb 26th, 2008, 06:51 AM
#8
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.
-
Feb 26th, 2008, 05:47 PM
#9
Thread Starter
New Member
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.
-
Feb 26th, 2008, 11:14 PM
#10
Re: Formula Problem
Please see edited code above.
-
Feb 27th, 2008, 01:22 PM
#11
Re: Formula Problem
 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.
-
Feb 27th, 2008, 04:30 PM
#12
Re: Formula Problem
 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.
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|