This doesn’t help with the solution you’re looking for, but for the issue of which one is larger- it’s always the one in scientific notation, because the printing only switches to it for large values.
Edit: at least I think so- I always assumed this is how it works but I’m not sure that’s written anywhere…
I haven’t touched this module in quite a while.
I’ll try to revisit that example sometime this week. I should definitively at least update to the readme to point to juliarc.jl → startup.jl.
I recommend against overwriting show() as it is somewhat of a base function, and changing it might have unexpected repercussions in other modules elsewhere.
It is much safer to overwrite the Base.display() function, as it applies only to results displayed to the REPL (aka Julia “console”). REPL output is meant for human consumption, and is not typically read back by machine.
For more details on the display/print/show/… call stack, you might want to take a look here:
(Note that display calls either print() or show()… but I did not draw it on that diagram).
Unfortunately, Julia will not change how it displays vectors if you overwrite the Base.display(r::REPL.REPLDisplay, v::Union{Float32,Float64}) method.
That’s because the call tree used to display vectors does not call display() on individual elements of the array/vector.
Here is an example of what you could define to customize the display() of numeric Vectors.
import REPL
using NumericIO
function Base.display(r::REPL.REPLDisplay, v::Vector)
N = length(v)
println(REPL.outstream(r.repl), N, "-element ", typeof(v), ":")
for v_i in v
println(formatted(REPL.outstream(r.repl), :SI, ndigits=4), v_i)
end
end
And what about Arrays?
"Array"s seem to use a more elaborate algorithm to display its multi-dimensional contents while maintaining good readability. You would have to experiment a bit to get your display(::Array, ...) function to produce pleasant results.
Comments
I honestly think we should consider re-evaluating how the Julia call tree display()s vectors & arrays so we can more easily switch out how numbers are displayed on the REPL.
What you are requesting sounds fairly reasonable to me - but I’m not convinced the solution is that trivial.
Indeed: repr()is an example of unintended consequences - but let me paint a clearer picture…
More generally:
Overwriting the show(::IO, ::Float64) method means you are modifying how pretty much ALL code EVERYWHERE where Julia functions print()Float64 values. This doesn’t just affect the REPL output.
That means that if some code somewhere was trying to print out 4.2e-6 to file… then it would end up writing “0.0” instead of “4.2e-6” (The normal behaviour).
Just like that, your one line of code broke pretty much any other function that needed to accurately write out fractional values that are significantly smaller than one to an ::IO device (like a file).
Maybe that’s ok if you write a tiny little custom program that doesn’t really rely on other functions… Maybe.
That being said: I strongly recommend you don’t do this unless you really just want a quick and dirty solution for your (throw-away) Julia-as-a-line-calculator session.
I wrote this function for this purpose. I keep it in my startup.jl . If I knew Julia better, I would make this into another dispatch of the @printf macro or, more to the point in this case, the show() function
"""
printfa = print format array
"""
function printfa(A, fmt="%7.3f")
print("[")
if ndims(A) == 1
for i in range(1, stop=length(A))
@eval @printf($fmt, $A[$i])
if i != length(A)
print(", ")
end
end
else
m, n = size(A)
for i in range(1, stop=m), j in range(1, stop=n)
@eval @printf($fmt, $A[$i, $j])
if j < n
print(" ")
elseif j == n && i < m
print("; \n")
end
end
end
println("]")
end