python numpy machine epsilon
An easier way to get the machine epsilon for a given float type is to use np.finfo()
:
print(np.finfo(float).eps)
# 2.22044604925e-16
print(np.finfo(np.float32).eps)
# 1.19209e-07
Machine Epsilon in Python
Floating point numbers have a certain precision, to a few decimal places in scientific notation. The larger the number, the larger the least significant digit in that representation, and thus the larger the "epsilon" that could contribute to that number.
Thus, the epsilon is relative to the number it is added to, which is in fact stated in the documentation you cited: "... such that 1.0 + eps != 1.0". If the "reference" number is smaller by, e.g. one order of magnitude, then eps is smaller, too.
If that was not the case, you could not calculate at all with numbers smaller than eps (2.2e-16
in my case).
Value for epsilon in Python
The information is available in sys.float_info
, which corresponds to float.h in C99.
>>> import sys
>>> sys.float_info.epsilon
2.220446049250313e-16
Machine epsilon with Numba
You can use np.finfo(np.float64).eps
in Numba njit
functions. This works well with Numba 0.54.1. See the following code:
@nb.njit('float64()')
def test():
return np.finfo(np.float64).eps
test() # Returns 2.220446049250313e-16
Declaring a constant is also fine. In both cases Numba generate a good assembly code that directly return the (precomputed) constant.
Is Python's epsilon value correct?
I think that what you you are seeing is how Python's float
type handles rounding when it runs out of precision. The Wikipedia text you quoted describing epsilon
appears to leave this part out.
In your example, 1 + delta
gets rounded up to 1 + epsilon
. Even though a float
can specify the differences between delta
and epsilon
, it cannot represent the difference between 1 + delta
and 1 + epsilon
. As you have noticed (with your delta2
test), the largest number that gets rounded down to 1
rather than up to 1 + epsilon
appears to be 1 + epsilon/2
.
So, a correct definition of what epsilon
means in Python might be:
epsilon
is the smallest positive floating point number such that (1 + epsilon) - 1
equals epsilon
.
Related Topics
How to Do Row-To-Column Transposition of Data in CSV Table
How to Read One Single Line of CSV Data in Python
How to Find Element by Part of Its Id Name in Selenium with Python
How to Implement a Minimal Server for Ajax in Python
Axes Class - Set Explicitly Size (Width/Height) of Axes in Given Units
Double Precision Floating Values in Python
Groupby Column and Filter Rows with Maximum Value in Pyspark
Passing Numpy Arrays to a C Function for Input and Output
Pyeval_Initthreads in Python 3: How/When to Call It? (The Saga Continues Ad Nauseam)
How to Write Code to Autocomplete Words and Sentences
Python Regular Expression Pattern * Is Not Working as Expected
Keep Same Dummy Variable in Training and Testing Data
Drag and Drop Explorer Files to Tkinter Entry Widget
How to Convert a Scikit-Learn Dataset to a Pandas Dataset
How to Initialize the Base (Super) Class
Reading the Target of a .Lnk File in Python
Why Do Two Identical Lists Have a Different Memory Footprint