Why can comparing floating-point numbers with == be problematic in R?

Quick Overview

Asks why testing floating-point numbers for exact equality with == gives surprising results in R and how to compare them safely. It tests understanding of binary representation, accumulated rounding error and tolerance-based comparison with all.equal().

Why can comparing floating-point numbers with == be problematic in R?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Why can comparing floating-point numbers with `==` be problematic in R? ```hint Look past the default print Print `0.1 + 0.2` and `0.3` with `digits = 17` and compare what you see. ``` ### What a Strong Answer Covers - Why many decimal fractions have no exact binary representation as doubles - A concrete exact comparison that fails, and why the default printed output hides it - How to compare with a tolerance in R, including why `all.equal()` needs `isTRUE()` - The difference between an absolute and a relative tolerance, and where each fails - Where the problem shows up in data work beyond a literal `==` ### Follow-up Questions - `all.equal()` returns one result for two whole vectors. How would you compare them element by element? - How would you choose a tolerance for a column whose values range from 1e-9 to 1e9? - How would you filter or join a data frame safely on a computed numeric column? - Which numbers can a double represent exactly?

Overview: Asks why testing floating-point numbers for exact equality with == gives surprising results in R and how to compare them safely. It tests understanding of binary representation, accumulated rounding error and tolerance-based comparison with all.equal().

Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Why can comparing floating-point numbers with == be problematic in R?

What a Strong Answer Covers Guidance

  • Why many decimal fractions have no exact binary representation as doubles
  • A concrete exact comparison that fails, and why the default printed output hides it
  • How to compare with a tolerance in R, including why all.equal() needs isTRUE()
  • The difference between an absolute and a relative tolerance, and where each fails
  • Where the problem shows up in data work beyond a literal ==

Follow-up Questions Guidance

  • all.equal() returns one result for two whole vectors. How would you compare them element by element?
  • How would you choose a tolerance for a column whose values range from 1e-9 to 1e9?
  • How would you filter or join a data frame safely on a computed numeric column?
  • Which numbers can a double represent exactly?