Hi there,
The following If does not trigger when exps(i,1) =1 and T=1?
Is it because exps(i,1) is of type variant and T is of type double?
BazCode:continue:
If (totFwds(i) = 0 Or exps(i, 1) <= T) Then
mu(i) = 0
i = i + 1
GoTo continue
End If
Printable View
Hi there,
The following If does not trigger when exps(i,1) =1 and T=1?
Is it because exps(i,1) is of type variant and T is of type double?
BazCode:continue:
If (totFwds(i) = 0 Or exps(i, 1) <= T) Then
mu(i) = 0
i = i + 1
GoTo continue
End If
Hi baz,
this may be a fruitless hint, but are you sure your algorithm is numerically stable (especially when dealing with numbers that are *nearly* equal)? There was a big dicussion in a different thread on this board about the subject.
http://www.vbforums.com/showthread.php?t=211054
cheers,
ray
Bazman, there has to be a different reason why the code is not entering the loop...
Try this simple example and you will see that it enters the loop ;)
Code:Sub Sample()
Dim exps(1, 1) As Variant, T As Double
T = 1
If exps(1, 1) <= T Then
MsgBox "Show me"
End If
End Sub
yes your example works fine if I set exps(1,1)=1.
But my example runin the same module still does not work?
Here is how exps and st are defined
dt is used to control the "time" of the simuation it should be 0.1. exps(nfwds,1)=10 nTsteps=100. But there seems to be some rounding error?Code:
Dim exps as double
Dim
exps = Range("exps").Value 'exps is read in from the spreadsheet it assigend to type variant/double
'For i = 1 To nfwds + 1 'here I tried using a for to assign exps to
'exps(i, 1) = i 'variant/interger the assignment worked but the loop
'Next i ' still does not work
dt = 1 / (nTsteps \ exps(nfwds, 1))
If someone can explain this it would be wonderful!!
This also works for me....
Code:Sub Sample()
Dim exps As Variant, T As Double
T = 1
'exps = Range("A1:A3").Value
exps = Range("exps").Value
For i = 1 To UBound(exps)
If exps(i, 1) <= T Then
MsgBox "Show me"
End If
Next
End Sub
Thanks for your help, but the problem remains.
This is really bizarre?
I am attaching the whole file.
To replicate the error, just go to sub SAB/LMM putting a stop at the line: Next T.
Run though till you are stopped at T=0.9 then step through, T will change to 1 and almost immediately you will be at the control loop where the problem is
T will be equal to 1 but so will exps(i,1) but the if will not trigger?Code:continue:
If (totFwds(i) = 0 Or exps(i, 1) <= T) Then
mu(i) = 0
i = i + 1
GoTo continue
End If
Your file is in a mess and the code is haphazardly arranged... I will have to arrange it first to understand what exactly are you trying to do :)
I will post back once I go through it...
If you have any questions please do not hesitate to ask
Guys,
this is the behavior i meant with my first reply and reference to the other thread.
Try this modification and your If-statement should react as expected. It does for me in the situation you referred to where T = 1. (T is actually the trouble-maker here; T IS NOT really "1". It's something very close to "1").
So, in the case above, you don't test for "equal T" but instead for "extremly close to T"Code:continue:
'//put if here to test for fi=0 of fi expired
'//change correls to roll with expiry
If (totFwds(i) = 0 Or _
exps(i, 1) < T Or _
(Abs(exps(i, 1) - T)) < 0.0000001) Then
mu(i) = 0
i = i + 1
GoTo continue
End If
It's all about precision and binary representation of (floating-point) numbers.
That said, NEVER test for "equal" when working with floating point precision. Most likely it will fail.
cheers,
ray
That works!!
Thank you!!!!!!!!!!!!!!!!!!!
Still think the problem is wierd though, and I thought working with computers would be precise as can be?
Glad i could help :)
Don't want to go too far on this. But it's a well known effect. Read the thread i mentioned in my first reply if you like. That guy "phinds" was completely right in his argumentation.
Computers are finite machines with finite precision and the binary projection (in which ALL computers work) of the decimal number 0.1 is 0.00011001100110011... to infinity. You can try this on wolframalpha.com:
convert 0.1 base 10 to base 2
And since computers have only finite precision, they have to truncate this number at some point sooner or later. And when you convert that back to decimal you'll get something like 0.099999999999 which when outputted by VB looks like 0.1 but it just isn't!
This all falls into the field of Numeric Math and is quite fundamental.
And, yes, your code could use some structure ;) for the sake of readability and later maintainance :D
Please mark this thread as RESOLVED
cheers,
ray
As a supplemental example (staying in decimal): Is 3 * 1/3 equal to 1?
3/3 is!
But 3 * 0.333333333... to infinity will never be. It's imposible! At least when your dealing with finite precision ;)
how do I mark as resolved
Go to the top of the thread. There you'll find "Thread tools". The last entry in the list is "Mark Thread Resolved".
cheers,
ray
OK thanks