+ Reply to Thread
Results 1 to 6 of 6

Precision problem while using the MOD function

  1. #1
    Registered User
    Join Date
    12-25-2019
    Location
    Charlotte, Nortgh Carolina, USA
    MS-Off Ver
    office 365
    Posts
    3

    Angry Precision problem while using the MOD function

    I am using the MOD function and then inverting the resulting values.
    When I invert 1/(MOD(11/10,1) Excel shows 10 which is correct, because
    MOD(11/10,1) EQUALS 0.1 and inverting 0.1 gives you 10.
    However when I look into the precision of this value, it is actually 9.999999999999990000000
    In most applications this would not be a problem, but it is on mine.
    I tried just simply making 1/0.1 and it gives me an even 10, but when I use mod then I have the
    problem. Any reasons or fixes for this?
    I am using Excel for MAC
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    12-25-2019
    Location
    Charlotte, Nortgh Carolina, USA
    MS-Off Ver
    office 365
    Posts
    3

    Re: Precision problem while using the MOD function

    I have used round down and floor, and the same happens, but only with the value 11/10 with 12/10 for example it works good.

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,819

    Re: Precision problem while using the MOD function

    Floating point errors are just an inherent part of the way computers do arithmetic. More than you probably want to know about floating point calculations and associated errors: https://www.excelforum.com/groups/ma...nd-errors.html

    Usual solutions are to use a rounding function, but it sounds like you have already tried that, though you don't specify exactly how you tried using these rounding functions. I would have expected something like =ROUND(MOD(11/10,1),0) to return exactly 10. Have you tried something like that?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  4. #4
    Registered User
    Join Date
    12-25-2019
    Location
    Charlotte, Nortgh Carolina, USA
    MS-Off Ver
    office 365
    Posts
    3

    Re: Precision problem while using the MOD function

    I tried the rounding function instead, but it gives me the same error; as well as the floor.
    Whats weird is that it only happens for 11/10 when I enter straight 0.1, the inverse has no precision problems.
    I am going to try nesting it as you put it, and the INT function as well.

  5. #5
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Precision problem while using the MOD function

    Mr. Shorty is absolutely correct.

    Since your sample file contains 50 digits after the decimal, it seems like you don't understand how Excel stores numbers.
    Excel stores numbers in binary format and 0.1 can NOT be stored EXACTLY in binary format.
    Excel floating point numbers can be represented in 15 significant digits. When you divide by a number like 0.1 you lose one or two digits of precision.

    Your problem is not an error, but in the way the results are viewed. If you change the output in your file to 13 digits after the decimal (14 significant digits for 9.9999...) Excel will display the answer you want (10.0000....).

    For an explanation from Microsoft see: https://www.microsoft.com/en-us/micr...wrong-answers/

    Lewis

  6. #6
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Precision problem while using the MOD function

    Quote Originally Posted by Pedro Luis View Post
    I tried the rounding function instead, but it gives me the same error; as well as the floor.
    Whats weird is that it only happens for 11/10 when I enter straight 0.1, the inverse has no precision problems.
    What's your exact ROUND formula? C3: =ROUND(1/MOD(11/10,1),0) displays 10, D3: 10 displays 10 (no surprise), E3: =C3=D3 returns TRUE.

    Excel does some implicit truncation handling for SOME formulas, e.g., C3: =1/MOD(11/10,1) displays 10, D3: =C3-10 displays 0, and E3: =D3=0 returns TRUE. However, F3: =(C3-10) displays and returns -8.88178E-15. Should =C3-10 and =(C3-10) return different values? Even if the answer is definitely not, that's what Excel does.

    Anyway, details on 1/MOD({11..19}/10,1).

    A B B displays
    1 =1/MOD(11/10,1) =(A1-10) -8.88178E-15
    2 =1/MOD(12/10,1) =(A2-10/2) 8.88178E-16
    3 =1/MOD(13/10,1) =(A3-10/3) -4.44089E-16
    4 =1/MOD(14/10,1) =(A4-10/4) 4.44089E-16
    5 =1/MOD(15/10,1) =(A5-10/5) 0
    6 =1/MOD(16/10,1) =(A6-10/6) -2.22045E-16
    7 =1/MOD(17/10,1) =(A7-10/7) 0
    8 =1/MOD(18/10,1) =(A8-10/8) 0
    9 =1/MOD(19/10,1) =(A9-10/9) 0

    The reason row 5 shows 0 in col B is because 15/10 is exactly represented in binary floating point, so MOD(15/10,1) is exactly 0.5, and its reciprocal is exactly 2.

    As for rows 7 to 9, they show 0 because the truncation errors are too small for Excel to represent in 15 decimal places. It's not that binary floating point represents them exactly. Rather that it'd take more than 52 mantissa bits to represent the difference.

    Why is 11/10 different from the others? Order of magnitude of the truncation error. The others are < 1E-15 in absolute value, but 11/10 is > 1E-15 in absolute value. This is actually pure happenstance. You'll get different results for denominators that are products of powers of primes other than 2 and 5.
    Last edited by hrlngrv; 01-23-2020 at 09:03 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. Excel problem of floating precision like 100/33 result 3.03030303030303
    By Shahadat65 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-22-2016, 12:28 AM
  2. Using precision as displayed: WKS vs. WKB
    By h2count in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-26-2012, 06:21 PM
  3. Is it an INTEGER precision problem ?
    By ElmerS in forum Excel General
    Replies: 3
    Last Post: 03-31-2010, 08:41 AM
  4. Problem with Iterative Code, Excel Precision, or my maths??
    By Phil_V in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-19-2009, 09:59 AM
  5. Precision displayed does not match precision in cell
    By James Wilkerson in forum Excel General
    Replies: 10
    Last Post: 06-15-2005, 10:05 AM
  6. precision question
    By D in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-15-2005, 05:06 PM
  7. Precision problem with automated formula fill-in
    By Erny Meyer in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-04-2005, 10:06 AM

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