Reputation: 5898
I'd like to take a set of predictions and round them to machine tolerance, then calculate a hash from those numbers. The goal is to determine whether a complicated R process which generates those numbers has been altered. Each substantive change to the codebase to the process will generate these numbers, round them them, then compute the hash so identicalness of the process can be easily determined and tracked in a database.
I'm familiar with all.equal()
, but using all.equal()
would require every set of numbers to be compared with every other set of numbers directly in R--whereas what I'm envisioning the comparison of identicalness to a large set of process versions can be done just based on hashes in a table.
So my question is how would I round a numeric vector in R to a precision exactly comparable to the precision used in all.equal
?
i.e., let's name the rounding function that accomplishes what I want machine.round
:
identical(machine.round(x),machine.round(y))
is TRUE
if and only if all.equal(x,y)
is TRUE
.
what is the definition of machine.round
?
#two vectors which are considered equal by all.equal but not by identical
x <- c(.3, .4 - .1, .5 - .2, .6 - .3, .7 - .4)
y <- c(.3, .3, .3, .3, .3)
all.equal(x,y)
identical(x,y)
#two vectors which are considered unequal by all.equal and identical:
x2 <- c(.30001, .4 - .1, .5 - .2, .6 - .3, .7 - .4)
y2 <- c(.3, .3, .3, .3, .3)
all.equal(x2,y2)
identical(x2,y2)
I want a function machine.round()
that returns TRUE
for identical(machine.round(x),machine.round(y))
but FALSE
for identical(machine.round(x2),machine.round(y2))
Upvotes: 1
Views: 214
Reputation: 66844
The default tolerance used by all.equal
can be found by examinating its formal arguments:
formals(all.equal.numeric)$tolerance
sqrt(.Machine$double.eps)
Which evaluates to:
eval(formals(all.equal.numeric)$tolerance)
[1] 1.490116e-08
However, I don't think rounding then comparing is the correct approach. You should be checking closeness within this tolerance as all.equal
does.
For example:
abs(x-y)<sqrt(.Machine$double.eps)
[1] TRUE TRUE TRUE TRUE TRUE
abs(x2-y2)<sqrt(.Machine$double.eps)
[1] FALSE TRUE TRUE TRUE TRUE
Upvotes: 1