1

What is a robust and concise way to make less <and greater > comparisons that are robust to floating-point imprecision, i.e., operations that are robust to small differences that could stem from floating-point inaccuracy issues?

In other terms: For == we have all.equal, but what do we have for < and >?

Tidyverse example with tibble

library(tidyverse)

tribble(~ x, 0.1 + 0.2) %>%
  mutate(
    test = x > 0.3
  )

results in

# A tibble: 1 × 2
      x test 
  <dbl> <lgl>
1   0.3 TRUE 

whereas I would expect FALSE instead of TRUE (since 0.3 > 0.3 mathematically is false).

thando
  • 483
  • 2
  • 4
  • 18
  • 1
    Could you clarify what you are hoping to get? Do you want a version of `>` that ignores small differences that could plausibly be floating point accuracy issues? I assume you want your example to produce `FALSE`, not `TRUE`... – Gregor Thomas Jun 22 '22 at 16:01
  • Thank you, I tried to clarify the question. – thando Jun 22 '22 at 16:05
  • 1
    Closing as a duplicate because [this answer to the duplicate question](https://stackoverflow.com/a/56309050/190277) shows how to do what you want. – Ben Bolker Jun 22 '22 at 19:10
  • Thanks for the hint. Translating from the linked answer for `>=`, the answer to my question would be a check like: `0.1 + 0.2 > 0.3 & !all.equal(0.1 + 0.2, 0.3)` – thando Jun 22 '22 at 20:07

0 Answers0