+ Reply to Thread
Results 1 to 6 of 6

Calculation error when using VLOOKUP due to rounding

  1. #1
    Registered User
    Join Date
    04-25-2017
    Location
    Greenland
    MS-Off Ver
    2016
    Posts
    8

    Calculation error when using VLOOKUP due to rounding

    I have made a completely horrific formula that looksup values from three different tables, depending on two variables changed by the user, and a lot of predefined areas.

    I've tried translating the formula to English, but if there's a function that you don't recognise, then it's likely because it's in Danish. The formula is as follows:

    =IFERROR(IF(D15>INDIRECT("A_mål"&E$11);INDIRECT("Bund"&E$11);IFERROR(IFNA(INDEX(INDIRECT(VLOOKUP(E$11;Tankmatrix;4;0));MATCH(FLOOR.MATH(IF(INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15>VLOOKUP(E$11;Tankmatrix;2;0);VLOOKUP(E$11;Tankmatrix;2;0);INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15);100);INDEX(INDIRECT(VLOOKUP(E$11;Tankmatrix;4;0));;1);0);MATCH(FLOOR.MATH(IF(INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15>VLOOKUP(E$11;Tankmatrix;2;0);VLOOKUP(E$11;Tankmatrix;2;0);INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15)-FLOOR.MATH(IF(INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15>VLOOKUP(E$11;Tankmatrix;2;0);VLOOKUP(E$11;Tankmatrix;2;0);INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15);100);10);INDEX(INDIRECT(VLOOKUP(E$11;Tankmatrix;4;0));1;);0))+INDEX(INDIRECT(VLOOKUP(E$11;Tankmatrix;5;0));MATCH(FLOOR.MATH(IF(INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15>VLOOKUP(E$11;Tankmatrix;2;0);VLOOKUP(E$11;Tankmatrix;2;0);INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15)-FLOOR.MATH(IF(INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15>VLOOKUP(E$11;Tankmatrix;2;0);VLOOKUP(E$11;Tankmatrix;2;0);INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15);10);1);INDEX(INDIRECT(VLOOKUP(E$11;Tankmatrix;5;0));;1);0);MATCH(FLOOR.MATH(IF(INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15>VLOOKUP(E$11;Tankmatrix;2;0);VLOOKUP(E$11;Tankmatrix;2;0);INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15)-FLOOR.MATH(IF(INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15>VLOOKUP(E$11;Tankmatrix;2;0);VLOOKUP(E$11;Tankmatrix;2;0);INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15);1);0,1)+0,01;INDEX(INDIRECT(VLOOKUP(E$11;Tankmatrix;5;0));1;);1))+INDIRECT(VLOOKUP(E$11;Tankmatrix;6;0));"");""));"")

    Horrific, right?

    Anyhow, my problem is that while it does what it is supposed to do, there is a small error that occurs every now and then, and I suspect that it is around the end of the code, more accurately in this part

    INDEX(INDIRECT(VLOOKUP(E$11;Tankmatrix;5;0));MATCH(FLOOR.MATH(IF(INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15>VLOOKUP(E$11;Tankmatrix;2;0);VLOOKUP(E$11;Tankmatrix;2;0);INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15)-FLOOR.MATH(IF(INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15>VLOOKUP(E$11;Tankmatrix;2;0);VLOOKUP(E$11;Tankmatrix;2;0);INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15);10);1);INDEX(INDIRECT(VLOOKUP(E$11;Tankmatrix;5;0));;1);0);MATCH(FLOOR.MATH(IF(INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15>VLOOKUP(E$11;Tankmatrix;2;0);VLOOKUP(E$11;Tankmatrix;2;0);INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15)-FLOOR.MATH(IF(INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15>VLOOKUP(E$11;Tankmatrix;2;0);VLOOKUP(E$11;Tankmatrix;2;0);INDIRECT(VLOOKUP(E$11;Tankmatrix;7;0))-D15);1);0,1)+0,01;INDEX(INDIRECT(VLOOKUP(E$11;Tankmatrix;5;0));1;);1))

    It seems that if I try rounding to 0,1 which sadly is needed, then Excel sometimes beefs this up a bit.
    When I do this calculation: FLOOR.MATH(564,3-FLOOR.MATH(564,3;1);0,1) I would expect the formula to return 0,3, but it actually returns 0,2. This wouldn't be a problem if the error was consistent as I could just add +0,01 and that would fix it, but if I use 564,2 I expect it to return 0,2 and indeed that is what it returns.
    In this case I am searching for an exact match with VLOOKUP.

    If instead I'm using the approximated match with the VLOOKUP, then the formula would only need to look like this:
    564,3-FLOOR.MATH(564,3;1)
    then the match would take care of the ounding, but I am unsure whether or not the error occurs here to be honest.

    Does anyone have an idea as to how I can get around this annoying, erratic error?

    I'm adding my sheet as an attachment, be aware that it has macros built in. There are also several hidden sheets with the tables that the formula searches in.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    04-25-2017
    Location
    Greenland
    MS-Off Ver
    2016
    Posts
    8

    Re: Calculation error when using VLOOKUP due to rounding

    It might actually be the MATCH function and not the VLOOKUP that is doing this.

    Also, the sheet is locked, but without a password

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

    Re: Calculation error when using VLOOKUP due to rounding

    Quote Originally Posted by Baconfish View Post
    When I do this calculation: FLOOR.MATH(564,3-FLOOR.MATH(564,3;1);0,1) I would expect the formula to return 0,3, but it actually returns 0,2.
    [....]
    If instead I'm using the approximated match with the VLOOKUP, then the formula would only need to look like this:
    564,3-FLOOR.MATH(564,3;1)
    then the match would take care of the ounding, but I am unsure whether or not the error occurs here to be honest.
    Does anyone have an idea as to how I can get around this annoying, erratic error?
    Note: I use period for the decimal point, where you use comma; and I use comma to separate parameters, where you use semicolon. Moreover, my version of Excel only has FLOOR; but I believe that is the same as FLOOR.MATH as you use it.

    Yes, that is probably at least one source of your problems. Honestly, I am not bothering to look at anything else.

    You seem to want to "extract" the fractional part of a value, rounded to 1 decimal place. Am I correct?

    If so, a simpler formula is:

    =ROUND(MOD(564.3,1),1)

    First, note that 564,3-FLOOR.MATH(564,3;1) only appears to be 0.3. If that formula is in A1, note that =A1=0.3 return FALSE(!).

    In any case, even it worked, it would be by coincidence. In other words, it might not work for other values.

    There are two issues....

    First, most decimal fractions can only be approximated in 64-binary floating-point, which Excel uses to store numeric values. Moreover, the approximation depends on the magnitude of the integer part. For example, the exact value is on the right:
    Please Login or Register  to view this content.
    Here, I use period for the decimal point, and comma to demarcate the first 15 significant digits, which is all that Excel displays and formats, an arbitrary limitation.

    Note the difference in the approximations of 0.3 (3/10).

    So in general, when we expect a calculation to be accurate to n decimal places, we should explicitly round to that number of decimal places. (Not to an arbitrary number of decimal places like 10, as some people suggest.)

    Second, because of these approximations, FLOOR(.MATH) is unreliable when the second parameter is a decimal fraction (unless it is an exact power of 2).
    Last edited by joeu2004; 04-25-2017 at 12:46 PM. Reason: minor

  4. #4
    Registered User
    Join Date
    04-25-2017
    Location
    Greenland
    MS-Off Ver
    2016
    Posts
    8

    Re: Calculation error when using VLOOKUP due to rounding

    Actually I want to dissect the number 564,3 to become 500, 60, 4 and 0,3.

    I've reworked the thing completely as it was much too complex, and it is now easier to work with. I also got rid of the error that kept bugging me by using the VLOOKUP entirely instead of the MATCH and INDEX.

    The solution is shorter and much more elegant. As you can see, I'm adding 0,01 to the decimal number and then letting the VLOOKUP round the number by selecting an approximate match rather than a exact. For the rest, an exact match is still needed.

    Thank you for the indepth explanation of decimals in excel, that'll be sure to help me later on.

    Edit#1 Apparently I'm notallowed to add the formula to this reply, so I'll add it to the post instead if that's possible.

    Edit#2 I'm not allowed to do that either, so I can't really show my working code
    Last edited by Baconfish; 04-25-2017 at 02:33 PM.

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

    Re: Calculation error when using VLOOKUP due to rounding

    Quote Originally Posted by joeu2004 View Post
    You seem to want to "extract" the fractional part of a value, rounded to 1 decimal place. Am I correct?
    Obviously not! Sorry about that.

    If the value were 564.29, presumably you want 0.2. My ROUND formula returns 0.3. Klunk!

    Ostensibly, the correct formula is:

    ROUNDDOWN(MOD(564.3,1),1)

    But that still returns 0.2 (sigh).

    Again, the problem is with the limited approximation of 0.3 due to the magnitude of the integer part (564). Consequently, the exact decimal represetation of 564.3 is 564.299999999999,954525264911353588104248046875. So MOD(564.3,1) returns exactly 0.299999999999954,525264911353588104248046875, which of course should round down to 0.2.

    Moreover, I assume that 564.3 is really a calculated value. So what appears to be 564.3, due to formatting, might be something else, even if it appears to be 564.300000000000 when formatted to show 15 significant digits.

    Previously, I said that if you expect calculations to be accurate to n decimal places, you should explicitly round to that number of decimal places. So if you expect calculations to be accurate to 5 decimal places, you should write:

    ROUNDDOWN(ROUND(MOD(564.3,1),5),1)

    However, for some calculations, we have no accuracy expectations; and imposing one might alter the outcome adversely. Example: financial interest calculations (amortization).

    Off-hand, I cannot think of any general solution to the numerical error caused by the "truncation" of the approximation of 0.3 in 564.3.

    Maybe there is one, and it just escapes me at the moment. (I'm have a "senior moment".)

    Many people suggest weird adjustments like adding/subtracting 1E-10 or rounding to 10 decimal places or some-such. Some people might suggest making that adjustment in the VLOOKUP/MATCH look-up range. None of that works in general.

    I'll continue to think about it. But I don't have much hope. It would be nice to know if the "round to n decimal places" solution is sufficient for your purposes.
    Last edited by joeu2004; 04-25-2017 at 02:21 PM. Reason: minor

  6. #6
    Registered User
    Join Date
    04-25-2017
    Location
    Greenland
    MS-Off Ver
    2016
    Posts
    8

    Re: Calculation error when using VLOOKUP due to rounding

    The number 564,3 is a number entered by the user, and will likely always be a one decimal number. Maybe two if we're unlucky.

    http://imgur.com/7VRHG1U As I can't add the formula to any of my posts, then here's a picture. That seems to work in all cases, I've tried everything from ,0 to ,9 and it calculated correctly.

    I should probably still take a look at working around the FLOOR.MATH if it is that unreliable. As you can see, I used the weird adjustments and added +0,01 and while that works, it is by no means optimal. I should probably also use a MATCH function rather than adding +2 to the column number in the VLOOKUP.

    But it works.

+ 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] Back-calculating a Value which uses Rounding in its Calculation
    By dragon324 in forum Excel General
    Replies: 3
    Last Post: 11-25-2016, 07:08 AM
  2. [SOLVED] How to stop last digit rounding up for percentage calculation
    By Br0ther_bruce in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 07-04-2014, 12:07 AM
  3. Display rounded number but don't use rounding in calculation
    By darxide23 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-05-2013, 03:47 PM
  4. Error in Time calculation: could be VLOOKUP?
    By JugglingReferee in forum Excel General
    Replies: 2
    Last Post: 01-12-2010, 10:51 PM
  5. [SOLVED] rounding a calculation down to the nearest 0.5 - how?
    By Suza in forum Excel General
    Replies: 2
    Last Post: 03-29-2006, 10:15 AM
  6. How do I stop a calculation rounding up
    By Lynneth in forum Excel General
    Replies: 8
    Last Post: 01-13-2006, 04:00 PM
  7. Rounding calculation to ONLY two deci places
    By Mustang in forum Excel General
    Replies: 5
    Last Post: 11-17-2005, 01:52 PM

Tags for this Thread

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