Skip to content

Commit 7a5708f

Browse files
committed
Refactor and speed up branch of vec_approx_equal NA/NaN testing
1 parent 172c3c1 commit 7a5708f

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

R/patch.R

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,19 @@ vec_approx_equal0 <- function(vec1, vec2, na_equal, abs_tol, inds1 = NULL, inds2
118118
# Matching vec_equal, we ignore names and other attributes.
119119
if (!is.null(inds1)) vec1 <- vec_slice(vec1, inds1)
120120
if (!is.null(inds2)) vec2 <- vec_slice(vec2, inds2)
121-
na_or_nan1 <- is.na(vec1)
122-
na_or_nan2 <- is.na(vec2)
123-
# Since above are bare logical vectors, we can use `fifelse`
124121
if (na_equal) {
122+
na_or_nan1 <- is.na(vec1)
123+
na_or_nan2 <- is.na(vec2)
124+
# Since above are bare logical vectors, we can use `fifelse`
125125
res <- fifelse(
126126
!na_or_nan1 & !na_or_nan2,
127127
abs(vec1 - vec2) <= abs_tol,
128128
na_or_nan1 & na_or_nan2 & (is.nan(vec1) == is.nan(vec2))
129129
)
130130
} else {
131131
# Like `==` and `vec_equal`, we consider NaN == {NA, NaN, anything else}
132-
# to be NA.
133-
res <- fifelse(
134-
!na_or_nan1 & !na_or_nan2,
135-
abs(vec1 - vec2) <= abs_tol,
136-
NA
137-
)
132+
# to be NA. That logic is actually baked into the basic formula:
133+
res <- abs(vec1 - vec2) <= abs_tol
138134
}
139135

140136
if (!is.null(dim(vec1))) {

0 commit comments

Comments
 (0)