Skip to content

Commit

Permalink
feat: Add near()
Browse files Browse the repository at this point in the history
Closes #36
  • Loading branch information
nathaneastwood committed Jun 7, 2020
1 parent fb07dd5 commit 44579f6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: poorman
Type: Package
Title: A Poor Man's Base R Copy of 'dplyr' Verbs
Version: 0.2.0.19
Version: 0.2.0.20
Authors@R: person("Nathan", "Eastwood", "", "[email protected]",
role = c("aut", "cre"))
Maintainer: Nathan Eastwood <[email protected]>
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export(min_rank)
export(mutate)
export(n)
export(n_distinct)
export(near)
export(ntile)
export(num_range)
export(peek_vars)
Expand Down
16 changes: 16 additions & 0 deletions R/near.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#' 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 `==`, because it has a built in tolerance.
#'
#' @param x,y Numeric vectors to compare
#' @param tol Tolerance of comparison.
#'
#' @examples
#' sqrt(2) ^ 2 == 2
#' near(sqrt(2) ^ 2, 2)
#'
#' @export
near <- function(x, y, tol = .Machine$double.eps^0.5) {
abs(x - y) < tol
}
4 changes: 4 additions & 0 deletions inst/tinytest/test_near.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
expect_true(
near(sqrt(2)^2, 2),
info = "near() accepts nearby fp values"
)
22 changes: 22 additions & 0 deletions man/near.Rd

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

0 comments on commit 44579f6

Please sign in to comment.