How to display Float64 with more digits

I can’t find any reasonable solution to this seemingly simple issue. I want to make arrays of Float64 print with all their digits in the REPL. What method do I need to redefine for the array below to be printed with as many digits as the scalar?

julia> randn(2)
2-element Array{Float64,1}:
 1.38958 
 0.337895

julia> randn()
-1.2296217328819647

This has proven a surprisingly hard question to google, even though I know I have seen a discussion about this before.

2 Likes

A quick hack (overwriting the method to ignore the compact directive in the IOContext):

<< Base.show(io::IO, x::Union{Float64,Float32}) = Base.Grisu._show(io, x, Base.Grisu.SHORTEST, 0, true, false)
WARNING: Method definition show(IO, Union{Float64, Float32}) in module Grisu at grisu/grisu.jl:120 overwritten in module Main at REPL[0]:1.

<< rand(5)
>> 5-element Array{Float64,1}:
 0.41685881323027596
 0.43774537729371676
 0.8202714955342147 
 0.04893977866057142
 0.10743689580376481
2 Likes

Rather than hacks, you can use:

julia> show(IOContext(STDOUT, :compact=>false), "text/plain", randn(2))
2-element Array{Float64,1}:
  1.3718634685435964 
 -0.13288472215889663

That is, you write to an IOContext stream with the :compact property set to false.

10 Likes

My assumption was that this was supposed to be a “default” setting, e.g. work in the REPL for arrays returned from functions etc.

See also https://github.com/JuliaLang/julia/issues/6493 … as I commented in that thread, it would be really trivial to implement a way to control the options passed by the REPL to its default IOContext, if we can just agree on the API.

1 Like

Something like https://github.com/JuliaLang/julia/pull/2716?

I use

showfull(io, x) = show(IOContext(io; compact = false, limit = false), x)
showfull(x) = showfull(STDOUT, x)

I find it so useful, especially for quick dumping of data, that I put it in my personal library.

7 Likes

Thanks for your answers! I was looking for a quick hack, to get many significant digits to copy into a unit test.
I often did use the format long command in matlab, so a simple, supported way of changing this at runtime would be highly appreciated!

1 Like

Just for the record, I found that for Array{Array{Float64}} and other kinds of nestings the best solution is the one provided by @kristoffer.carlsson:

julia> aa = map(x->rand(2), 1:30);

julia> show(aa)
Array{Float64,1}[[0.0213247, 0.948015], [0.138881, 0.40988], [0.607954, 0.247514], [0.961001, 0.800016], [0.795015, 0.196375], [0.419876, 0.210042], [0.147419, 0.596204], [0.655934, 0.33775], [0.261628, 0.0353036], [0.519283, 0.739367], [0.230638, 0.424647], [0.688784, 0.783928], [0.044198, 0.903389], [0.586573, 0.00641704], [0.277042, 0.507606], [0.101339, 0.800158], [0.666822, 0.733885], [0.87061, 0.571897], [0.332233, 0.938371], [0.869072, 0.480484], [0.51722, 0.170909], [0.580332, 0.808131], [0.759806, 0.740258], [0.698577, 0.111267], [0.240175, 0.627679], [0.550143, 0.461072], [0.973468, 0.718771], [0.0565576, 0.264779], [0.817806, 0.608844], [0.628226, 0.437315]]
julia> Base.show(io::IO, x::Union{Float64,Float32}) = Base.Grisu._show(io, x, Base.Grisu.SHORTEST, 0, true, false)
WARNING: Method definition show(IO, Union{Float64, Float32}) in module Grisu at grisu/grisu.jl:120 overwritten in module Main at REPL[3]:1.

julia> show(aa)
Array{Float64,1}[[0.021324667397920383, 0.9480149215510971], [0.138880834453613, 0.40987972158543085], [0.6079541294971065, 0.24751390731387568], [0.9610008104594108, 0.8000157649253907], [0.7950146270582541, 0.19637533429629594], [0.4198755613318861, 0.21004159997572347], [0.14741911239782546, 0.5962040289294155], [0.6559335926380139, 0.33775026650517614], [0.26162809258798947, 0.03530358291261915], [0.5192830984181518, 0.7393666634519216], [0.23063800042570692, 0.4246473510698796], [0.6887842499592063, 0.7839281077852538], [0.04419801672455703, 0.9033893804858726], [0.5865728668047527, 0.006417043034595338], [0.2770423434991949, 0.5076055668589032], [0.1013389015445827, 0.800157991794983], [0.6668216663063329, 0.7338845843658275], [0.8706098748990936, 0.571896899630201], [0.33223293436102663, 0.9383705916674328], [0.8690723007002836, 0.480483678396469], [0.5172200765245383, 0.1709090967694149], [0.5803315111363412, 0.808131364624961], [0.7598063175372176, 0.7402582072192914], [0.6985771670962562, 0.11126665093090793], [0.24017453001037214, 0.6276793732495389], [0.5501427472686542, 0.4610718061669279], [0.9734682038049125, 0.7187708095005967], [0.05655757945487383, 0.2647792748418938], [0.817805627665227, 0.6088441410524292], [0.6282263186278678, 0.43731495814963006]]

This is really helpful!

1 Like

This thread comes top when searching for “Julia display more digits in REPL”, but some of the suggestions are outdated. Likewise for the second-highest thread.

As of Julia 1.6, Base.Grisu was moved to the Grisu.jl package. This is mostly for backward-compatibility. I’d be grateful if you would share the Post-1.6 recommended approach to have more digits displayed in the REPL. And, as an extra, would be grateful if you would state whether the same method work for displaying digits inline. Thanks! :smiley:

1 Like

This is what I’ve been using, but I don’t know any better:

using Printf
Base.show(io::IO, f::Float64) = @printf(io, "%1.10f", f)
1 Like

That may not be surprising as the topic is 4 years old at this point.

I think that Discourse warns you about replying to old topics, please kindly consider that and only revive these if you have a compelling reason.

If you have a question, feel free to start a new topic.

1 Like

A similar solution was posted by @Seif_Shebl here.

To further increase the number of printed digits, say to 100, one can use BigFloats:

using Printf
Base.show(io::IO, f::BigFloat) = @printf(io, "%.100f", f)

julia> BigFloat(pi)
3.1415926535897932384626433832795028841971693993751058209749445923078164062861980294536250318213496467
2 Likes

I know. It’s a tricky one. As I wrote, This thread comes top when searching for “Julia display more digits in REPL”. I’ll start a new discussion.

Use the function big:

big.(rand(2))
1 Like

I had to change some things for this to work, btw. Thanks for the snippet!

showfull(io, x) = show(IOContext(io, :compact => false, :limit => false), x)
showfull(x) = showfull(stdout, x)

Julia’s println() truncating digits caused precision errors for me when using the data in other programs later on. I needed full precision.

If you are saving floating-point data for use in other programs, I would think about using a standard file format, which should take care of the precision issue. e.g. HDF5.jl for a binary format (the most compact way to save at full precision), or CSV via DelimitedFiles.jl or CSV.jl, or JSON via JSON.jl, or …