Skip to content

Commit 586d784

Browse files
authored
Merge pull request JuliaLang#309 from JuliaLang/anj/compat
Compat.isapprox method with nans keyword argument
2 parents e96a5e8 + 9ea34db commit 586d784

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ Currently, the `@compat` macro supports the following syntaxes:
106106

107107
* `.&` 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)
108108

109+
* `Compat.isapprox` with `nans` keyword argument [#20022](https://github.com/JuliaLang/julia/pull/20022)
110+
109111
## Renamed functions
110112

111113
* `pointer_to_array` and `pointer_to_string` have been replaced with `unsafe_wrap(Array, ...)` and `unsafe_wrap(String, ...)` respectively

src/Compat.jl

+9-13
Original file line numberDiff line numberDiff line change
@@ -935,19 +935,6 @@ if VERSION < v"0.4.0-dev+6068"
935935
Base.real{T<:Real}(::Type{Complex{T}}) = T
936936
end
937937

938-
if VERSION < v"0.4.0-dev+6578"
939-
rtoldefault{T<:AbstractFloat}(::Type{T}) = sqrt(eps(T))
940-
rtoldefault{T<:Real}(::Type{T}) = 0
941-
rtoldefault{T<:Number,S<:Number}(x::Union(T,Type{T}), y::Union(S,Type{S})) = rtoldefault(promote_type(real(T),real(S)))
942-
function Base.isapprox{T<:Number,S<:Number}(x::AbstractArray{T}, y::AbstractArray{S}; rtol::Real=rtoldefault(T,S), atol::Real=0, norm::Function=vecnorm)
943-
d = norm(x - y)
944-
return isfinite(d) ? d <= atol + rtol*max(norm(x), norm(y)) : x == y
945-
end
946-
const = isapprox
947-
(x,y) = !(x y)
948-
export ,
949-
end
950-
951938
# Use as Compat.Linspace to get improved linspace in both 0.3 and 0.4
952939
if VERSION < v"0.4.0-dev+6110"
953940
include("linspace.jl")
@@ -1774,4 +1761,13 @@ if VERSION >= v"0.5.0-dev+5509" && VERSION < v"0.6.0-dev.1614"
17741761
include_string(".|(xs...) = broadcast(|, xs...)")
17751762
end
17761763

1764+
if VERSION < v"0.6.0-dev.2093" # Compat.isapprox to allow for NaNs
1765+
using Base.rtoldefault
1766+
function isapprox(x::Number, y::Number; rtol::Real=rtoldefault(x,y), atol::Real=0, nans::Bool=false)
1767+
x == y || (isfinite(x) && isfinite(y) && abs(x-y) <= atol + rtol*max(abs(x), abs(y))) || (nans && isnan(x) && isnan(y))
1768+
end
1769+
else
1770+
import Base.isapprox
1771+
end
1772+
17771773
end # module

test/runtests.jl

+4
Original file line numberDiff line numberDiff line change
@@ -1599,3 +1599,7 @@ A = view(rand(5,5), 1:3, 1:3)
15991599
@test [true, false] .& [true, true] == [true, false]
16001600
@test [true, false] .| [true, true] == [true, true]
16011601
end
1602+
1603+
# julia#20022
1604+
@test !Compat.isapprox(NaN, NaN)
1605+
@test Compat.isapprox(NaN, NaN, nans=true)

0 commit comments

Comments
 (0)