diff --git a/DESCRIPTION b/DESCRIPTION index c5aaa1f..98d1995 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", "", "nathan.eastwood@icloud.com", role = c("aut", "cre")) Maintainer: Nathan Eastwood diff --git a/NAMESPACE b/NAMESPACE index af235bf..f9e8111 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -60,6 +60,7 @@ export(min_rank) export(mutate) export(n) export(n_distinct) +export(near) export(ntile) export(num_range) export(peek_vars) diff --git a/R/near.R b/R/near.R new file mode 100644 index 0000000..16c36a4 --- /dev/null +++ b/R/near.R @@ -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 +} diff --git a/inst/tinytest/test_near.R b/inst/tinytest/test_near.R new file mode 100644 index 0000000..b16dd9e --- /dev/null +++ b/inst/tinytest/test_near.R @@ -0,0 +1,4 @@ +expect_true( + near(sqrt(2)^2, 2), + info = "near() accepts nearby fp values" +) diff --git a/man/near.Rd b/man/near.Rd new file mode 100644 index 0000000..bac8901 --- /dev/null +++ b/man/near.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/near.R +\name{near} +\alias{near} +\title{Compare two numeric vectors} +\usage{ +near(x, y, tol = .Machine$double.eps^0.5) +} +\arguments{ +\item{x, y}{Numeric vectors to compare} + +\item{tol}{Tolerance of comparison.} +} +\description{ +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. +} +\examples{ +sqrt(2) ^ 2 == 2 +near(sqrt(2) ^ 2, 2) + +}