Results 1 to 9 of 9

Thread: Hmmm...VB being funny?

  1. #1

    Thread Starter
    Addicted Member Martin Wilson's Avatar
    Join Date
    Mar 2002
    Location
    :)
    Posts
    236

    Hmmm...VB being funny?

    VB Code:
    1. For i = 0.2 To 0.3 Step 0.01
    2.         If i = 0.25 Then
    3.             Form1.Print "0.25 - Yay! :)"
    4.         Else
    5.             Form1.Print i
    6.         End If
    7.     Next i
    Does this work for you? I know it is probably just a problem with type comparison but it still seems a little stupid.
    What is the answer to this question?

  2. #2
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    What do you mean by it acting funny? All I saw was that it never got to .25 and therefore didn't print it...
    <removed by admin>

  3. #3
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    It works this way:

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim i As Single
    3.    
    4.     For i = 0.2 To 0.3 Step 0.01
    5.         If Val(i) = 0.25 Then
    6.             Debug.Print "0.25 - Yay! "
    7.         Else
    8.             Debug.Print i
    9.         End If
    10.     Next i
    11. End Sub
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  4. #4
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Try this with the Val() function:
    VB Code:
    1. For i = 0.2 To 0.3 Step 0.01
    2.   If Val(i) = 0.25 Then
    3.     Form1.Print "0.25 - Yay! :)"
    4.   Else
    5.     Form1.Print i
    6.   End If
    7. Next

    [edit]
    Dammit... a day late and a dollar short...
    <removed by admin>

  5. #5
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    Originally posted by MidgetsBro
    Try this with the Val() function:
    VB Code:
    1. For i = 0.2 To 0.3 Step 0.01
    2.   If Val(i) = 0.25 Then
    3.     Form1.Print "0.25 - Yay! :)"
    4.   Else
    5.     Form1.Print i
    6.   End If
    7. Next

    [edit]
    Dammit... a day late and a dollar short...
    beat ya'

    Anyway... have a look at this thread Why is this false?
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  6. #6
    New Member
    Join Date
    Jul 2002
    Location
    Indianapolis
    Posts
    11
    Is the loop variable a variant, a double, or a ???

  7. #7
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808
    It doesn't mind much really....
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  8. #8

    Thread Starter
    Addicted Member Martin Wilson's Avatar
    Join Date
    Mar 2002
    Location
    :)
    Posts
    236
    I managed to fix the problem straight away but for someone who is new to VB that could cause a really annoying problem.
    I read the other thread and in this case i is probably something like 0.2500000000000001, indeed, if you increase the 0.3 to 1.0, the code prints 0.810000000000001 instead of 0.81. The print code must require 13 or more 0 before it cuts them off.
    Maybe the = function should work this way as well, or an alternative provided?

    Oh well
    What is the answer to this question?

  9. #9

    Thread Starter
    Addicted Member Martin Wilson's Avatar
    Join Date
    Mar 2002
    Location
    :)
    Posts
    236
    Adding Val() does not fix all the instances of this problem:
    Code:
        Dim i As Double
        For i = 0.1 To 1 Step 0.02
            If Val(i) = 0.94 Then
                Form1.Print "0.94 - Yay! "
            Else
                Form1.Print i
            End If
        Next i
    In this case you need to round() it to 2 decimal places.
    What is the answer to this question?

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