


How Can I Reliably Compare Floating-Point Numbers for Equality in Python?
Approximating Float Equality in Python: Standard Library Functions to the Rescue
It is widely recognized that comparing floats for exact equality can be problematic due to floating-point precision limitations, rendering simple equality checks unreliable. How can we overcome this challenge in Python?
Python's Built-in Solution
Python version 3.5 introduced two essential functions for this purpose: math.isclose and cmath.isclose. These functions, as defined in PEP 485, enable robust comparison of floats while accounting for precision-related discrepancies.
The isclose Function
The isclose function takes three parameters:
- a and b: The floats to be compared
- rel_tol (optional): A relative tolerance threshold
- abs_tol (optional): An absolute tolerance threshold
rel_tol represents a percentage of deviation allowed relative to the magnitudes of a and b. abs_tol, on the other hand, is an absolute threshold that must be met regardless of the magnitudes.
Tolerance Considerations
The function compares the absolute difference between a and b to both rel_tol * max(|a|, |b|) and abs_tol. If the difference is less than either of these values, the floats are considered equal.
Equivalent Implementation for Earlier Python Versions
For Python versions prior to 3.5, an equivalent function can be defined as follows:
def isclose(a, b, rel_tol=1e-09, abs_tol=0.0): return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)
Utilizing this function allows you to confidently compare floats for approximate equality, an invaluable tool when working with numerical data in Python.
The above is the detailed content of How Can I Reliably Compare Floating-Point Numbers for Equality in Python?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Fastapi ...

Using python in Linux terminal...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
