# Thou shalt not compare numeric values (except when it works)

This was just going to be a few Tweets but it ended up being a bit of a rollercoaster of learning for me, and I haven’t blogged in far too long, so I’m writing it up quickly as a ‘hey look at that’ example for newcomers.

I’ve been working on the ‘merging data’ part of my book and, as I do when I’m writing this stuff, I had a play around with some examples to see if there was anything funky going on if a reader was to try something slightly different. I’ve been using dplyr for the examples after being thoroughly convinced on Twitter to do so. It’s going well. Mostly.

## if you haven't already done so, load dplyr
suppressPackageStartupMessages(library(dplyr))

My example involved joining together two tibbles containing text values. Nothing too surprising. I wondered though; do numbers behave the way I expect?

Now, a big rule in programming is ‘thou shalt not compare numbers’, and it holds especially true when numbers aren’t exactly integers. This is because representing non-integers is hard, and what you see on the screen isn’t always what the computer sees internally.

Thou shalt not compare numbers.

If I had a tibble where the column I would use to join had integers

dataA <- tribble(
~X, ~Y,
0L, 100L,
1L, 101L,
2L, 102L,
3L, 103L
)
dataA
## # A tibble: 4 x 2
##       X     Y
##   <int> <int>
## 1     0   100
## 2     1   101
## 3     2   102
## 4     3   103

and another tibble with numeric in that column

dataB <- tribble(
~X, ~Z,
0, 1000L,
1, 1001L,
2, 1002L,
3, 1003L
)
dataB
## # A tibble: 4 x 2
##       X     Z
##   <dbl> <int>
## 1     0  1000
## 2     1  1001
## 3     2  1002
## 4     3  1003

would they still join?

full_join(dataA, dataB)
## Joining, by = "X"
## # A tibble: 4 x 3
##       X     Y     Z
##   <dbl> <int> <int>
## 1     0   100  1000
## 2     1   101  1001
## 3     2   102  1002
## 4     3   103  1003

Okay, sure. R treats these as close enough to join. I mean, maybe it shouldn’t, but we’ll work with what we have. R doesn’t always think these are equal

identical(0L, 0)
## [1] FALSE
identical(2L, 2)
## [1] FALSE

though sometimes it does

0L == 0
## [1] TRUE
2L == 2
## [1] TRUE

(== coerces types before comparing). Well, what if one of these just ‘looks like’ the other value (can be coerced to the same?)

dataC <- tribble(
~X, ~Z,
"0", 100L,
"1", 101L,
"2", 102L,
"3", 103L
)
dataC
## # A tibble: 4 x 2
##       X     Z
##   <chr> <int>
## 1     0   100
## 2     1   101
## 3     2   102
## 4     3   103
full_join(dataA, dataC)
## Joining, by = "X"
## Error in full_join_impl(x, y, by$x, by$y, suffix$x, suffix$y, check_na_matches(na_matches)): Can't join on 'X' x 'X' because of incompatible types (character / integer)

That’s probably wise. Of course, R is perfectly happy with things like

"2":"5"
## [1] 2 3 4 5

and == thinks that’s fine

"0" == 0L
## [1] TRUE
"2" == 2L
## [1] TRUE

but who am I to argue?

