From eed1bb96a40fe35cad2a543b2e32878341b74a3b Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Fri, 3 Mar 2017 16:38:59 +0000 Subject: [PATCH] allow tolerance args for isapprox --- src/Unitful.jl | 9 +++++---- test/runtests.jl | 5 +++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Unitful.jl b/src/Unitful.jl index 359fee5d..9eb3339d 100644 --- a/src/Unitful.jl +++ b/src/Unitful.jl @@ -786,10 +786,11 @@ isless(x::Number, y::Quantity) = _isless(promote(x,y)...) .<=(x::Number, y::Quantity) = x <= y end -isapprox{T,D,U}(x::Quantity{T,D,U}, y::Quantity{T,D,U}) = isapprox(x.val, y.val) -isapprox(x::Quantity, y::Quantity) = isapprox(uconvert(unit(y), x).val, y.val) -isapprox(x::Quantity, y::Number) = isapprox(uconvert(NoUnits, x), y) -isapprox(x::Number, y::Quantity) = isapprox(y,x) +isapprox{T,D,U}(x::Quantity{T,D,U}, y::Quantity{T,D,U}; atol=zero(Quantity{real(T),D,U}), kwargs...) = + isapprox(x.val, y.val; atol=atol.val, kwargs...) +isapprox(x::Quantity, y::Quantity; kwargs...) = isapprox(uconvert(unit(y), x).val, y.val; kwargs...) +isapprox(x::Quantity, y::Number; kwargs...) = isapprox(uconvert(NoUnits, x), y; kwargs...) +isapprox(x::Number, y::Quantity; kwargs...) = isapprox(y,x; kwargs...) function isapprox{T1,D,U1,T2,U2}(x::AbstractArray{Quantity{T1,D,U1}}, y::AbstractArray{Quantity{T2,D,U2}}; rtol::Real=Base.rtoldefault(T1,T2), diff --git a/test/runtests.jl b/test/runtests.jl index 07a79834..9bd03c64 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -394,6 +394,11 @@ end @test frexp(1.5m) == (0.75m, 1.0) @test unit(nextfloat(0.0m)) == m @test unit(prevfloat(0.0m)) == m + + @test isapprox(1.0u"m", 1.1u"m"; atol=0.2u"m") + @test !isapprox(1.0u"m", 1.1u"m"; atol=0.05u"m") + @test isapprox(1.0u"m", 1.1u"m"; rtol=0.2) + @test !isapprox(1.0u"m", 1.1u"m"; rtol=0.05) end @testset "> fastmath" begin