Skip to main content
Mathematics LibreTexts

6.3: Numerical Error

  • Page ID
    63898
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Consider the following python statement when answering the questions below:

    0.1 + 0.2 == 0.3
    False
    Question

    Why does Python return False even though the above statement is clearly true?

    Do This

    Let’s consider another example. Run the following code which should return true.

    import numpy as np
    J = np.array([20])
    L = [20]
    
    pow(L[0],8) == pow(J[0],8)
    True

    If you have an older version of numpy installed (like 1.18.5) then the results of running the above cell may be false (did anyone get this result?). This is because numpy changed how it handles something called “roundoff error”. here is another cell that may help you see better what is going on:

    import numpy as np
    J = np.array([20])
    L = [20]
    print(pow(20,8))
    print(pow(L[0],8))
    print(pow(J[0],8))
    25600000000
    25600000000
    25600000000
    

    The older version of numpy would return the following:

    25600000000
    25600000000
    -169803776
    

    We could say to always upgrade to the latest stable version (generally a good idea). But some other libraries that depend on numpy may not be up to date so sometimes python will install an older version to maintain compatibility. For example, one really popular program is tensorflow, which often requires an older version of numpy.

    Question

    If Python is sometimes wrong, why do we use it?

    Question

    What are ways you can do to watch out for these types of errors?

    Question

    Modify the following program to return True if the values are within some small number (e) of each other.

    def checktrue(a,b,e=0.001):
        return a == b
    
    #Test function
    checktrue(0.1+0.2, 0.3)
    False
    Question

    What is a good value to set for e and why?

    Question

    The errors seen in this example seem like they would be fairly common in Python. See if you can find a function in Numpy that has the same purpose as checktrue:

    The class answercheck program will take into consideration round off error. For example, the checkanswer.float command would consider both of the above correct:

    from urllib.request import urlretrieve
    urlretrieve('https://raw.githubusercontent.com/colbrydi/jupytercheck/master/answercheck.py', 
                'answercheck.py');
    from answercheck import checkanswer
    
    checkanswer.float(0.300,'e85b79abfd76b7c13b1334d8d8c194a5');
    Testing 0.3
    Answer seems to be correct
    
    
    checkanswer.float(0.1+0.2,'e85b79abfd76b7c13b1334d8d8c194a5')
    Testing 0.3
    Answer seems to be correct
    
    

    This page titled 6.3: Numerical Error is shared under a CC BY-NC 4.0 license and was authored, remixed, and/or curated by Dirk Colbry via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.