+ Reply to Thread
Results 1 to 10 of 10

round up/down anomaly?

  1. #1
    Registered User
    Join Date
    11-21-2015
    Location
    Wales
    MS-Off Ver
    2013
    Posts
    4

    round up/down anomaly?

    I thought I might help someone with a problem they were having:
    "Modified Rounding
    I need a formula that will round a value based on whether it is less than or equal to .25. So, for instance, 3.24 would round down to 3 and 3.25 (or greater) would round up to 4."
    So I wrote the formula =IF(MOD(B2,1)>0.24,ROUNDUP(B2,0),ROUNDDOWN(B2,0)) and tested it and it worked fine until it had to deal with numbers between 32.24 and 44.24 where it rounds up instead of down! I was wondering if anyone could tell me if I've missed something (likely) or is it a problem with Excel (less likely)!
    Thanks very much.
    Paul.

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

    Re: round up/down anomaly?

    Quote Originally Posted by thesurfer View Post
    I wrote the formula =IF(MOD(B2,1)>0.24,ROUNDUP(B2,0),ROUNDDOWN(B2,0)) and tested it and it worked fine until it had to deal with numbers between 32.24 and 44.24 where it rounds up instead of down! I was wondering if anyone could tell me if I've missed something (likely) or is it a problem with Excel (less likely)!
    Likely solution:

    =IF(ROUND(MOD(B2,1),2)>0.24,ROUNDUP(B2,0),ROUNDDOWN(B2,0))

    The problem is probably due to anomalies of 64-bit binary floating-point arithmetic, the native computer representation and arithmetic that Excel uses. Explicitly rounding usually works around such anomalies, which are common.

  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,929

    Re: round up/down anomaly?

    Hi, welcome to the forum

    Try this instead...
    =IF(MOD(B2,1)>=0.25,ROUNDUP(B2,0),ROUNDDOWN(B2,0)
    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
    11-21-2015
    Location
    Wales
    MS-Off Ver
    2013
    Posts
    4

    Re: round up/down anomaly?

    Thanks very much for your swift replies - both solutions work...all I have to do now is get my head around the reasons!

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

    Re: round up/down anomaly?

    Quote Originally Posted by joeu2004 View Post
    =IF(ROUND(MOD(B2,1),2)>0.24,ROUNDUP(B2,0),ROUNDDOWN(B2,0))
    Quote Originally Posted by FDibbins View Post
    =IF(MOD(B2,1)>=0.25,ROUNDUP(B2,0),ROUNDDOWN(B2,0))
    And what do you think =SUM(53.36, 88.35, 7.54) should return?

    The sum is displayed as 149.25, even when formatted with 12 decimal places (15 significant digits), the most that Excel formats.

    But in fact, it is 149.249999999999,971578290569595992565155029296875.

    (I use comma to demarcate the first 15 significant digits.)

    So the formula with ROUND(MOD(B2,1),2)>0.24 returns 150, as expected (I presume).

    But the formula with MOD(B2,1)>=0.25 returns 149(!).

    The point that I alluded to previously is: MOD(...,1) returns exactly the binary representation of the decimal fraction, which is not always what we want.

    But that does beg the question: are my assumptions correct?

    In particular, I assumed that "thesurfer" wants 0.245 and greater -- that is, any value that displays as 0.25 when formatted to 2 decimal places -- to round up because it is displayed as 0.25

    @thesurfer: If my assumption is incorrect, please be more precise about your requirements. Your code (">0.24"), your English description (not "less than or equal to .25"), and even your example do not agree.
    Last edited by joeu2004; 11-21-2015 at 09:49 PM. Reason: cosmetic

  6. #6
    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,929

    Re: round up/down anomaly?

    happy to help and thanks for the feedback.

    When looking for a anything < or = to something (like 0.25) I always use <= with the precise value, if I can

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

    Re: round up/down anomaly?

    Quote Originally Posted by thesurfer View Post
    I thought I might help someone with a problem they were having: "Modified Rounding
    I need a formula that will round a value based on whether it is less than or equal to .25. So, for instance, 3.24 would round down to 3 and 3.25 (or greater) would round up to 4."
    Another issue that you might want to ask the person you are helping: what does 4.25 round to?

    If 3.25 rounds to 4, you might think that 4.25 rounds to 5. But it is not uncommon to "round to even", aka "banker's rounding". For normal rounding, that applies to exactly 3.5 and 4.5, for example; both round to 4. But there is no reason why the same principle cannot apply when the rounding midpoint is x.25 instead of x.5.

    Quote Originally Posted by thesurfer View Post
    both solutions work
    Not if the value to be rounded might be calculated, instead of entered manually or input from a text file. See my example in response #5.

    Quote Originally Posted by thesurfer View Post
    all I have to do now is get my head around the reasons!
    For overwhelming and sometimes misleading and incorrect details, see KB 78113 (click here) [1].

    [1] http://support.microsoft.com/kb/78113

    Basically, in standard binary floating-point, numbers are approximated by the sum of 53 consecutive powers of 2 (called bits), some of which might be zero. Consequently, most non-integers cannot be represented exactly.

    That is also true of most integers greater than 9,007,199,254,740,992, which is 2^53.

    To demonstrate: Using pencil and paper, try to represent 1/10 exactly using only 1/16 + 1/32 + 0/64 + 0/128 + 1/256 etc -- that is, the sum of fractional powers of 2. It cannot be done!

    For your examples:
    Please Login or Register  to view this content.
    I use comma to demarcate the first 15 significant digits, which is all that Excel formats.

    Note that the approximation of x.24 is different for some examples. This is because some of the 53 bits must be used to represent the integer part. So there are fewer bits to represent the decimal fraction, which might result in the sum of fewer powers of 2.

    By coincidence, the approximation of x.24 is the same in 0.24 and 1.24. But it is greater than 0.24 in 3.24 and 32.24.

    That is why MOD(B2,1)>0.24 returns TRUE when B2 is 32.24.

    So why doesn't MOD(B2,1)>0.24 also return TRUE when B2 is 3.24?

    Well, it is! If we write MOD(B2,1)-0.24>0, that returns TRUE even when B2 is 3.24.

    MOD(B2,1)>0.24 returns FALSE when B2 is 3.24 because Excel tries to be clever sometimes. Sometimes, when two operands are "close enough", Excel treats them as equal. But the operative word is "sometimes". As demonstrated, the heuristic is applied inconsistently.

  8. #8
    Registered User
    Join Date
    11-21-2015
    Location
    Wales
    MS-Off Ver
    2013
    Posts
    4

    Re: round up/down anomaly?

    Thanks for your very detailed reply, this has inspired me to learn more about basic maths and indeed Excel. This is the first time I have fallen foul of the floating point anomaly and it's certainly given me much to think about when interpreting displayed results. Maybe I should have changed the spreadsheet's setting to 'set precision as displayed'. Out of curiosity, when calculating your example:
    0.24 0.239999999999999,9911182158029987476766109466552734375
    1.24 1.23999999999999,99911182158029987476766109466552734375
    3.24 3.24000000000000,02131628207280300557613372802734375
    32.24 32.2400000000000,0198951966012828052043914794921875
    how did you manage to calculate such a long string of numbers after the decimal point? I'm assuming Excel won't do this.

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

    Re: round up/down anomaly?

    Quote Originally Posted by thesurfer View Post
    Maybe I should have changed the spreadsheet's setting to 'set precision as displayed'.
    Although I agree that seems to be the simplest way to ensure everything is rounded "properly", I usually advise against it for a lot of reasons.

    If you choose to experiment with setting PAD, be sure to make a copy of your Excel file first.

    The biggest danger with PAD is that it might change constants permanently.

    For example, it is not uncommon to enter interest rates with 4 or more percentage decimal places (e.g. 4.1234%), but to display them with less precision (e.g. formatted as Percentage with 2 decimal places). If we now set PAD, 4.1234% will be changed to 4.12%, and that will change all of the dependent calculations, presumably unintentionally. If we recognize our mistake and either unselect PAD or change the format to Percentage with 4 decimal places, we're too late: the value will still be 4.12%.

    Another big danger with PAD is that it is non-selective.

    At first, you might think that is exactly why you want to use it. But if you have many worksheets, it will take you some time to review every calculation to be sure setting PAD did not have unintended and undesirable consequences.

    For formulas, we might correct those consequences by changing the cell formats.

    Finally, another issue with PAD is: it applies only to the final value of a cell.

    Consequenty, setting PAD does not fix surprises like IF(10.1 - 10 = 0.1, TRUE) returns FALSE(!). (For the same reason that MOD(...,1)>=0.25 does not always work as intended.) So you still must review every formula to see where you might need to use ROUND explicitly, notwithstanding setting PAD.

    I believe I have documented other issues with PAD in the past. But those are the first ones that come to mind.

    Quote Originally Posted by thesurfer View Post
    Out of curiosity, when calculating your example:
    0.24 0.239999999999999,9911182158029987476766109466552734375
    [....] how did you manage to calculate such a long string of numbers after the decimal point? I'm assuming Excel won't do this.
    I process the binary representation and sum the powers of 2 exactly. And you assume correctly: Excel (and VBA) will not do that.

  10. #10
    Registered User
    Join Date
    11-21-2015
    Location
    Wales
    MS-Off Ver
    2013
    Posts
    4

    Re: round up/down anomaly?

    Thanks again....I've learnt a lot today. I'll have a look at some of your earlier posts when I get time.

+ 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. anomaly in COUNTBLANK funciton
    By venkat1926 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-24-2010, 03:59 AM
  2. how to have a worksheet respond to an anomaly
    By slipit1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-20-2008, 11:55 AM
  3. IF function anomaly
    By BBS in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 12-07-2007, 11:51 AM
  4. DATEDIF anomaly?
    By cruisy in forum Excel General
    Replies: 5
    Last Post: 05-25-2007, 12:10 AM
  5. Anomaly with Chart Labels
    By D. Bishop in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 09-20-2005, 09:05 AM
  6. [SOLVED] Printing Anomaly
    By Otto Moehrbach in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-26-2005, 11:05 AM
  7. Formatting Anomaly
    By Christopher Weaver in forum Excel General
    Replies: 2
    Last Post: 05-13-2005, 06:06 PM
  8. [SOLVED] VLOOKUP Anomaly
    By Tosca in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-08-2005, 05:08 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