decimal_truth {tinycodet} | R Documentation |
The %d==%, %d!=% %d<%, %d>%, %d<=%, %d>=%
(in)equality operators
perform decimal (class "double") number truth testing.
They are virtually equivalent to the regular (in)equality operators,
==, !=, <, >, <=, >=
,
except for two aspects:
The decimal number (in)equality operators assume that
if the absolute difference between any two numbers
x
and y
is smaller than the Machine tolerance,
sqrt(.Machine$double.eps)
,
then x
and y
should be consider to be equal.
For example: 0.1*7 == 0.7
returns FALSE
, 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.7
returns TRUE
.
Only numeric input is allowed, so characters are not coerced to numbers.
I.e. 1 < "a"
gives TRUE
, whereas 1 %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
.
x %d==% y
x %d!=% y
x %d<% y
x %d>% y
x %d<=% y
x %d>=% y
x %d{}% bnd
x %d!{}% bnd
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 |
A logical vector with the same dimensions as x
,
indicating the result of the element by element comparison.
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
x %d==% y # here it's done correctly
x %d!=% y # correct
x %d<% y # correct
x %d>% y # correct
x %d<=% y # correct
x %d>=% y # correct
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+1
x %d==% y
x %d!=% y
x %d<% y
x %d>% y
x %d<=% y
x %d>=% y
x <- 1L:5L
y <- x-1
x %d==% y
x %d!=% y
x %d<% y
x %d>% y
x %d<=% y
x %d>=% y