Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Julia: Is there a way to test for equality like R's all.equal()?

Tags:

julia

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?

like image 624
Nick Avatar asked Jan 01 '16 07:01

Nick


1 Answers

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

like image 137
Dan Getz Avatar answered Oct 19 '22 22:10

Dan Getz