Skip to content

Commit 0453339

Browse files
committed
Support printing Measurement{Num}
1 parent 28fa123 commit 0453339

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

src/show.jl

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,59 +13,57 @@
1313
#
1414
# This file defines the methods to represent `Measurement` objects in various places.
1515
#
16-
# FIXME: should special-handle non-AbstractFloat T's.
17-
#
1816
### Code:
1917

2018
import Printf
2119

22-
function truncated_print(io::IO, m::Measurement, error_digits::Int;
20+
full_print(io::IO, val, err; atbeg = "", atend = "", pm = "±") =
21+
print(io, atbeg, val, get(io, :compact, false) ? pm : " $pm ", err, atend)
22+
23+
function truncated_print(io::IO, m::Measurement;
2324
atbeg = "", atend = "", pm = "±")
24-
val = if iszero(m.err) || !isfinite(m.err)
25-
m.val
26-
else
27-
err_digits = -Base.hidigit(m.err, 10) + error_digits
28-
digits = if isfinite(m.val)
29-
max(-Base.hidigit(m.val, 10) + 2, err_digits)
25+
error_digits::Int = get(io, :error_digits, 2)
26+
27+
val = m.val
28+
err = m.err
29+
30+
if error_digits < 0
31+
error("`error_digits` must be non-negative")
32+
elseif error_digits > 0 && !SymbolicsBase.issymbollike(val)
33+
val = if iszero(err) || !isfinite(err)
34+
val
3035
else
31-
err_digits
36+
err_digits = -Base.hidigit(err, 10) + error_digits
37+
digits = if isfinite(val)
38+
max(-Base.hidigit(val, 10) + 2, err_digits)
39+
else
40+
err_digits
41+
end
42+
round(val, digits = digits)
3243
end
33-
round(m.val, digits = digits)
34-
end
35-
print(io, atbeg, val,
36-
get(io, :compact, false) ? pm : " $pm ",
37-
round(m.err, sigdigits=error_digits), atend)
44+
err = round(err, sigdigits=error_digits)
45+
end # if
46+
full_print(io, val, err, atbeg = atbeg, atend = atend, pm = pm)
3847
end
3948

40-
full_print(io::IO, measure::Measurement) =
41-
print(io, measure.val, get(io, :compact, false) ? "±" : " ± ", measure.err)
42-
4349
function Base.show(io::IO, m::Measurement)
44-
error_digits = get(io, :error_digits, 2)
45-
if error_digits > 0
46-
truncated_print(io, m, error_digits)
47-
elseif error_digits == 0
48-
full_print(io, m)
49-
else
50-
error("`error_digits` must be non-negative")
51-
end # if
50+
truncated_print(io, m)
5251
end
5352

5453
function Base.show(io::IO, ::MIME"text/latex", measure::Measurement)
55-
error_digits = get(io, :error_digits, 2)
56-
truncated_print(io, measure, error_digits, atbeg = "\$", atend = "\$", pm = "\\pm")
54+
truncated_print(io, measure, atbeg = "\$", atend = "\$", pm = "\\pm")
5755
end
5856

5957
for mime in (MIME"text/x-tex", MIME"text/x-latex")
6058
function Base.show(io::IO, ::mime, measure::Measurement)
61-
error_digits = get(io, :error_digits, 2)
62-
truncated_print(io, measure, error_digits, pm = "\\pm")
59+
truncated_print(io, measure, pm = "\\pm")
6360
end
6461
end
6562

6663
# Representation of complex measurements. Print something that is easy to
6764
# understand and that can be meaningfully copy-pasted into the REPL, at least
6865
# for standard numeric types.
66+
# FIXME: does this handle complex symbolic Measurements correctly?
6967
for mime in (MIME"text/plain", MIME"text/x-tex", MIME"text/x-latex")
7068
function Base.show(io::IO, mtype::mime, measure::Complex{<:Measurement})
7169
r, i = reim(measure)

0 commit comments

Comments
 (0)