Results 1 to 19 of 19

Thread: [RESOLVED] Whaaaat?! Int(x) doing something weird.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    448

    Resolved [RESOLVED] Whaaaat?! Int(x) doing something weird.

    Can anyone explain this? These values were taken from my Immediate window, and reflect what my program is doing ((ue) is a Double data type):

    Code:
    ?ue
     2 
    ?str(ue)
     2
    ?int(ue)
     1
    Whaaaaat?!

  2. #2
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    461

    Re: Whaaaat?! Int(x) doing something weird.

    This misbehavior is probably a bug related to newer Windows version(s), as they're rare but known (because Microsoft tends from time to time to break something). Which one you're using?

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Whaaaat?! Int(x) doing something weird.

    what value is actually assigned or calculated into ue?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Whaaaat?! Int(x) doing something weird.

    Quote Originally Posted by MikiSoft View Post
    This misbehavior is probably a bug related to newer Windows version(s), as they're rare but known (because Microsoft tends from time to time to break something). Which one you're using?
    Seems very unlikely.


    Since we haven't seen the code for all we know you have overridden VBA.Int() with your own Int() function in the current namespace.

  5. #5
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: Whaaaat?! Int(x) doing something weird.

    here is the contents of my debug window
    Code:
    ?1+1
     123 
    ?2+2
     234 
    ?"treddie is a joker"
    well,if you say so
    Whaaaaaaat ?????
    do not put off till tomorrow what you can put off forever

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    448

    Re: Whaaaat?! Int(x) doing something weird.

    System is Windows 7 Ultimate, 6.1.7600 Build 7600

    The equation to get value (ue) is:
    Code:
    ue = (bx / vx)
    It then goes into:
    Code:
    qx = (Int(ue)) + 1
    All variables are declared as Double.

    This problem is intermittent...Multiple passes come through this same code and the result is unpredictable. I thought maybe it has to do with vb showing ue = 2, when maybe it is actually extremely close to "2", as in "1.999999...9". Seems like I have seen this before, but it is rare. Now, in this program, it is happening a lot.

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Whaaaat?! Int(x) doing something weird.

    That was my assumption from the start.
    Your current program must just be calculating things that are nearly a whole number, but not quite.
    This is to be expected when using floating point numbers.
    Code:
    'a is declared as a Double.
    
    'in the immediate window
    
    a = 1.999999999999995
    ?a
     1.99999999999999 
    
    a = 1.999999999999996
    ?a
     2 
    ?int(a)
     1

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    448

    Re: Whaaaat?! Int(x) doing something weird.

    Quote Originally Posted by passel View Post
    That was my assumption from the start.
    Your current program must just be calculating things that are nearly a whole number, but not quite.
    This is to be expected when using floating point numbers.
    Code:
    'a is declared as a Double.
    
    'in the immediate window
    
    a = 1.999999999999995
    ?a
     1.99999999999999 
    
    a = 1.999999999999996
    ?a
     2 
    ?int(a)
     1
    Passel...I think your test pretty much sums it up. The bummer is that Microsoft would let that ambiguity slip through. So vb is is displaying a rounded version of what it is actually using internally? Why not show the frickin float in its entirety?! That sounds like a vb bug.

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Whaaaat?! Int(x) doing something weird.

    Blame Intel...

    Problem is that floats are by their very nature imprecise because digital computers cna't handle them well enough through binary representation... So what you're seeing is the computer's best guess at how it can store the number. It's the Pentium Approximation Syndrome.

    And yet it's really only a problem depending on your scale.... further out it goes, the worse it gets... and then when you hit a repeating decimal like 2/3 ... it has to stop after some point, it can't keep recording decimal places infinitely.

    There's a couple of sayings:
    2+2=5 for extremely large values of 2


    We are Pentium of Intel, rounding is futile, you will be approximated.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    448

    Re: Whaaaat?! Int(x) doing something weird.

    Lol!
    I refuse to be approximated. "I am a man! Not a number!" -quote by #6, I believe.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    448

    Re: Whaaaat?! Int(x) doing something weird.

    Well, it ain't necessarily elegant, but I wrote this fault-tolerant Int() function, to address this vb issue:

    Code:
      'Fault tolerant Int():
      
      Dim ResultFlag As Byte
      Dim TempLoop As Integer
      Dim ue_str As String
      Dim CharStr As String
      Dim ue_int As Integer
      
      ue_str = Str(ue)
      
      ResultFlag = 0
      
      For TempLoop = 1 To Len(ue_str)
        CharStr = Mid(ue_str, TempLoop, 1)
        
        If CharStr = "." Then    'Life is good...True float value is intact.
          ResultFlag = 1
          GoTo jumpover
        End If
    
      Next TempLoop
      
    jumpover:
    
      If ResultFlag = 0 Then  'True float value may be compromized.
        ue_int = Val(ue_str)
      ElseIf ResultFlag = 1 Then  'True float value is intact.
        ue_int = Int(ue)
      End If

  12. #12
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Whaaaat?! Int(x) doing something weird.

    Use Exit For instead of Goto jumpover and remove the label jumpover:


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    448

    Re: Whaaaat?! Int(x) doing something weird.

    Good point. Thanks!

  14. #14
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: Whaaaat?! Int(x) doing something weird.

    or the entire loop can be dismissed:
    Code:
      If InStr(ue_str, ".") Then
         'contains "."
      Else
         'does not contain "."
      End If
    wether it is fault tolerant or not, i dare not say
    after all, you suppose a locale specific decimal separator (.)
    where does that double come from ?
    from a textbox filled in by a user ?
    do not put off till tomorrow what you can put off forever

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    448

    Re: Whaaaat?! Int(x) doing something weird.

    or the entire loop can be dismissed:
    Great point. Much better solution.

    wether it is fault tolerant or not, i dare not say
    after all, you suppose a locale specific decimal separator (.)
    where does that double come from ?
    from a textbox filled in by a user ?
    Either from a text file that gets numerical data from an Adobe Illustrator file, or from textboxes that are protected from human errors by a checker function.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    448

    Re: Whaaaat?! Int(x) doing something weird.

    Hm...This seems to be much better. Forget everything else, just use:
    Code:
    xx = Int(Val(Str(x)))
    For more user control over where the accuracy "trigger" point should lie:
    Code:
    xx = Int(Round(Val(Str(x)),r))
    where (r) is the desired decimal place to round at.

    Without rounding, it appears that the trigger decimal place is different for numbers that have more digits in the integer portion of the float.
    Last edited by treddie; Mar 6th, 2016 at 06:29 PM.

  17. #17
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,454

    Re: Whaaaat?! Int(x) doing something weird.

    Quote Originally Posted by treddie View Post
    Hm...This seems to be much better. Forget everything else, just use:
    Code:
    xx = Int(Val(Str(x)))
    For more user control over where the accuracy "trigger" point should lie:
    Code:
    xx = Int(Round(Val(Str(x)),r))
    Not really good advice here...

    The line:
    Code:
      xx = Int(Round(Val(Str(x)),r))
    is truly horrible ... why not use Round directly on x?
    Code:
      xx = Round(x)
    or in case x is within Int32-Range:
    Code:
      xx = CLng(x) 'CLng() does rounding as well
    or if xx is of type Long then implicit type-conversion might suffice as well:
    Code:
      xx = x 'also the implicit conversions (in case xx is an Integer-Type) do a rounding
    Olaf

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    448

    Re: Whaaaat?! Int(x) doing something weird.

    is truly horrible ... why not use Round directly on x?
    True. I just added that as a mod to the original line. Personally, I don't think I would have any use for the second version, since the whole point is to leave the number alone at however many decimal places it contains (keep it untouched), unless it reaches the point of ambiguity where the result of Int (x) becomes unreliable.

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    448

    Re: Whaaaat?! Int(x) doing something weird.

    Thanks everyone for your help. I am marking this thread as resolved.

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