A Python 3 script running on a Linux server needs to determine whether a newly-calculated metric is equal to an expected value. The developer tried:
if 0.1 + 0.2 == 0.3:
send_alert()
but discovered that the condition evaluates to False because of binary floating-point representation. Which standard-library function, added in Python 3.5, offers a reliable way to test whether two floating-point numbers are approximately equal within a user-defined tolerance?
Binary floating-point values such as 0.1 and 0.2 cannot be represented exactly, so their sum is slightly larger than 0.3. The math.isclose() function compares two floats using relative and/or absolute tolerances, so it correctly treats 0.1 + 0.2 as close enough to 0.3 for most practical purposes. The other answers are not valid: math.equal() and sys.float_compare() do not exist, and statistics.close() is not part of the statistics module.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
What does binary floating-point representation mean, and why does it cause issues in Python calculations?
Open an interactive chat with Bash
How does the `math.isclose()` function work in Python, and what parameters does it accept?
Open an interactive chat with Bash
What are some practical use cases where `math.isclose()` is essential?
Open an interactive chat with Bash
CompTIA Linux+ XK0-006 (V8)
Automation, Orchestration, and Scripting
Your Score:
Report Issue
Bash, the Crucial Exams Chat Bot
AI Bot
Loading...
Loading...
Loading...
Pass with Confidence.
IT & Cybersecurity Package
You have hit the limits of our free tier, become a Premium Member today for unlimited access.
Military, Healthcare worker, Gov. employee or Teacher? See if you qualify for a Community Discount.
Monthly
$19.99
$19.99/mo
Billed monthly, Cancel any time.
3 Month Pass
$44.99
$14.99/mo
One time purchase of $44.99, Does not auto-renew.
MOST POPULAR
Annual Pass
$119.99
$9.99/mo
One time purchase of $119.99, Does not auto-renew.
BEST DEAL
Lifetime Pass
$189.99
One time purchase, Good for life.
What You Get
All IT & Cybersecurity Package plans include the following perks and exams .