Skip to content

Commit 74cea69

Browse files
committed
Merge pull request #130 from JuliaLang/isapprox
compat for JuliaLang/julia#12472
2 parents e324d63 + cbdebe3 commit 74cea69

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ Currently, the `@compat` macro supports the following syntaxes:
5757

5858
* `typealias AbstractString String` - `String` has been renamed to `AbstractString` [#8872](https://github.com/JuliaLang/julia/pull/8872)
5959

60+
* `typealias AbstractFloat FloatingPoint` - `FloatingPoint` has been renamed to `AbstractFloat` [#12162](https://github.com/JuliaLang/julia/pull/12162)
61+
6062
* `typealias AssertionError ErrorException` - `AssertionError` was introduced in [#9734](https://github.com/JuliaLang/julia/pull/9734); before `@assert` threw an `ErrorException`
6163

6264
* For all unsigned integer types to their equivalents with uppercase `I`. [#8907](https://github.com/JuliaLang/julia/pull/8907)
@@ -84,6 +86,8 @@ Currently, the `@compat` macro supports the following syntaxes:
8486
Julia 0.4 precompilation information to be provided (with no effect in earlier versions).
8587
(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.)
8688

89+
* `isapprox(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.
90+
8791
## Renamed functions
8892

8993
* `itrunc`, `iround`, `iceil`, `ifloor` are now accessed via `trunc(T, x)`, etc. [#9133](https://github.com/JuliaLang/julia/pull/9133)

src/Compat.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,4 +543,27 @@ if VERSION < v"0.4.0-dev+6506"
543543
export include_dependency
544544
end
545545

546+
if VERSION < v"0.4.0-dev+6425"
547+
typealias AbstractFloat FloatingPoint
548+
export AbstractFloat
549+
end
550+
551+
if VERSION < v"0.4.0-dev+6068"
552+
Base.real{T<:Real}(::Type{T}) = T
553+
Base.real{T<:Real}(::Type{Complex{T}}) = T
554+
end
555+
556+
if VERSION < v"0.4.0-dev+6578"
557+
rtoldefault{T<:AbstractFloat}(::Type{T}) = sqrt(eps(T))
558+
rtoldefault{T<:Real}(::Type{T}) = 0
559+
rtoldefault{T<:Number,S<:Number}(x::Union(T,Type{T}), y::Union(S,Type{S})) = rtoldefault(promote_type(real(T),real(S)))
560+
function Base.isapprox{T<:Number,S<:Number}(x::AbstractArray{T}, y::AbstractArray{S}; rtol::Real=rtoldefault(T,S), atol::Real=0, norm::Function=vecnorm)
561+
d = norm(x - y)
562+
return isfinite(d) ? d <= atol + rtol*max(norm(x), norm(y)) : x == y
563+
end
564+
const = isapprox
565+
(x,y) = !(x y)
566+
export ,
567+
end
568+
546569
end # module

test/runtests.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,3 +412,11 @@ Compat.@irrational mathconst_one 1.0 big(1.)
412412

413413
@test nothing === __precompile__(false) # tests should never be precompiled
414414
@test nothing === include_dependency("foo")
415+
416+
@test real(Int) == real(Complex{Int}) == Int
417+
418+
@test isa(1.2, AbstractFloat)
419+
420+
@test [1,2,3] [1,2,3+1e-9]
421+
@test [0,1] [1e-9, 1]
422+
@test [0,1] [1e-3, 1]

0 commit comments

Comments
 (0)