Value for epsilon in Python

夙愿已清 提交于 2019-11-28 06:09:52

The information is available in sys.float_info, which corresponds to float.h in C99.

>>> import sys
>>> sys.float_info.epsilon
2.220446049250313e-16
Ergwun

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.

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?).

If you cannot find a function to do that, remember that the algorithm to calculate the machine precision 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 precision is:", eps)

In my case, I got:

The machine precision is: 2.220446049250313e-16

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!