Skip to content
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

Closed
MartinOtter opened this issue Oct 21, 2020 · 4 comments
Closed

How to print with more as 3 digits? #78

MartinOtter opened this issue Oct 21, 2020 · 4 comments

Comments

@MartinOtter
Copy link

MartinOtter commented Oct 21, 2020

In the following example:

using Measurements;
a = 1.23456789012345 ± 0.123456789012345
println("a = $a")

# results in :
a = 1.23 ± 0.12

Sorry, but I did not find a way to easily print a measurement variable with more as 3 digits.

@giordano
Copy link
Member

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.

@MartinOtter
Copy link
Author

MartinOtter commented Oct 21, 2020

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:

Compact representation (needs to be adapted):
1.23456789012345 ± 0.123456789012345

Standard representation of Float numbers
1.23457 ± 0.123457

Standard representation of Float numbers
1.23456789012345 ± 0.123456789012345

Via print: Compact representation (needs to be adapted):
1.23456789012345 ± 0.123456789012345

Via print: Standard representation of Float numbers
1.23457 ± 0.123457

Via print: Standard representation of Float numbers
1.23456789012345 ± 0.123456789012345

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:

println(stdout2, "Via print: $v2\n")

@giordano
Copy link
Member

giordano commented Feb 1, 2021

@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

?

@MartinOtter
Copy link
Author

Good solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants