The term “epsilon” typically refers to a small positive number that is used to represent the difference between two floating-point numbers.
It is often used in numerical computations to account for rounding errors and to compare two numbers for equality.
In Python, the constant sys.float_info.epsilon
represents the smallest positive float value that is greater than zero. It can be used to compare the difference between two float values to determine if they are equal within a certain tolerance.
For example:
import sys
eps = sys.float_info.epsilon
if abs(a - b) < eps:
print("a and b are equal within the tolerance of epsilon")
Code language: JavaScript (javascript)
As of writing, on my system sys.float_info.epsilon
returns 2.220446049250313e-1
.
Calculating Machine Epsilon in Python
Using the below code you can calculate the machine epsilon in Python:
eps = 1.0
while eps + 1 > 1:
eps /= 2
eps *= 2
print(f"Machine Epsilon: {eps}")
# Machine Epsilon: 2.220446049250313e-16
Code language: PHP (php)
As of writing, the code above returns 2.220446049250313e-1
, the same as sys.float_info.epsilon
.
Leave a Reply