Skip to content

Commit

Permalink
Implement near.
Browse files Browse the repository at this point in the history
Fixes #1607.
  • Loading branch information
hadley committed Mar 7, 2016
1 parent 97e656d commit 0ebdf60
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Collate:
'lead-lag.R'
'location.R'
'manip.r'
'near.R'
'nth-value.R'
'order-by.R'
'over.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ export(mutate_each_q)
export(n)
export(n_distinct)
export(n_groups)
export(near)
export(nth)
export(ntile)
export(nycflights13_postgres)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# dplyr 0.4.3.9000

* new `near(x, y)` is a helper for `abs(x, y) < tol` (#1607).

* new `as_data_frame.tbl_cube()` (#1563, @krlmlr).

* new parameters `indexes` and `unique_indexes` to `compute()` (#1499, @krlmlr).
Expand Down
15 changes: 15 additions & 0 deletions R/near.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#' Compare two numeric vectors.
#'
#' This is a safe way of comparing if two vectors of floating point numbers
#' are (pairwise) equal. This is safer than using \code{==}, because it has
#' a built in tolerance
#'
#' @param x,y Numeric vectors to compare
#' @param tol Tolerance of comparison.
#' @export
#' @examples
#' sqrt(2) ^ 2 == 2
#' near(sqrt(2) ^ 2, 2)
near <- function(x, y, tol = .Machine$double.eps ^ 0.5) {
abs(x - y) < tol
}
23 changes: 23 additions & 0 deletions man/near.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tests/testthat/context-near.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
context("near")

test_that("near accepts nearby fp values", {
expect_true(near(sqrt(2) ^ 2, 2))
})

0 comments on commit 0ebdf60

Please sign in to comment.