+ Reply to Thread
Results 1 to 10 of 10

INT function returns unexpected results

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

    INT function returns unexpected results

    In trying to answer another user's question, I created a prototype workbook using this formula

    =INT((C1-INT(C1))*10)

    where C1 contains an arbitrary number. (See attachment.) This formula is intended to return the first digit after the decimal point as an integer. And it works, except when C1 contains numbers from 8.2 to 31.2 (inclusive) with only a 2 after the decimal point.

    I broke the formula down into multiple formula steps. The error occurs in the final step when applying INT to the integer 2, returning 1. But this only happens in the cases just described. It seems that INT does not see every 2 as the same number.

    This may be some obscure binary precision issue, but if you take 0.2, multiply it by 10, then Excel shows it as an integer 2, I would think you are dealing with an exact representation of the integer 2. And the problem only occurs in some cases with a decimal portion of 0.2, but not others.

    Anybody ever see anything like this?
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: INT function returns unexpected results

    Floating-point precision problem.

    8.2 - 8 = 1.99999.... = 3FC9999999999980

    0.2 = 3FC999999999999A

    You can round after the subtraction, or extract the result from the formatted string.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert darkyam's Avatar
    Join Date
    03-05-2008
    Location
    Houston, TX
    MS-Off Ver
    2013
    Posts
    2,191

    Re: INT function returns unexpected results

    Another option is to just use text functions, such as
    =MID(C1,FIND(".",C1)+1,1)+0

  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: INT function returns unexpected results

    Quote Originally Posted by shg View Post
    Floating-point precision problem.

    8.2 - 8 = 1.99999.... = 3FC9999999999980

    0.2 = 3FC999999999999A

    You can round after the subtraction, or extract the result from the formatted string.
    I suspected something like that. I actually used to teach stuff like this (model numbers and safe numbers) but that was 20 years ago and haven't thought about it for a while.

    This question is more an exploration of internals than an immediate pragmatic issue, so I hope my persistence isn't annoying. I understand how to work around this in specific cases, but it gives me pause to think that I have to carefully consider every future use of INT.

    It does raise one question:

    Why does it work correctly for 2.2, 3.2, 4.2, 5.2, 6.2, 7.2 then fail at 8.2? And then start working again at 32.2, and apparently for all higher numbers?

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: INT function returns unexpected results

    Because 32.2 - 32 = 0.200000000000003, which errs beneficially for this.

  6. #6
    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: INT function returns unexpected results

    Quote Originally Posted by darkyam View Post
    Another option is to just use text functions, such as
    =MID(C1,FIND(".",C1)+1,1)+0
    I advised against that because it returns #VALUE! if the number is an integer....

  7. #7
    Forum Expert darkyam's Avatar
    Join Date
    03-05-2008
    Location
    Houston, TX
    MS-Off Ver
    2013
    Posts
    2,191

    Re: INT function returns unexpected results

    6String, I tested it through 16384, and the error happens more often than you'd think. For numbers between 2^3 and 2^5, 2^7 and 2^9, 2^11 and 2^13, there are errors. I'd suspect the pattern continues, but I'm not sure why it's there.

    And there could always be an error checker in the formula. =If(ISERROR(FIND(".",C1)),0,<current formula>). Shg's way is simpler, using a rounddown instead of INT. I was just throwing it out there.

  8. #8
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: INT function returns unexpected results

    Quote Originally Posted by shg
    8.2 - 8 = 1.99999....
    Actually I read in a math book recently that 1.99999... and 2 are considered to be the same number by mathematicians because by definition if there are no values between two represented numbers on a number line, then the two numbers are equivalent. Yes, I know that Excel only takes it out to 15 digits, just trying to add some trivia.
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: INT function returns unexpected results

    Quote Originally Posted by 6SJ
    Quote Originally Posted by darkyam
    Another option is to just use text functions, such as
    =MID(C1,FIND(".",C1)+1,1)+0
    I advised against that because it returns #VALUE! if the number is an integer
    So wrap a TEXT function around C1.

    In this case, processing the result of the conversion to decimal is exactly what you want.

  10. #10
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: INT function returns unexpected results

    just for info
    put 1.2 in a1
    2.2 in a2
    in b1 put a1-row()
    and in b2 =a2-row()
    select a1:b2 drag down formatted as number as many places as you want you'll see where it changes
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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