Anyway, how far apart can those integers and numerics be before they aren’t able to be joined? What if we shift the ‘numeric in name only’ values away from the integers just a teensy bit? .Machine$double.eps is the built-in value for ‘the tiniest number you can produce’. On this system it’s 2.22044610^{-16}. dataBeps <- tribble( ~X, ~Z, 0 + .Machine$double.eps, 1000L,
1 + .Machine$double.eps, 1001L, 2 + .Machine$double.eps, 1002L,
3 + .Machine$double.eps, 1003L ) dataBeps ## # A tibble: 4 x 2 ## X Z ## <dbl> <int> ## 1 2.220446e-16 1000 ## 2 1.000000e+00 1001 ## 3 2.000000e+00 1002 ## 4 3.000000e+00 1003 full_join(dataA, dataBeps)  ## Joining, by = "X" ## # A tibble: 6 x 3 ## X Y Z ## <dbl> <int> <int> ## 1 0.000000e+00 100 NA ## 2 1.000000e+00 101 NA ## 3 2.000000e+00 102 1002 ## 4 3.000000e+00 103 1003 ## 5 2.220446e-16 NA 1000 ## 6 1.000000e+00 NA 1001 Well, that’s… weirder. The values offset from 2 and 3 joined fine, but the 0 and 1 each got multiple copies since R thinks they’re different. What if we offset a little further? dataB2eps <- tribble( ~X, ~Z, 0 + 2*.Machine$double.eps, 1000L,
1 + 2*.Machine$double.eps, 1001L, 2 + 2*.Machine$double.eps, 1002L,
3 + 2*.Machine$double.eps, 1003L ) dataB2eps ## # A tibble: 4 x 2 ## X Z ## <dbl> <int> ## 1 4.440892e-16 1000 ## 2 1.000000e+00 1001 ## 3 2.000000e+00 1002 ## 4 3.000000e+00 1003 full_join(dataA, dataB2eps) ## Joining, by = "X" ## # A tibble: 8 x 3 ## X Y Z ## <dbl> <int> <int> ## 1 0.000000e+00 100 NA ## 2 1.000000e+00 101 NA ## 3 2.000000e+00 102 NA ## 4 3.000000e+00 103 NA ## 5 4.440892e-16 NA 1000 ## 6 1.000000e+00 NA 1001 ## 7 2.000000e+00 NA 1002 ## 8 3.000000e+00 NA 1003 That’s what I’d expect. So, what’s going on? Why does R think those numbers are the same? Let’s check with a minimal example: For each of the values 0:4, let’s compare that integer with the same offset by .Machine$double.eps

suppressPackageStartupMessages(library(purrr)) ## for the 'thou shalt not for-loop' crowd
map_lgl(0:4, ~ as.integer(.x) == as.integer(.x) + .Machine$double.eps) ## [1] FALSE FALSE TRUE TRUE TRUE And there we have it. Some sort of relative difference tolerance maybe? In any case, the general rule to live by is to never compare floats. Add this to the list of reasons why. For what it’s worth, I’m sure this is hardly a surprising detail to the dplyr team. They’ve dealt with things like this for a long time and I’m sure it was much worse before those changes. Update: As noted in the comments, R does have a way to check if things are ‘nearly equal’ (within some specified tolerance) via all.equal() purrr::map_lgl(0:4, ~all.equal(.x, .x + .Machine$double.eps))
## [1] TRUE TRUE TRUE TRUE TRUE

However, this does require the user to either specify the exact tolerance under which they consider two numbers ‘equal’, or to use the default (which, judging by the source of all.equal.numeric() is sqrt(.Machine\$double.eps) or around 1.490116^{-8} on this system). This means that numbers can be ‘quite’ different (depending on what’s an important difference) and still considered equal

> purrr::map_lgl(0:4, ~ all.equal(.x, .x + 1e-8))
## [1] TRUE TRUE TRUE TRUE TRUE

### 6 Responses to “Thou shalt not compare numeric values (except when it works)”

1. Owe says:

Sorry for a nitpick, but that GIF is very annoying, at least on MS IE – it constantly resizes, and moves all the text.

• Sorry, which? This post didn’t have any animated gifs and the only image should be a jpg with specified size.

2. […] article was first published on R – Irregularly Scheduled Programming, and kindly contributed to […]

3. Luke Smith says:

The R Inferno suggests using all.equal() to compare doubles, or to use round() where necessary.

• Thank you! I’ve added a note on all.equal(). One reason to not like that function is that the type returned is varying – logical (TRUE) if they’re ‘equal’, and character if they’re not. Makes mapping the result quite fun.

4. Eric says:

Or you can use near() as in 5.2.1 here: