If you want the epsilon for Python's float , use sys. float_info ; it would be strange to use NumPy just for that. If you're after values for NumPy types ( np. float32 , np.
How can this be measured? For any format, the machine epsilon is the difference between 1 and the next larger number that can be stored in that format. 2−23 ·= 1.19 × 10−7 i.e., we can store approximately 7 decimal digits of a number x in decimal format.
The machine epsilon (in double precision) is eps =2.220446049250313e−016. It is obtained when the number of terms is n =53.
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
Another easy way to get epsilon is:
In [1]: 7./3 - 4./3 -1
Out[1]: 2.220446049250313e-16
It will already work, as David pointed out!
>>> def machineEpsilon(func=float):
... machine_epsilon = func(1)
... while func(1)+func(machine_epsilon) != func(1):
... machine_epsilon_last = machine_epsilon
... machine_epsilon = func(machine_epsilon) / func(2)
... return machine_epsilon_last
...
>>> machineEpsilon(float)
2.220446049250313e-16
>>> import numpy
>>> machineEpsilon(numpy.float64)
2.2204460492503131e-16
>>> machineEpsilon(numpy.float32)
1.1920929e-07
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With