Hacker News new | past | comments | ask | show | jobs | submit login

Here's an example of them working together rather effortlessly:

    using Unitful, IntervalArithmetic
    const m = u"m"
    const k = 2π/10m

    f(x) = cos(k * x)/(k^2 - 1/x^2)

    julia> f((10.0 ± 0.1)m)
    [2.5924, 2.60024] m^2
If I try to put in a dimensionally incorrect argument to f, I get an error:

    julia> f((10 ± 0.01)u"s")
    ERROR: MethodError: no method matching cos(::Quantity{Interval{Float64},𝐓*𝐋^-1,Unitful.FreeUnits{(m^-1, s),𝐓*𝐋^-1,nothing}})
    Closest candidates are:
      cos(::Float16) at math.jl:1085
      cos(::Complex{Float16}) at math.jl:1086
      cos(::Missing) at math.jl:1138
      ...
This isn't the most friendly error message in the world, but that can certainly be worked on and with a bit of patience it's pretty clear that it's saying that the problem was that it tried to compute the cosine of a unitful quantity which is nonsense.

_____________________________________

Unfortunately I had to use unitful intervals (i.e. (10.0 ± 0.1)m) instead of intervals of uniful quantities (i.e. 10.0m ± 0.1m) because IntervalArithmetic.jl demands that it takes real quantities, but Unitful quantities are subtypes of Number instead of Real.




Just as a followup, I just realized that the Measurements.jl package does know how to deal with non-real uncertainties, so one can instead do

    using Unitful, Measurements
    const m = u"m"
    const k = 2π/10m

    f(x) = cos(k * x)/(k^2 - 1/x^2)

    julia> f(10.0m ± 0.1m)
    (2.5989 ± 0.0014) - (0.0 ± 0.16)im m^2




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: