Take this R
example:
> x = 0.5 - 0.3
> y = 0.3 - 0.1
> x == y # although mathematically TRUE, it’s FALSE for limited precision
[1] FALSE
> all.equal(x,y) # equal up to precision of computer
[1] TRUE
To quote from R documentation:
‘all.equal(x, y)’ is a utility to compare R objects ‘x’ and ‘y’ testing ‘near equality’. If they are different, comparison is still made to some extent, and a report of the differences is returned. Do not use ‘all.equal’ directly in ‘if’ expressions-either use ‘isTRUE(all.equal(....))’ or ‘identical’ if appropriate.
In Julia
, x == y
will return false
. Is there a way to check this equality up to machine precision in Julia?
isapprox(x,y)
is what you are looking for.
use ?isapprox
in the REPL for additional help. Specifically, 2 parameters which specify relative tolerance for error and absolute tolerance for error.
Happy 2016
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With