-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
function to change numeric output format #6493
Comments
See also MATLAB's |
This is more appropriate for the IJulia repository. IPython magics were last discussed in JuliaLang/IJulia.jl#12 but the issue was closed due to lack of interest. |
I think there's a scope question--is this something we'd like to support in the native REPL, or only in IJulia? |
I don't think this is IJulia-specific at all. This is something we need to address generally for all kinds of UIs. It's fine to have a global setting that changes the way things are printed interactively, but I think this is rather bad practice for anything non-interactive. |
If the question turn into printing in general... See also: http://docs.sympy.org/dev/modules/printing.html |
An exception is code producing documents automatically from a mix of text content and Julia code, like IJulia but for LaTex, ODF, etc. You likely want to specify once for all how numbers should be printed in that case. That probably can be considered as "interactive", since you immediately get to see the result. |
Is there currently any workaround to universally change the format of numeric output? |
Can something be done here with the new |
It is certainly technically possible, although I'm not sure that it is necessarily the right solution. At some level, this moves from printing of a value, to formatting of the value. That suggests (to me at least) that the initial output maybe should some sort of rich document rather than a string, then combine that with a style-sheet to make the final output. I think that level of detail would be more appropriate for a package however. That's a yak-shave I've considered pursuing further, but more pressing issues have been holding my attention. So I'll certainly go along with it if someone wants to add this to IOContext, for lack of any solid alternative to propose right now. |
There's NumericIO.jl which provides some of this features and maybe could help. |
Sorry for doing this again, but I have gone through alot of conversations on the subject, and would like to make a little synopsis before moving forward. I realize some of what I say probably does not match the accepted picture at the moment. I am trying to fill in the gaps using my own experience with Julia, hoping it might help readers (and hopefully not hinder): High-level differentiations:
Some basic relationships
For example:
I know in Julia v0.5, we replaced
Instead, I believe the
As stated before, the display mechanism applies in a more general context (not limited to Take for example the idea of displaying a plot object created with Plots.jl:
|
As for the comment from @fbruetting: NumericIO.jl is not a solution to the problem in general, but could be used to improve the control of Julia's numeric output:
Of course, some things would have to be modified to conform with my description above:
|
The most basic thing that is needed here is just to change the REPL display function so that it passes To the extent that |
Any final conclusion about this? |
Dusting off an old issue here; I think @stevengj, as usual, has the right idea here: some mechanism (global |
For accessing the REPL's |
Did this mature to the point that there is a concrete example of how to adjust the number of significant digits? I share the complaint that others have expressed previously, |
I'm sure the issue is much more complex than I'm aware (since I haven't read all the posts here), but I just wanted to share my quick, simplistic solution that was sufficient for my needs: using Printf
Base.show(io::IO, x::Float64) = write(io, @sprintf("%.2f", x)) It's fairly straightforward to turn this into a macro: macro format(ex)
quote
Base.show(io::IO, x::Float64) = write(io, @sprintf($ex, x))
end
end which can then be called as follows: julia> @format "%.2f"
julia> rand(3, 3)
3×3 Array{Float64,2}:
0.64 0.68 0.98
0.85 0.41 0.49
0.42 0.45 0.96 |
That is a temporary manual solution. I need something which isn't a macro for this. Why can't there be a regular function which dynamically accepts a format string? |
What's wrong with a macro? |
@chakravala, feel free to review/chime in on #32859, which allows doing |
@OliverEvans96 the issue with a macro arises from long-term support. A macro gets the job done, but it quickly turns to a headache for future code maintainers. Couple this with the fact that macros exist in isolation and it's easy to see that they will be orphaned and bit rot on a long-enough horizon. I think the desire is to have something officially supported so that it's a tier one feature. |
Why can we not just do the following?
|
I want change output format for numbers (stand alone floats, float matrix,
BigFloat
s). I can do it in IPython by using%precision
magic function. I didn't find any equivalent in Julia.I know that I can use
round
but it actually returns different number. It's also cumbersome to convert arrays with combinationmap
+round
. One another problem: I didn't get how to change real or output precision ofBigFloat
:set_bigfloat_precision
changes precision for all newBigFloat
s,copy
anddeepcopy
don't help (they copy value irrespective of new defaultBigFloat
precision), naive precision change viaBigFloat.prec
corrupts it.I know, that I handle lot's of different topics in this question and I as a programmer understand that lot's of difficulties lays in output formats. But my friend, mathematician asked me: "Why can I just type %precision in my IPython notebook but not in Julia? Isn't output format change essential feature?".
The text was updated successfully, but these errors were encountered: