Skip to content

Commit 28fa123

Browse files
committed
Allow constructing measurement with symbol-like types
1 parent 81dabe1 commit 28fa123

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1010
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
1111
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
1212
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
13+
SymbolicsBase = "13454fcc-48b2-433e-829a-0418001dc7cb"
1314

1415
[compat]
1516
Calculus = "0.4.1, 0.5"
1617
RecipesBase = "0.6.0, 0.7, 0.8, 1.0"
1718
Requires = "0.5.0, 1"
19+
SymbolicsBase = "0.1.0"
1820
julia = "1"
1921

2022
[extras]

src/Measurements.jl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ __precompile__()
2121

2222
module Measurements
2323

24+
# We want to special-case symbolic variables, which requires detecting them.
25+
using SymbolicsBase
26+
2427
# Calculus is used to calculate numerical derivatives in "@uncertain" macro.
2528
using Calculus
2629
using Statistics
@@ -91,9 +94,16 @@ function __init__()
9194
end
9295

9396
measurement(x::Measurement) = x
94-
measurement(val::T) where {T<:AbstractFloat} = Measurement(val, zero(T), UInt64(0), empty_der2(val)) # FIXME
95-
measurement(val::Real) = measurement(float(val)) # FIXME
96-
function measurement(val::T, err::T) where {T<:AbstractFloat} # FIXME
97+
function measurement(val::T) where {T<:Real}
98+
if !(T<:AbstractFloat) && !SymbolicsBase.issymbollike(val)
99+
return measurement(float(val))
100+
end
101+
return Measurement(val, zero(T), UInt64(0), empty_der2(val))
102+
end
103+
function measurement(val::T, err::T) where {T<:Real}
104+
if !(T<:AbstractFloat) && !SymbolicsBase.issymbollike(val)
105+
return measurement(promote(float(val), float(err))...)
106+
end
97107
newder = empty_der2(val)
98108
if iszero(err)
99109
Measurement{T}(val, err, UInt64(0), newder)
@@ -102,7 +112,7 @@ function measurement(val::T, err::T) where {T<:AbstractFloat} # FIXME
102112
return Measurement{T}(val, err, tag, Derivatives(newder, (val, err, tag)=>one(T)))
103113
end
104114
end
105-
measurement(val::Real, err::Real) = measurement(promote(float(val), float(err))...) # FIXME
115+
measurement(val::Real, err::Real) = measurement(promote(val, err)...)
106116
measurement(::Missing, ::Union{Real,Missing} = missing) = missing
107117
const ± = measurement
108118

0 commit comments

Comments
 (0)