Skip to content

Commit

Permalink
add TwoSided
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin committed Feb 1, 2025
1 parent 7a27911 commit 0000000
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Uncertain"
uuid = "b33f403f-199b-472b-b9d0-1cd2092892d0"
authors = ["Alexander Plavin <alexander@plav.in>"]
version = "0.1.3"
version = "0.1.4"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand Down
4 changes: 3 additions & 1 deletion src/Uncertain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ include("types.jl")
include("show.jl")
include("maths.jl")
include("agg.jl")
include("twosided.jl")
include("disambiguation.jl")

""" ±ᵤ(val, unc)
Expand Down Expand Up @@ -54,7 +55,8 @@ export Value, ValueAny, ValueNumber, ValueReal, value, uncertainty, nσ, weighte
using ..Uncertain:
Value, ValueAny, ValueNumber, ValueReal,
value, uncertainty, nσ,
weightedmean
weightedmean,
TwoSided, width, maxdiff
using ..Uncertain: ±ᵤ as ±
end

Expand Down
25 changes: 25 additions & 0 deletions src/twosided.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
""" TwoSided{T}(lo::T, hi::T)
Asymmetric two-sided uncertainty: `lo` is the uncertainty in the lower direction, and `hi` in the upper direction.
Both `lo` and `hi` should be positive.
Uncertainty of `TwoSided(x, x)` should behave like just `x`. Only makes sense for numbers with a natural order.
"""
struct TwoSided{T}
lo::T
hi::T
end

Base.show(io::IO, u::TwoSided) = print(io, "TwoSided(", u.lo, ", ", u.hi, ")")

Base.float(::Type{TwoSided{T}}) where {T} = TwoSided{float(T)}

Base.:*(a::TwoSided, b) = @modify(p -> p * b, a[ₚ])
Base.:*(a, b::TwoSided) = @modify(p -> a * p, b[ₚ])

for f in [:rad2deg, :deg2rad]
@eval Base.$f(a::TwoSided) = @modify($f, a[ₚ])
end

width(a::TwoSided) = a.lo + a.hi
maxdiff(a::TwoSided) = max(a.lo, a.hi)
15 changes: 15 additions & 0 deletions test/twosided.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@testitem "one vs two sided" begin
using Unitful

@test U.width(0.1) == 0.2
@test U.maxdiff(0.1) == 0.1

@test U.width(U.TwoSided(0.1, 0.2)) 0.3
@test U.maxdiff(U.TwoSided(0.1, 0.2)) == 0.2
@test U.maxdiff(U.TwoSided(0.2, 0.1)) == 0.2

@test U.TwoSided(0.1, 0.2) * 2 == U.TwoSided(0.2, 0.4)
@test 2 * U.TwoSided(0.1, 0.2) == U.TwoSided(0.2, 0.4)
@test U.TwoSided(0.1, 0.2) * u"m" == U.TwoSided(0.1u"m", 0.2u"m")
@test 1u"m" * U.TwoSided(0.1, 0.2) == U.TwoSided(0.1u"m", 0.2u"m")
end

2 comments on commit 0000000

@aplavin
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/125389

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.4 -m "<description of version>" 0000000058b865b7d6523fe93a971d6e7675b8d9
git push origin v0.1.4

Also, note the warning: Version 0.1.4 skips over 0.1.3
This can be safely ignored. However, if you want to fix this you can do so. Call register() again after making the fix. This will update the Pull request.

Please sign in to comment.