Last chance! 50% off unlimited learning
Sale ends in
neps*eps
.
Note that x == 0
will not work in all cases.
By default eps
is the smallest possible floating point value
that can be represented by the running machine, i.e.
.Machine$double.eps
and neps
is one.
By changing neps
it is easy to adjust how close to zero "close"
means without having to know the machine precision (or remembering how
to get it).## S3 method for class 'default':
isZero(x, neps=1, eps=.Machine$double.eps, ...)
vector
of values.eps
specifying how close to zero
"close" means. If eps
is the smallest value such that
1 + eps != 1
, i.e. .Machine$double.eps
, neps
must
be greater or equall.equal
().
Comparison
.
.Machine
.x <- 0
print(x == 0) # TRUE
print(isZero(x)) # TRUE
x <- 1
print(x == 0) # FALSE
print(isZero(x)) # FALSE
x <- .Machine$double.eps
print(x == 0) # FALSE
print(isZero(x)) # FALSE
x <- 0.9*.Machine$double.eps
print(x == 0) # FALSE
print(isZero(x)) # TRUE
# From help(Comparisions)
x1 <- 0.5 - 0.3
x2 <- 0.3 - 0.1
print(x1 - x2)
print(x1 == x2) # FALSE on most machines
print(identical(all.equal(x1, x2), TRUE)) # TRUE everywhere
print(isZero(x1-x2)) # TRUE everywhere
Run the code above in your browser using DataLab