+ Reply to Thread
Results 1 to 6 of 6

Why does adding an IF statement alter the outcome of the calculation occasionally?

  1. #1
    Registered User
    Join Date
    09-05-2022
    Location
    Sarpsborg, Norway
    MS-Off Ver
    Microsoft 365
    Posts
    3

    Question Why does adding an IF statement alter the outcome of the calculation occasionally?

    I am trying to calculate overtime, and am using the following simple setup (file attached):

    A1: 07:30
    B1: 15:15

    C1: =((B1-A1)*1440)-465 This will show a positive value if the difference between A1 & B1 is more than 465 minutes, and a negative value if it's below 465. I get 0, which is correct
    C2: =IF(A2,((B2-A2)*1440)-465,"") Should give the same outcome, as long as A2 is not empty. I get -0.00000000000005684342 ...


    However, if I repeat the same process with 08:30-16:15, both formulas give me the correct answer, 0

    Why is this happening?
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    12-28-2014
    Location
    NSW, Australia
    MS-Off Ver
    MS365
    Posts
    604

    Re: Why does adding an IF statement alter the outcome of the calculation occasionally?

    Another wierd Excel thing
    I can't help answering your question as to why, but maybe this little change will help your calculations.

    =IF(A2,INT((B2-A2)*1440)-465,"")

  3. #3
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,942

    Re: Why does adding an IF statement alter the outcome of the calculation occasionally?

    My 1st question would be...
    =if(A2...what?

    All this is ding is testing to see if A2 contains a value. If it does, then it calcs ((B2-A2)*1440)-465 otherwise it returns "" (nothing)

    You are encountering excel's floating point decimal error. It is a known error, read this to understand what it is...
    https://docs.microsoft.com/en-us/off...ccurate-result
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  4. #4
    Registered User
    Join Date
    09-05-2022
    Location
    Sarpsborg, Norway
    MS-Off Ver
    Microsoft 365
    Posts
    3

    Re: Why does adding an IF statement alter the outcome of the calculation occasionally?

    Quote Originally Posted by FDibbins View Post
    My 1st question would be...
    =if(A2...what?

    All this is ding is testing to see if A2 contains a value. If it does, then it calcs ((B2-A2)*1440)-465 otherwise it returns "" (nothing)

    You are encountering excel's floating point decimal error. It is a known error, read this to understand what it is...
    *link removed*

    The IF-statement was more complicated, and basically only there to prevent the calculation from taking place should any of the required values not be present.

    My whole point is that the formula has a different outcome when I add the IF-statement, and only for certain values.

  5. #5
    Registered User
    Join Date
    09-05-2022
    Location
    Sarpsborg, Norway
    MS-Off Ver
    Microsoft 365
    Posts
    3

    Re: Why does adding an IF statement alter the outcome of the calculation occasionally?

    Just did a test with Google Sheets.

    For the 07:30-15:15 period, it also reports -0.00000000000005684342 (both with and without the IF)
    For 08:30-16:15 it reports 0

    So it can't be excel's floating point decimal error, as suggested. Unless Google Sheets is a web interface for Microsoft Excel...?

  6. #6
    Valued Forum Contributor
    Join Date
    07-13-2021
    Location
    California, USA
    MS-Off Ver
    2010
    Posts
    513

    Re: Why does adding an IF statement alter the outcome of the calculation occasionally?

    Quote Originally Posted by EvertM View Post
    My whole point is that the formula has a different outcome when I add the IF-statement, and only for certain values.
    To summarize, you are asking why

    =((B1-A1)*1440)-465

    results in exactly zero (0.00E+00 in Scientific format)

    but the same calculation in an IF expression does not. Namely:

    =IF(A2,((B2-A2)*1440)-465,"")

    results in about -5.68E-14, an infinitesimal difference.

    FDibbins partially answers the question: the infinitesimal difference (-5.68E-14) is the correct result due to binary arithmetic anomalies.

    But Excel arbitrarily replaces such infinitesimal differences with exactly zero, but only under certain arcane conditions and in very limited contexts, namely: only formulas of the literal form =expression - expression and both operands are nonzero (or addition of nonzero values with opposite signs).

    This is poorly and incompletely explained under the title "Example when a value reaches zero" in the MSFT document that FDibbins cites.

    (There are also some conditions where the SUM function -- and only the SUM function -- also replaces infinitesimal differences with exactly zero.)

    The syntax is very specific. For example, =(expression) and =expression+0 will defeat this heuristic, resulting in an infinitesimal difference.

    So, if you wrote =((B1-A1)*1440 - 465) instead, you would see the same result as you see with the IF expression, assuming the cell format is General or Scientific.
    Last edited by curiouscat408; 09-06-2022 at 08:48 PM.

+ 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] Formula to alter calculation dependant on drop down list value
    By peterderrington in forum Excel Formulas & Functions
    Replies: 34
    Last Post: 03-12-2021, 09:53 AM
  2. [SOLVED] IF statement with variable outcome
    By Jeff Rich in forum Excel General
    Replies: 9
    Last Post: 01-17-2020, 04:55 PM
  3. [SOLVED] if statement third outcome
    By steddas in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-08-2018, 09:57 AM
  4. Calculation - Adding IF statement to existing logic
    By bdav1216 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 12-27-2016, 10:37 AM
  5. [SOLVED] how to alter my select statement
    By dmcgov in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-04-2016, 10:43 AM
  6. [SOLVED] Multiple outcome if and statement
    By retroboy17 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 03-08-2014, 02:09 AM
  7. [SOLVED] issue with outcome of if/then statement in VBA
    By Marijke in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-19-2013, 10:18 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