简体   繁体   中英

understanding application of function: x > y - z & x < y + z

I am not particularly great with math and am trying to understand some R code. There is a function called "compareforequality" that looks like this:

compareforequality <- function(val1, val2, epsilon)
{
    val1 = as.numeric(val1);
    val2 = as.numeric(val2);
    equal = val1 > (val2 - epsilon) & val1 < val2 + epsilon;
    equal
}

where val1 and val2 are vector of numbers that signify timepoints (usually integers between -10 and 1000 that identify days in a time series), and epsilon is set to 1e-10. I can see that it will return true/false if the values are the same/different, but what is the application of a function like this instead of using something like identical(). What effect does the value of epsilon have on the comparison?

Thanks,

The point is not for them to be exactly equal, it's to compare for rough equality, as in " val1 is within epsilon of val2 ".

The classic example of the usefulness of something like this is probably floating point numbers, where (for instance) 0.1 + 0.2 != 0.3 , but 0.1 + 0.2 is within epsilon of 0.3 for some small epsilon , which is quite often enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM