Results 1 to 14 of 14

Thread: [RESOLVED] If not triggering?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    13

    Resolved [RESOLVED] If not triggering?

    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?

    Code:
    continue:
                If (totFwds(i) = 0 Or exps(i, 1) <= T) Then
                mu(i) = 0
                i = i + 1
                GoTo continue
                End If
    Baz

  2. #2
    New Member RaytracerFFM's Avatar
    Join Date
    Jun 2010
    Location
    Frankfurt, Germany
    Posts
    15

    Re: If not triggering?

    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
    Last edited by RaytracerFFM; Jun 9th, 2010 at 01:45 AM.

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: If not triggering?

    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
    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    13

    Re: If not triggering?

    Quote Originally Posted by koolsid View Post
    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!!

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: If not triggering?

    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
    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

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    13

    Re: If not triggering?

    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

    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?
    Attached Files Attached Files

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: If not triggering?

    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...
    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

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    13

    Re: If not triggering?

    If you have any questions please do not hesitate to ask

  9. #9
    New Member RaytracerFFM's Avatar
    Join Date
    Jun 2010
    Location
    Frankfurt, Germany
    Posts
    15

    Re: If not triggering?

    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").

    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.

  10. #10

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    13

    Re: If not triggering?

    That works!!

    Thank you!!!!!!!!!!!!!!!!!!!

    Still think the problem is wierd though, and I thought working with computers would be precise as can be?

  11. #11
    New Member RaytracerFFM's Avatar
    Join Date
    Jun 2010
    Location
    Frankfurt, Germany
    Posts
    15

    Re: If not triggering?

    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

    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

  12. #12

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    13

    Re: If not triggering?

    how do I mark as resolved

  13. #13
    New Member RaytracerFFM's Avatar
    Join Date
    Jun 2010
    Location
    Frankfurt, Germany
    Posts
    15

    Re: If not triggering?

    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

    If your query has been Solved, please mark it as such in the top menu via Mark Thread Resolved under Thread Tools

    Was a post helpful? Then you might care to rate it by clicking Rate This Post on the lower left hand side of it

  14. #14

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    13

    Re: If not triggering?

    OK thanks

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