Skip to content

Commit

Permalink
Compat.isapprox method with nans keyword argument
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasnoack committed Jan 20, 2017
1 parent e96a5e8 commit 9ea34db
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ Currently, the `@compat` macro supports the following syntaxes:

* `.&` and `.|` are short syntax for `broadcast(&, xs...)` and `broadcast(|, xs...)` (respectively) in Julia 0.6 (only supported on Julia 0.5 and above) [#17623](https://github.com/JuliaLang/julia/pull/17623)

* `Compat.isapprox` with `nans` keyword argument [#20022](https://github.com/JuliaLang/julia/pull/20022)

## Renamed functions

* `pointer_to_array` and `pointer_to_string` have been replaced with `unsafe_wrap(Array, ...)` and `unsafe_wrap(String, ...)` respectively
Expand Down
22 changes: 9 additions & 13 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -935,19 +935,6 @@ if VERSION < v"0.4.0-dev+6068"
Base.real{T<:Real}(::Type{Complex{T}}) = T
end

if VERSION < v"0.4.0-dev+6578"
rtoldefault{T<:AbstractFloat}(::Type{T}) = sqrt(eps(T))
rtoldefault{T<:Real}(::Type{T}) = 0
rtoldefault{T<:Number,S<:Number}(x::Union(T,Type{T}), y::Union(S,Type{S})) = rtoldefault(promote_type(real(T),real(S)))
function Base.isapprox{T<:Number,S<:Number}(x::AbstractArray{T}, y::AbstractArray{S}; rtol::Real=rtoldefault(T,S), atol::Real=0, norm::Function=vecnorm)
d = norm(x - y)
return isfinite(d) ? d <= atol + rtol*max(norm(x), norm(y)) : x == y
end
const = isapprox
(x,y) = !(x y)
export ,
end

# Use as Compat.Linspace to get improved linspace in both 0.3 and 0.4
if VERSION < v"0.4.0-dev+6110"
include("linspace.jl")
Expand Down Expand Up @@ -1774,4 +1761,13 @@ if VERSION >= v"0.5.0-dev+5509" && VERSION < v"0.6.0-dev.1614"
include_string(".|(xs...) = broadcast(|, xs...)")
end

if VERSION < v"0.6.0-dev.2093" # Compat.isapprox to allow for NaNs
using Base.rtoldefault
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))
end
else
import Base.isapprox
end

end # module
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1599,3 +1599,7 @@ A = view(rand(5,5), 1:3, 1:3)
@test [true, false] .& [true, true] == [true, false]
@test [true, false] .| [true, true] == [true, true]
end

# julia#20022
@test !Compat.isapprox(NaN, NaN)
@test Compat.isapprox(NaN, NaN, nans=true)

0 comments on commit 9ea34db

Please sign in to comment.