Comparing Floating Points Number for Almost-Equality in Python
Last Updated :
21 Sep, 2024
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)
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))
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))
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.
Similar Reads
Python | Numpy np.assert_almost_equal() method With the help of np.assert_almost_equal() method, we can get the assertion error if two items are not equal up to desired precision value by using np.assert_almost_equal() method. Syntax : np.assert_almost_equal(actual, desired, decimal) Return : Return the assertion error if two values are not equa
1 min read
Floating point error in Python Python, a widely used programming language, excels in numerical computing tasks, yet it is not immune to the challenges posed by floating-point arithmetic. Floating-point numbers in Python are approximations of real numbers, leading to rounding errors, loss of precision, and cancellations that can t
8 min read
Python | Numpy np.assert_array_almost_equal() method With the help of np.assert_array_almost_equal() method, we can get the assertion error if two array objects are not equal up to desired precision value by using np.assert_array_almost_equal() method. Syntax : np.assert_array_almost_equal(actual, desired, decimal) Return : Return the assertion error
1 min read
Python | Numpy np.assert_approx_equal() method With the help of np.assert_approx_equal() method, we can get the assertion error if two items are not equal up to significant digits by using np.assert_approx_equal() method. Syntax : np.assert_approx_equal(actual, desired, significant) Return : Return the assertion error if two values are not equal
1 min read
Python | Numpy np.assert_equal() method With the help of np.assert_equal() method, we can get the assertion error when two objects are not equal by using np.assert_equal() method. Syntax : np.assert_equal(actual, desired) Return : Return assertion error if two object are unequal. Example #1 : In this example we can see that by using np.as
1 min read