Skip to content

Commit

Permalink
Add method for rem2pi
Browse files Browse the repository at this point in the history
Fix #27.
  • Loading branch information
giordano committed Nov 23, 2018
1 parent 2b4f6ae commit 6d28c99
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
History of Measurements.jl
==========================

v1.1.0 (2018-1?-??)
-------------------

### New Features

* New method for `rem2pi`
([#29](https://github.com/JuliaPhysics/Measurements.jl/issues/29)).

v1.0.2 (2018-09-26)
-------------------

Expand Down
16 changes: 11 additions & 5 deletions src/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -902,13 +902,18 @@ Base.mod(a::Measurement, b::Measurement) = a - fld(a, b)*b
Base.mod(a::Measurement, b::Real) = result(mod(a.val, b), 1, a)
Base.mod(a::Real, b::Measurement) = result(mod(a, b.val), -fld(a, b.val), b)

Base.# Use definition of "rem" function:
Base.# http://docs.julialang.org/en/stable/manual/mathematical-operations/#division-functions
# Use definition of "rem" function:
# https://docs.julialang.org/en/latest/manual/mathematical-operations/#Division-functions-1
Base.rem(a::Measurement, b::Measurement) = a - div(a, b)*b
Base.rem(a::Measurement, b::Real) = result(rem(a.val, b), 1, a)
Base.rem(a::Real, b::Measurement) = result(rem(a, b.val), -div(a, b.val), b)
Base.rem(a::Measurement, b::Union{Measurement,Float64}, ::RoundingMode{:Nearest}) =
a - b * round(a / b, RoundNearest)
Base.rem(a::Float64, b::Measurement, ::RoundingMode{:Nearest}) =
a - b * round(a / b, RoundNearest)

Base.mod2pi(a::Measurement) = result(mod2pi(a.val), 1, a)
Base.rem2pi(a::Measurement, r::RoundingMode) = result(rem2pi(a.val, r), 1, a)

### Machine precision

Expand All @@ -924,9 +929,10 @@ Base.typemax(::Type{Measurement{T}}) where {T<:AbstractFloat} = typemax(T)

### Rounding

Base.round(a::Measurement; kwargs...) =
measurement(round(value(a); kwargs...), round(uncertainty(a); kwargs...))
Base.round(::Type{T}, a::Measurement) where {T<:Integer} = round(T, a.val)
Base.round(a::Measurement, r::RoundingMode=RoundNearest; kwargs...) =
measurement(round(value(a), r; kwargs...), round(uncertainty(a); kwargs...))
Base.round(::Type{T}, a::Measurement, r::RoundingMode=RoundNearest) where {T<:Integer} =
round(T, a.val, r)
Base.floor(a::Measurement) = measurement(floor(a.val))
Base.floor(::Type{T}, a::Measurement) where {T<:Integer} = floor(T, a.val)
Base.ceil(a::Measurement) = measurement(ceil(a.val))
Expand Down
15 changes: 15 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -495,12 +495,27 @@ end
@test @inferred(mod(-5.8, a)) @inferred(-5.8 - fld(-5.8, a) * a)
@test @inferred(rem(a, -3.7)) @inferred(a + div(a, -3.7) * 3.7)
@test @inferred(rem(-4.9, a)) @inferred(-4.9 - div(-4.9, a) * a)
@test @inferred(rem(a, 4.27, RoundNearest))
a - 4.27 * round(a / 4.27, RoundNearest)
@test @inferred(rem(-5.1, a, RoundNearest))
-5.1 - a * round(-5.1 / a, RoundNearest)
@test @inferred(rem(a, -1.2, RoundToZero)) rem(a, -1.2)
@test @inferred(rem(3.45, a, RoundToZero)) rem(3.45, a)
@test @inferred(rem(a, 6.7, RoundDown)) mod(a, 6.7)
@test @inferred(rem(-8.9, a, RoundDown)) mod(-8.9, a)
@test @inferred(rem(a, -3.14, RoundUp)) mod(a, 3.14)
@test @inferred(rem(2.718, a, RoundUp)) mod(2.718, -a)
for b in (10w, x, y)
@test @inferred(mod(a, b)) a - fld(a, b)*b
@test @inferred(rem(a, b)) a - div(a, b)*b
@test @inferred(rem(a, b, RoundNearest)) a - b * round(a / b, RoundNearest)
end
end
@test @inferred(mod2pi(pi*x)) measurement(pi, 0.1*pi)
for r in (RoundNearest, RoundToZero, RoundUp, RoundDown), a in (3x, 5y, 10w)
@test @inferred(rem2pi(a, r)) rem(a, 2pi, r)
@test rem2pi(a, r) a - 2pi * round(a / (2pi), r)
end
end

@testset "Machine precisionx" begin
Expand Down

0 comments on commit 6d28c99

Please sign in to comment.