Open In App

Comparing Floating Points Number for Almost-Equality in Python

Last Updated : 21 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In programming, comparing floating-point numbers can be tricky due to the inherent precision issues. Floats in Python, as in many programming languages, have limited precision and may not exactly represent some decimal values. This article explores how to compare floats for almost equality, providing methods and examples to help we handle floating-point precision issues effectively.

Challenges with Float Comparison

Floating-point numbers are represented in computer systems using a fixed number of bits. This representation can lead to precision issues, where numbers that are theoretically equal might not be represented exactly the same way in memory. Common challenges include:

  • Rounding Errors: Floats may not always store exact values due to rounding.
  • Imprecision: Small differences in representation can lead to seemingly unequal values.
  • Equality Issues: Direct comparison using equality operators (==) might fail due to these tiny discrepancies.

For example, 0.1 + 0.2 might not exactly equal 0.3 due to how floats are stored internally.

Python
print(0.1+0.2 == 0.3)
print(0.1+0.3 == 0.2+0.2)

Output
False
True


Methods for Comparison

To handle these challenges, several methods can be used to compare floating-point numbers for almost equality in Python.

  • Using Absolute Tolerance
  • Using math.isclose()
  • Using Numpy.isclose()

Let us see them one by one.

1. Using Absolute Tolerance

The absolute tolerance method involves comparing the absolute difference between two floats using the abs() function to a small threshold value. If the difference is less than the threshold, the numbers are considered almost equal.

Python
def are_almost_equal(a, b, tolerance=1e-9):
    return abs(a - b) < tolerance

a = 0.1 + 0.2
b = 0.3
print(are_almost_equal(a, b))

Output
True

2. Using math.isclose()

Python's math module provides the math.isclose() function which is designed for this purpose. It allows us to specify both relative and absolute tolerances. The rel_tol ompares the relative size of the difference between a and b to the magnitude of the numbers. The abs_tol ensures that even when comparing very small numbers, a fixed tolerance is applied to prevent the relative tolerance from being too small.

Python
import math

def are_almost_equal(a, b, rel_tol=1e-9, abs_tol=1e-9):
    return math.isclose(a, b, rel_tol=rel_tol, abs_tol=abs_tol)

# Comparing very large and very small numbers
large_num = 1e+100
small_num = 1e+100 + 1e-10
print(are_almost_equal(large_num, small_num)) 

Output
True

3. Using Numpy

For scientific computing and handling arrays of floating-point numbers, NumPy provides a similar function numpy.isclose() for comparing floats.

Python
import numpy as np

def are_almost_equal(a, b):
    return np.isclose(a, b)

a = np.array([1e+100])
b = np.array([1e+100 + 1e-10])

print(are_almost_equal(a, b)) 

Output:

[ True]

Conclusion

Comparing floating-point numbers for almost-equality requires careful handling due to precision limitations. By using methods such as absolute tolerance, relative tolerance, and Python’s built-in math.isclose() function, we can accurately determine if two floating-point numbers are nearly equal. This approach ensures that our comparisons are robust and less affected by the intricacies of floating-point arithmetic.


Next Article
Article Tags :
Practice Tags :

Similar Reads