diff --git a/README.md b/README.md index 02c548f7f..769711843 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,8 @@ Currently, the `@compat` macro supports the following syntaxes: Julia 0.4 precompilation information to be provided (with no effect in earlier versions). (However, to enable precompiling in 0.4, it is better to explicitly put `VERSION >= v"0.4.0-dev+6521" && __precompile__()` before your `module` statement, so that Julia knows to precompile before anything in your module is evaluated.) +* `isapparox(A, B)` for arrays ([JuliaLang/julia#12472](https://github.com/JuliaLang/julia/pull/12472)), and synonyms `≈` ([U+2248](http://www.fileformat.info/info/unicode/char/2248/index.htm), LaTeX `\approx`) and `≉` ([U+2249](http://www.fileformat.info/info/unicode/char/2249/index.htm), LaTeX `\napprox`) for `isapprox` and `!isapprox`, respectively. + ## Renamed functions * `itrunc`, `iround`, `iceil`, `ifloor` are now accessed via `trunc(T, x)`, etc. [#9133](https://github.com/JuliaLang/julia/pull/9133) diff --git a/src/Compat.jl b/src/Compat.jl index e4463db9e..48850c722 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -543,4 +543,22 @@ if VERSION < v"0.4.0-dev+6506" export include_dependency end +if VERSION < v"0.4.0-dev+6068" + Base.real{T<:Real}(::Type{T}) = T + 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 + end # module diff --git a/test/runtests.jl b/test/runtests.jl index efe9768f6..a43cfe4c9 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -412,3 +412,10 @@ Compat.@irrational mathconst_one 1.0 big(1.) @test nothing === __precompile__(false) # tests should never be precompiled @test nothing === include_dependency("foo") + +@test real(Int) == real(Complex{Int}) == Int + +@test [1,2,3] ≈ [1,2,3+1e-9] +@test [0,1] ≈ [1e-9, 1] +@test [0,Inf] ≈ [0,Inf] +@test [0,Inf] ≉ [0,-Inf]