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.
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
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))
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?
If someone can explain this it would be wonderful!!
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
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
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
Code:
continue:
If (totFwds(i) = 0 Or exps(i, 1) <= T) Then
mu(i) = 0
i = i + 1
GoTo continue
End If
T will be equal to 1 but so will exps(i,1) but the if will not trigger?
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").
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
So, in the case above, you don't test for "equal T" but instead for "extremly close to T"
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
Last edited by RaytracerFFM; Jun 9th, 2010 at 10:38 AM.
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
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
Last edited by RaytracerFFM; Jun 12th, 2010 at 12:50 PM.
Reason: Added supplemental example