Skip to content

Commit

Permalink
Merge pull request #57 from simonbyrne/sb/isapprox
Browse files Browse the repository at this point in the history
allow tolerance args for isapprox
  • Loading branch information
ajkeller34 authored Mar 3, 2017
2 parents 2b72133 + eed1bb9 commit b38843b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Unitful.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b38843b

Please sign in to comment.