+ Reply to Thread
Results 1 to 5 of 5

Error with > operator (0.2 > 0.2??)

  1. #1
    Registered User
    Join Date
    01-18-2014
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    3

    Error with > operator (0.2 > 0.2??)

    Can anyone explain the following behavior?


    If (4/20) > (1-0.8) then
    'VBA executes this code based on 0.2 (Variant/Double) > 0.2 (Variant/Double)
    End If

    Try it for yourself...

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Error with > operator (0.2 > 0.2??)

    Floating point arithmetic done by a computer is complicated. As you know, computers store numbers in binary format. A real number is stored as a sign, base and exponent. As you learned as a youngster, in base 10 arithmetic, some numbers cannot be represented exactly in base 10 digits. For example, 1/3 gives a repeating decimal of

    0.3...

    You have the same situation with binary numbers. The value 4/20 is exactly 0.2 in base 10, but gives the repeating binary

    0.0011...

    I suspect that the answers to 4/20 and 1-0.8, although they yield the same number when done in base 10, give two slightly different answers in the least significant binary digit when rounded for storage on a computer, due to how they are computed. Representing the constant 0.8 then subtracting it from 1 is a completely different machine operation than a division.

    I have only had to actually worry about this twice, once when I was writing software for NASA to acquire satellite signals, and the other when I was teaching Ada which has a rigorous specification for machine representation of numbers. Both were decades ago so I'm a little rusty.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    01-18-2014
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    3

    Re: Error with > operator (0.2 > 0.2??)

    I'm sure you're exactly right, 6StringJazzer. What bothers me is that the VBA IDE shows both of these values as "0.2". If the outcome of these calculations are different, why isn't one of these values something like 0.2000000...1 or 0.199999...999 to the limit of Double precision?

    In any case, problem solved by applying Round() to both sides of the comparison.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Error with > operator (0.2 > 0.2??)

    Glad you resolved your issue, that is the textbook method.

    Why does it show both values as 0.2 but doesn't treat them that way internally? This paper may give some insight (small PDF file).

  5. #5
    Registered User
    Join Date
    01-18-2014
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    3

    Re: Error with > operator (0.2 > 0.2??)

    It does at that... thank you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 07-25-2012, 09:32 AM
  2. [SOLVED] Error with IF and AND operator
    By rehan0129 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-12-2012, 03:41 PM
  3. missing operator error
    By Jollyfrog in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-28-2010, 09:13 AM
  4. Spreasheet is vulnerable to operator error
    By GeorgeTriton in forum Excel General
    Replies: 3
    Last Post: 05-23-2007, 01:40 PM
  5. Replies: 6
    Last Post: 01-17-2006, 08:45 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1