# Comparison of Values Using Epsilon in Python

Relational operators in Python and in every other programming language for that matter, help us compare different values with one another. Relational operators also known as comparison operators are generally used as conditions in loops and conditional statements to perform specific functions.

Epsilon in programming refers to a small quantity that represents a difference when comparing floating point variables – usually with the double equal to(“==”) sign. It is usually represented by the ε(lowercase) sign or the E(uppercase) sign.

A simpler explanation for what might be confusing when comparing floating point integers is that every number we use in our programs is stored as a binary representation in the computer’s memory. Since irrational numbers such as the value of pi, trail a lot after the decimal point, we can’t compare irrational numbers up to the correct precisions.

Hence, to successfully compare numbers very close to each other, we need to use the epsilon method using numpy or the absolute function to successfully carry out large scientific comparisons that will otherwise result in wrong outputs.

In Python programming, machine epsilon is a small quantity representing the difference between two floating-point numbers. It aids in the accurate comparison of such values. Python’s sys module or numpy’s finfo function can be used to find the machine epsilon for float64 (default) and float32. However, in cases where the precise comparison of floating point values is crucial, Python’s abs() function can be used to mitigate rounding off errors, enabling more accurate comparisons.

## Understanding Relational Operators in Python

To carry out comparisons in Python, we first need to understand the relational operators supported by this language. The relational operators in Python and their respective syntax and functions are as follows:

• “==”–> The double equal to sign is used to compare two values and check whether they are equal.
• “>=”–>The greater than or equal to sign is used to check whether one value is greater than or equal to the other one.
• “<=”–>The less than or equal to sign is used to check whether one value is less than or equal to another.
• “!=”–> The exclamation mark followed by the equals sign is used to check whether two values are not equal to one another. Since the classic mathematical symbol for “not equal to”(““) sign is unavailable, we use this symbol instead.
• “<“–> The less than sign is used for check whether a number is less than another.
• “>”–> The greater than sign is used to simply check whether a number is greater than another.

Related: Python Comparison Operators

## Determining Machine Epsilon in Python

The machine epsilon also known as the rounding off error when performing calculations in Python using float values, is of great use when we need precision or accuracy. The simplest way to find the value of epsilon is using the system information module, that is, the sys module. You can find out the value of machine epsilon in the following way:

```>>import sys
>>sys.float_info.epsilon
2.220446049250313e-16 #by default it returns the epsilon value for float64
```

Note: sys.float_info.epsilon is the smallest difference between 1.0 and the next float number Python can represent.

There are other ways to find the value for epsilon in Python. Those methods involve the numpy or numerical python module. According to your system and required accuracy, floating point precision can also be of two types, they are, double-bit precision(float64) or single bit precision(float32).

Since numpy is mostly used for scientific calculations, it justifies why there are two methods of approaching this. When you need faster computations but less accuracy, you can use the float32 epsilon value, but when you need accuracy and time is hardly a factor, the float64 value will be the correct way to go about it.

This is how you can find out the two epsilon values using numpy. Make sure to install/update numpy in your system before importing it in your project, else it might result in import errors.

```>>import numpy as np
>>np.finfo(np.float32).eps
1.1920929e-07 #value of epsilon for float32
>>np.finfo(np.float64).eps
2.220446049250313e-16 #value of epsilon for float64
```

To know more about float32 vs float64, check out the numpy documentation.

## Dealing with Rounding Errors in Python Using abs()

In simpler words when we calculate complex mathematical equations manually, we tend to ignore rounding off or trailing values. In computers, such values cannot be ignored because your system is designed to be as accurate as possible. For example, usually we say that 0.9=13.09-13 when performing calculations manually, but the same equation when checked via Python code will tell us that the two values are not equal as shown below.

```#our code
#comparing floating points without precision
a=13.09-13
#checking if values are equal
if (0.09==13.09-13):
print('yes, numbers are equal')
else:
print('Numbers are not equal')
print("Value of 13.09-13=",a)
```

In the given code we want the output to say “yes, numbers are equal” but that doesn’t normally happen. The output is:

```Numbers are not equal
Value of 13.09-13= 0.08999999999999986
```

From the above picture it is clear that when comparing the two values with each other, they are not equal. This can be further explained clearly when you print the subtraction result from the 2nd line at the end and look at the value of the decimal places.

This difference in values is what the epsilon roughly represents. This comparison in Python can be manipulated to round up to our desired decimal places using the abs() function which returns the absolute value of a number.

We will re write the code from the above section and then compare the same again in the following manner.

```#comparing floating points with precision
a=13.09-13
#checking if values are equal using the abs function
if (abs(0.09- (a))< 1e-2):
print('yes, numbers are equal')
else:
print('Numbers are not equal')
```

In the above code we have compared the absolute value of the difference between 0.09 and a, where a=13.09-13 , which is 0. Now that the difference is less than 0.01(represented by the scientific notation, 1e-2), then the if statement will be executed as the interpreter will now consider the numbers equal.

This method eliminates the incredibly long decimal precision problem that we faced previously for comparison.

Now the output will be as desired.

```#output
yes, numbers are equal
```

Similar: Is There a Ceiling Equivalent of // Operator in Python?

## To conclude..

Python’s machine epsilon plays a critical role in achieving precision during floating point comparisons. While the standard epsilon value in Python is derived from float64, both float32 and float64 can be used based on the needs of accuracy or computational speed.

Moreover, to avoid rounding off errors while comparing floating point numbers, the abs() function proves to be highly effective. These techniques are fundamental when dealing with scientific computations involving float comparisons. How else can you optimize floating point comparisons in your Python code?