-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to print with more as 3 digits? #78
Comments
That's kind of similar to #5. I don't know any handy way to select the output format. What you can very manually do is julia> println("a = $(round(Measurements.value(a); digits=3)) ± $(round(Measurements.uncertainty(a); digits=3))")
a = 1.235 ± 0.123 I know this isn't great, but if you have concrete suggestions about how to control formatting, please share. |
I understand that for "usual" output the number of shown digits needs to be related to the digits of the uncertainty. I need it for unit testing (so all digits should be shown). Here is a rough way how this could be accomplished (in order that I can test it, I introduced MyMeasurement): module TestShow
using Measurements
mutable struct MyMeasurement
v::Measurement{Float64}
end
v1 = 1.23456789012345 ± 0.123456789012345
v2 = MyMeasurement(v1)
function Base.show(io::IO, m::MyMeasurement)
compact = get(io, :compact, missing)
if ismissing(compact)
# your current method
# (the following lines are just for testing and would have to be replaced with your current method)
println(io, "Compact representation (needs to be adapted):")
Base.show(io, Measurements.value(m.v))
print(io, " ± ")
Base.show(io, Measurements.uncertainty(m.v))
else
println(io, "Standard representation of Float numbers")
Base.show(io, Measurements.value(m.v))
print(io, " ± ")
Base.show(io, Measurements.uncertainty(m.v))
end
return nothing
end
stdout1 = IOContext(stdout, :compact => true)
stdout2 = IOContext(stdout, :compact => false)
show(stdout , v2); println("\n")
show(stdout1, v2); println("\n")
show(stdout2, v2); println("\n")
println(stdout , "Via print: ", v2, "\n")
println(stdout1, "Via print: ", v2, "\n")
println(stdout2, "Via print: ", v2, "\n")
end This results in the following output:
The user has then the choice to get 3 digits as default, or the standard 6 digits if :compact=true or 16 digits if compact=false. Unfortunately, it seems that interpolation ignores the setting of :compact:
|
@MartinOtter would you be happy with something like julia> using Measurements
julia> a = 1.23456789012345 ± 0.123456789012345
1.23 ± 0.12
julia> println(IOContext(stdout, :error_digits=>3), a)
1.235 ± 0.123
julia> println(IOContext(stdout, :error_digits=>4), a)
1.2346 ± 0.1235
julia> println(IOContext(stdout, :error_digits=>5), a)
1.23457 ± 0.12346
julia> println(IOContext(stdout, :error_digits=>6), a)
1.234568 ± 0.123457 ? |
Good solution |
In the following example:
Sorry, but I did not find a way to easily print a measurement variable with more as 3 digits.
The text was updated successfully, but these errors were encountered: