-
-
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
Erroneous rounding in float arrays when displaying in REPL #6608
Comments
Actually, looking at the code, this seems to be the intended, although it does lead to some user confusion. Closing, unless someone wants to debate the wisdom of this behaviour? showcompact(io::IO, x::Float64) =
(Base._limit_output::Bool) ? _show(io, x, PRECISION, 6, false) : _show(io, x, SHORTEST, 0, false)
|
I've not looked at the display code before, but 666667.0 is 7 significant figures, not 6. I would have expected six digit precision to be |
@jiahao: +1 to that
Can we open this issue back up? |
I agree that adding the .0 doesn't quite comply with the honest printing policy. The minimal fix for this is trivial...
... but it has bigger consequences. Now all float representations (truncated or not) only have a trailing period, instead of
|
This is clearly a bit of a hack and should be improved. |
Is the main reason for calling |
Yeah, it's mainly so that it printing arrays of e.g. random floats doesn't end up showing only one or two columns. It might be good to do something adaptive instead – i.e. only show shortened numbers when there's actually a lack of screen space, but that's a more complicated issue. I suspect that your Grisu implementation may very well be useful. |
Want to make sure this one doesn't get lost. Printing numbers is pretty important. |
This is fixed by Jacob's pure Julia implementation of grisu, but I think that's a pretty risky thing to merge right before a release. |
I'm not so sure. I'm sure it can help having a bit more finer-grained control over grisu, but this seems more like a broader design issue of "showing" things. In grisu, there's the notion of
Given the grisu architecture, we could try:
There are also other ideas to consider, has there's been talk of wanting to revamp the whole |
Yup, this is tough. Basically, what we want here is
In my cursory looking, I don't think that part b is exposed by the GRISU c library we're currently calling, but I may be mistaken. |
I don't think I get your exception case @mbauman, can you give an example? |
Sure. My thinking is that I'm turning the problem on its head. Instead of |
I'm not sure why the integer one can't be just |
Visually ambiguous wasn't the correct choice of words. I meant that |
For everything except show/repr we could potentially omit the period. Usually it's nice to clarify that something is a float though. |
I also think we should use what @quinnj calls "fixed printing" above: using a fixed number of digits after the period for all numbers in the array, and add padding in front of the number to align its period with that of the one with the larger number of digits. |
Counterargument: When array elements are at different orders of magnitude, I've found the "print everyone the same with a single scaling factor" approach that MATLAB uses to be extremely frustrating. An easy-to-remember "please just show me the whole matrix with enough precision" command would be helpful if we're going down this route. |
@pao R switches between standard and scientific notation depending on which one takes less width for the wanted precision, with a customizable factor favoring the former. It could be an interesting solution. |
Does that work per-element, though? For the general case with fixed digits, MATLAB does the same thing as R it sounds like--the |
In R, the same display format is used for all elements in a column of an array, presumably to ensure the output is aligned. Is that what you were asking? It would make sense IMHO to use the same format for all elements in an array instead, since you may want to compare elements across different columns. |
Here's a concrete example: how should |
I'd say like this: julia> [1 e; 0 eps()]
2x2 Array{Float64,2}: (rounded to 5 decimal places)
1.00000e+00 2.71828e+00
0.000000+00 2.22045e-16 |
what if we were to simply alter the printing code to refuse to ever get into this ambiguous situation? $ git diff
diff --git a/base/grisu.jl b/base/grisu.jl
index e6f3fb0..694e7b1 100644
--- a/base/grisu.jl
+++ b/base/grisu.jl
@@ -71,7 +71,7 @@ function _show(io::IO, x::FloatingPoint, mode, n::Int, typed, nanstr, infstr)
end
end
neg && write(io,'-')
- if pt <= -4 || pt > 6 # .00001 to 100000.
+ if pt <= -4 || pt > 6 || pt >= n # .00001 to 100000.
# => #.#######e###
write(io, pdigits, 1)
write(io, '.') this changes the algorithm to prefer scientific notation instead of printing trailing zero. it is therefore more accurate in all cases where it applies, except when (edit: this proposed patch also fixed #9072) |
@vtjnash I like it 👍 |
before:
after:
any preference on whether I also implement the complex expression above to preserve the "before" behavior for the array above? |
… rounded them, use scientific notation instead (fix JuliaLang#6608, fix JuliaLang#9072)
from: JuliaIO/JSON.jl#61
The text was updated successfully, but these errors were encountered: