6.3: Numerical Error
- Page ID
- 63898
Consider the following python statement when answering the questions below:
Why does Python return False even though the above statement is clearly true?
Let’s consider another example. Run the following code which should return 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:
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.
If Python is sometimes wrong, why do we use it?
What are ways you can do to watch out for these types of errors?
Modify the following program to return True if the values are within some small number (e
) of each other.
What is a good value to set for e
and why?
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: