Skip to content

Commit

Permalink
isapprox now tests max(atol,rtol*...) rather than atol+rtol*...
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed Jul 29, 2017
1 parent 1ce3c8e commit 124296d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ Library improvements
* `@test isequal(x, y)` and `@test isapprox(x, y)` now prints an evaluated expression when
the test fails ([#22296]).

* `isapprox(x,y)` now tests `norm(x-y) <= max(atol, rtol*max(norm(x), norm(y)))`
rather than `norm(x-y) <= atol + ...`.

* Uses of `Val{c}` in `Base` has been replaced with `Val{c}()`, which is now easily
accessible via the `@pure` constructor `Val(c)`. Functions are defined as
`f(::Val{c}) = ...` and called by `f(Val(c))`. Notable affected functions include:
Expand Down
4 changes: 2 additions & 2 deletions base/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ end
"""
isapprox(x, y; rtol::Real=sqrt(eps), atol::Real=0, nans::Bool=false, norm::Function)
Inexact equality comparison: `true` if `norm(x-y) <= atol + rtol*max(norm(x), norm(y))`. The
Inexact equality comparison: `true` if `norm(x-y) <= max(atol, rtol*max(norm(x), norm(y)))`. The
default `atol` is zero and the default `rtol` depends on the types of `x` and `y`. The keyword
argument `nans` determines whether or not NaN values are considered equal (defaults to false).
Expand Down Expand Up @@ -233,7 +233,7 @@ true
```
"""
function isapprox(x::Number, y::Number; rtol::Real=rtoldefault(x,y), atol::Real=0, nans::Bool=false)
x == y || (isfinite(x) && isfinite(y) && abs(x-y) <= atol + rtol*max(abs(x), abs(y))) || (nans && isnan(x) && isnan(y))
x == y || (isfinite(x) && isfinite(y) && abs(x-y) <= max(atol, rtol*max(abs(x), abs(y)))) || (nans && isnan(x) && isnan(y))
end

const = isapprox
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,7 @@ function isapprox(x::AbstractArray, y::AbstractArray;
atol::Real=0, nans::Bool=false, norm::Function=vecnorm)
d = norm(x - y)
if isfinite(d)
return d <= atol + rtol*max(norm(x), norm(y))
return d <= max(atol, rtol*max(norm(x), norm(y)))
else
# Fall back to a component-wise approximate comparison
return all(ab -> isapprox(ab[1], ab[2]; rtol=rtol, atol=atol, nans=nans), zip(x, y))
Expand Down

0 comments on commit 124296d

Please sign in to comment.