Wikipedia
def machineEpsilon(func=float):
machine_epsilon = func(1)
while func(1)+machine_epsilon != func(1):
machine_epsilon_last = machine_epsilon
machine_epsilon = func(machine_epsilon) / func(2)
return machine_epsilon_last
print(machineEpsilon())
Output:
2.22044604925e-16
epsilon.py (same as above method)
# Start with a value that's way too big.
epsilon = 1.0
# Keep halving it until it gets too small.
while 1.0 + epsilon > 1.0:
epsilon = epsilon / 2.0
# It's one step too small now so double
# it to move back one step.
epsilon = epsilon * 2.0
# And output the result.
print epsilon
Output:
2.22044604925e-16
Start with two non-negative numbers which are definitely larger and smaller than the machine epsilon. These define an interval which contains the machine epsilon. We repeatedly calculate the mid-point between them and determine if it is larger or smaller than the epsilon and change the bounds of the interval accordingly. We keep going until the interval is short enough.
epsilon2.py
too_large = 1.0
too_small = 0.0
tolerance = 1.0e-27
while too_large - too_small > tolerance:
estimate = (too_large + too_small) / 2.0
if 1.0 + estimate > 1.0:
too_large = estimate
else:
too_small = estimate
print too_small, '< epsilon <', too_large
Output:
1.11022302463e-16 < epsilon < 1.11022302463e-16
|