Value for epsilon in Python

PythonComparisonFloating PointEpsilon

Python Problem Overview


Is there a standard value for (or method for obtaining) epsilon in Python? I need to compare floating point values and want to compare against the smallest possible difference.

In C++ there's a function provided numeric_limits::epsilon( ) which gives the epsilon value for any given data type. Is there an equivalent in Python?

Python Solutions


Solution 1 - 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

Solution 2 - Python

As strcat posted, there is sys.float_info.epsilon.

But don't forget the pitfalls of using it as an absolute error margin for floating point comparisons. E.g. for large numbers, rounding error could exceed epsilon.

If you think you need a refresher, the standard reference is David Goldberg's What Every Computer Scientist Should Know About Floating-Point Arithmetic, or for a simpler review you can check out The Floating Point Guide.

Solution 3 - Python

If you cannot find a function to do that, remember that the algorithm to calculate the machine epsilon is very easy (you can test with your favourite programming language).E.g, for python:

eps = 1.0
while eps + 1 > 1:
    eps /= 2
eps *= 2
print("The machine epsilon is:", eps)

In my case, I got:

The machine epsilon is: 2.220446049250313e-16

Solution 4 - Python

Surprised nobody mentioned this here; I think many people would use numpy.finfo( type(variable) ).eps instead. Or .resolution if it is to assess precision.

Note that finfo is only for floating point types, and that it also works with Python's own float type (i.e. not restricted to numpy's types). The equivalent for integer types is iinfo, though it does not contain precision information (because, well, why would it?).

Solution 5 - Python

The following worked for me as well:

>>> import math
>>> math.ulp(1.0)
2.220446049250313e-16

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionthornateView Question on Stackoverflow
Solution 1 - PythonstrcatView Answer on Stackoverflow
Solution 2 - PythonErgwunView Answer on Stackoverflow
Solution 3 - Pythons.oucheneView Answer on Stackoverflow
Solution 4 - PythonJonathan HView Answer on Stackoverflow
Solution 5 - PythonRúben DiasView Answer on Stackoverflow