+ Reply to Thread
Results 1 to 4 of 4

INT and TRUNC functions don't seem to work correctly inside IF

  1. #1
    Registered User
    Join Date
    02-19-2018
    Location
    Sydney, Australia
    MS-Off Ver
    365
    Posts
    2

    INT and TRUNC functions don't seem to work correctly inside IF

    Hi,

    I am trying to construct a spreadsheet that calculates what notes/coin to give as change.
    Under a specific set of calculations, even though both INT and TRUNC functions return a "1" result, when nested into an IF function they return a zero.
    In the last entry line of the attached workbook, everything is fine until you enter $500 as the amount tendered. The formula in cell O4 then falls over and I get a zero result, even though the nested TRUNC function has a result of 1.
    What am I doing wrong?
    Regards,
    Gary
    Attached Files Attached Files

  2. #2
    Forum Expert kersplash's Avatar
    Join Date
    11-22-2016
    Location
    Perth
    MS-Off Ver
    Home 2016 (Windows 10)/Work 2013 Pro Plus (Windows 10)
    Posts
    2,012

    Re: INT and TRUNC functions don't seem to work correctly inside IF

    Try using Formulas -> Evaluate Formula. This shows that the result of the TRUNC(76.25-76.2) is actually 0.04999999999999972 not 0.05 as you probably expect.
    https://blogs.office.com/en-us/2008/...wrong-answers/
    Last edited by kersplash; 02-19-2018 at 03:12 AM.

  3. #3
    Registered User
    Join Date
    02-19-2018
    Location
    Sydney, Australia
    MS-Off Ver
    365
    Posts
    2

    Re: INT and TRUNC functions don't seem to work correctly inside IF

    Thank you kersplash.
    I wasn't aware of that floating point precision problem - thanks for directing me to that blog.
    I have used the ROUND function inside the TRUNC, and all is sweet.
    You've saved my sanity!
    Gary

  4. #4
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: INT and TRUNC functions don't seem to work correctly inside IF

    Quote Originally Posted by OzGary View Post
    I wasn't aware of that floating point precision problem - thanks for directing me to that blog
    .... Which is wrong in detail, as is common for these explanantions even from MSFT.

    In particular, it is not true that "Excel store[s] 15 significant digits of precision".

    However, it is true that Excel displays (formats) only up to 15 significant digits (rounded).

    And some for operations (including TRUNC and INT), Excel converts the exact internal value (binary) to its 15-significant-digit representaton first.

    For example, =76.25-76.2=0.0499999999999972 returns TRUE, but =76.25-76.2-0.0499999999999972=0 returns FALSE(!).

    The reason is: the "=" operator compares values rounded to 15 significant digits, but 76.25-76.2-0.0499999999999972 in that context is evaluated to its exact binary value, about -4.16E-17.

    And that difference demonstrates that Excel does not "store" only 15 significant digits.

    Ironically, in this case, =76.25-76.2-0.0499999999999972 returns exactly zero (0.00E+00 when formatted as Scientific) because Excel sometimes and inconsistently arbitrarily replaces the exact arithmetic result with zero under very specific conditions.

    In contrast, =76.25-76.2-0.0499999999999972-0 and =(76.25-76.2-0.0499999999999972) return about -4.16E-17 because they do not meet the specific conditions for the arbitrary substitution.

    Not for nothing, just when you thought your "sanity" was saved. (wink)

    But explicit rounding is indeed the best method for avoiding these anomalies (except for a defect in Excel (sigh), which appears very rarely). And round to the precision that you expect a calculation to be accurate to, not an arbitrary number of digits like 10, as some people suggest.
    Last edited by joeu2004; 02-19-2018 at 11:59 AM. Reason: minor

+ 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. [SOLVED] Can ROW() and COLUMN() be used inside other functions?
    By FoxWoodworking in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-24-2013, 08:58 AM
  2. Functions inside Solver algo
    By Zward25 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-26-2009, 03:40 PM
  3. Left and Mid functions inside subtotal.
    By klund in forum Excel General
    Replies: 12
    Last Post: 05-05-2009, 05:16 PM
  4. Functions inside a header
    By whftherb in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 05-13-2007, 08:31 AM
  5. udf inside excel functions
    By snowhite2007 in forum Excel General
    Replies: 0
    Last Post: 05-10-2007, 04:09 PM
  6. MIN and MAX functions inside IF argument?
    By jim7485 in forum Excel General
    Replies: 2
    Last Post: 09-03-2006, 10:25 PM
  7. Calculting SUM of cells with IF functions inside
    By Mihca in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-23-2005, 03:05 PM

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