Skip to content

Commit

Permalink
add tests, fix type constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Christ authored and Simon Christ committed Jun 27, 2019
1 parent 043470a commit a8a400c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/quantities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -283,20 +283,20 @@ _rounderr(f) = error("Specify the type of the quantity to convert to with $f. ",
if VERSION >= v"1"
# convenience methods
round(u::Units, q::Quantity, r::RoundingMode=RoundNearest; kwargs...) = round( numtype(q), u, q, r; kwargs...)
round(::Type{T}, u::Units, q::Quantity, r::RoundingMode=RoundNearest; kwargs...) = round(Quantity{T, dimension(q), typeof(u)}, q, r; kwargs...)
round(::Type{T}, u::Units, q::Quantity, r::RoundingMode=RoundNearest; kwargs...) where {T<:Number} = round(Quantity{T, dimension(q), typeof(u)}, q, r; kwargs...)
# working horse methods
round(x::AbstractQuantity, r::RoundingMode=RoundNearest; kwargs...) = _rounderr(:round)
round(x::DimensionlessQuantity, r::RoundingMode=RoundNearest; kwargs...) = round(uconvert(NoUnits, x), r; kwargs...)
round(::Type{T}, x::AbstractQuantity, r=RoundingMode=RoundNearest; kwargs...) where {T <: Integer} = _dimerr(:round)
round(::Type{T}, x::DimensionlessQuantity, r::RoundingMode=RoundNearest; kwargs...) where {T <: Integer} = round(T, uconvert(NoUnits, x), r; kwargs...)
round(::Type{T}, x::Quantity, r::RoundingMode=RoundNearest; kwargs...) where {T <: Integer} = T(round(uconvert(unit(T),x).val, r; kwargs...))
round(::Type{T}, x::Quantity, r::RoundingMode=RoundNearest; kwargs...) where {T <: Quantity} = T(round(ustrip(uconvert(unit(T),x)), r; kwargs...))
# that should actually be fixed in Base ↓
trunc(x::AbstractQuantity; kwargs...) = round(x, RoundToZero; kwargs...)
floor(x::AbstractQuantity; kwargs...) = round(x, RoundDown; kwargs...)
ceil(x::AbstractQuantity; kwargs...) = round(x, RoundUp; kwargs...)
trunc(t::Type{T}, x::AbstractQuantity; kwargs...) where {T<:Integer} = round(t, x, RoundToZero)
floor(t::Type{T}, x::AbstractQuantity) where {T<:Integer} = round(t, x, RoundDown)
ceil(t::Type{T}, x::AbstractQuantity) where {T<:Integer} = round(t, x, RoundUp)
trunc(::Type{T}, x::AbstractQuantity; kwargs...) where {T<:Number} = round(T, x, RoundToZero; kwargs...)
floor(::Type{T}, x::AbstractQuantity; kwargs...) where {T<:Number} = round(T, x, RoundDown; kwargs...)
ceil(::Type{T}, x::AbstractQuantity; kwargs...) where {T<:Number} = round(T, x, RoundUp; kwargs...)
else
for f in (:floor, :ceil, :trunc, :round)
@eval ($f)(x::AbstractQuantity; digits=0) = _dimerr($f)
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,8 @@ end
@test round(typeof(1mm), 1.0314m) === 1031mm
@test round(typeof(1.0mm), 1.0314m) === 1031.0mm
@test round(typeof(1.0mm), 1.0314m; digits=1) === 1031.4mm
@test round(u"inch", 1.0314m) === 41.0u"inch"
@test round(Int, u"inch", 1.0314m) === 41u"inch"
end
end

Expand Down

0 comments on commit a8a400c

Please sign in to comment.