| decimal_truth {tinycodet} | R Documentation |
Safer Decimal Number (In)Equality Testing Operators
Description
The %d==%, %d!=% %d<%, %d>%, %d<=%, %d>=% (in)equality operators
perform decimal (type "double") number truth testing.
They are virtually equivalent to the regular (in)equality operators,
==, !=, <, >, <=, >=,
except for 2 aspects:
The decimal number (in)equality operators assume that if the absolute difference between any 2 numbers
xandyis smaller than the Machine tolerance,sqrt(.Machine$double.eps), thenxandyshould be consider to be equal.
For example:(0.1 * 7) == 0.7returnsFALSE, even though they are equal, due to the way decimal numbers are stored in programming languages like 'R' and 'Python'.
But(0.1 * 7) %d==% 0.7returnsTRUE.
Only numeric input is allowed, so characters are not coerced to numbers.
I.e.1 < "a"givesTRUE, whereas1 %d<% "a"gives an error.
For character equality testing, see %s==% from the 'stringi' package.
Thus these operators provide safer decimal number (in)equality tests.
There are also the x %d{}% bnd and x %d!{}% bnd operators,
where bnd is a vector of length 2,
or a 2-column matrix (nrow(bnd)==length(x) or nrow(bnd)==1).
The x %d{}% bnd operator checks if x
is within the closed interval with bounds defined by bnd.
The x %d!{}% bnd operator checks if x
is outside the closed interval with bounds defined by bnd.
Moreover, the function is_wholenumber() is added, to safely test for whole numbers.
Usage
x %d==% y
x %d!=% y
x %d<% y
x %d>% y
x %d<=% y
x %d>=% y
x %d{}% bnd
x %d!{}% bnd
is_wholenumber(x, tol = sqrt(.Machine$double.eps))
Arguments
x, y |
numeric vectors, matrices, or arrays. |
bnd |
either a vector of length 2, or a matrix with 2 columns and 1 row,
or else a matrix with 2 columns where |
tol |
a single, strictly positive number close to zero, giving the tolerance. |
Value
For the %d...% operators:
A logical vector with the same dimensions as x,
indicating the result of the element by element comparison.
NOTE: Inf by Inf and -Inf by -Inf comparisons with
the %d...% operators return NA.
For is_wholenumber():
A logical vector with the same dimensions as x,
indicating the result of the element by element comparison.
NOTE: Inf, -Inf, NaN and NA all return NA for is_wholenumber().
See Also
Examples
x <- c(0.3, 0.6, 0.7)
y <- c(0.1 * 3, 0.1 * 6, 0.1 * 7)
print(x)
print(y)
x == y # gives FALSE, but should be TRUE
x != y # gives TRUE, should be FALSE
x > y # not wrong
x < y # gives TRUE, should be FALSE
# same as above, but here the results are correct:
x %d==% y # correct
x %d!=% y # correct
x %d<% y # correct
x %d>% y # correct
x %d<=% y # correct
x %d>=% y # correct
# check if numbers are in closed interval:
x <- c(0.3, 0.6, 0.7)
bnd <- cbind(x - 0.1, x + 0.1)
x %d{}% bnd
x %d!{}% bnd
# These operators work for integers also:
x <- 1L:5L
y <- 1L:5L
x %d==% y
x %d!=% y
x %d<% y
x %d>% y
x %d<=% y
x %d>=% y
x <- 1L:5L
y <- x + 1L
x %d==% y
x %d!=% y
x %d<% y
x %d>% y
x %d<=% y
x %d>=% y
x <- 1L:5L
y <- x - 1L
x %d==% y
x %d!=% y
x %d<% y
x %d>% y
x %d<=% y
x %d>=% y
# is_wholenumber:
is_wholenumber(1:10 + c(0, 0.1))