View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Dana DeLouis Dana DeLouis is offline
external usenet poster
 
Posts: 947
Default Precision in Excle VBA

If you would like, perhaps something along this line...

Do While TError = 0.3
TAssume = (TAssume + TCalc) / 2
TError = Abs(TAssume - TCalc)
Debug.Print TError
Loop

We see that you variable TAssume is moved closer and closer to TCalc
until your error is <0.3.
We might be able to solve for this recurrence relationship without
looping. I was thinking something like this:
We first calculate how many loops it would take to get the error below 0.3.
Then we solve for the actual value.

Sub Demo2()
Dim TAssume
Dim TCalc
Dim n

TAssume = 13
TCalc = 2

If Abs(TAssume - TCalc) = 0.3 Then
n = Log(20 * Abs(TAssume - TCalc) / 3) / Log(2)
n = WorksheetFunction.Ceiling(n, 1)
TAssume = ((2 ^ n - 2) * TCalc + 2 * TAssume) / 2 ^ n
End If
Debug.Print TAssume
End Sub

I hope I got that correct! :0

Dana DeLouis


Dana DeLouis wrote:
What kind of initial values do you have?

Sub Demo()
Dim TAssume
Dim TCalc
Dim TError

TAssume = 45
TCalc = 2

TError = Abs(TAssume - TCalc)
Do While TError = 0.3
TAssume = TAssume + 0.5 * (TCalc - TAssume)
TError = Abs(TAssume - TCalc)
Loop
Debug.Print TAssume
End Sub