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

Possible rounding error when substractring two Float64 arrays #8667

Closed
diggiharry opened this issue Oct 12, 2014 · 11 comments
Closed

Possible rounding error when substractring two Float64 arrays #8667

diggiharry opened this issue Oct 12, 2014 · 11 comments

Comments

@diggiharry
Copy link

When substracting a small float from a big float one gets the expected result:

julia> 1. - 10^-7.
0.9999999

Doing the same with an array gives an unexpected result:

[1.,1.] .- [10^-6.,10^-7.]
2-element Array{Float64,1}:
 0.999999
 1.0 

Somehow numbers below 10^-6 get rounded to zero.
Is there something I did wrong or might this be a bug ?

@andreasnoack
Copy link
Member

That is because the display of arrays is different from scalars. The number is the same, e.g.

julia> [1.0] .- [10^-7.]
1-element Array{Float64,1}:
 1.0

julia> ([1.0] .- [10^-7.])[1]
0.9999999

@JeffBezanson
Copy link
Member

Showing Arrays at the prompt, we round elements to 6 digits to save space.

@diggiharry
Copy link
Author

Aah ! Thanks !
Sorry, I got pretty confused over this :)

@StefanKarpinski
Copy link
Member

This is confusing sometimes, but I'm not sure what we could do differently. When you do, e.g. rand(5,5), you really don't want to see all of those digits.

@diggiharry
Copy link
Author

Maybe one could prefix the rounded numbers with ~, to indicate that it is not shown in full precision.

@StefanKarpinski
Copy link
Member

Not a bad idea. It's annoying that it would take up space for every value though. Maybe indicating in the array description that the values are rounded would be good. E.g.:

julia> randn(5,5)
5x5 Array{Float64,2} (values rounded):
 -1.58891    0.0413407  -0.249306   -0.214036     0.822793
 -0.38166    2.42342    -0.537856    0.795775    -0.62094
 -0.398926  -0.929601   -0.0779216  -1.90433      0.471998
  0.744185  -0.642462   -0.381877   -0.84185     -1.24628
 -1.60374    1.63264    -0.889873   -0.00555298  -0.243353

@diggiharry
Copy link
Author

You're right, for big arrays it would propably look ugly with the prefix. Indicating in the array description looks pretty good to me, because that's the first thing I checked when I noticed that the numbers were not what I expected.

@nalimilan
Copy link
Member

Something that may make it more obvious would be to round values to a given number of digits, not of significant digits. Currently, you could believe that the numbers are exact, since some have more digits than others. This would also make the output aligned and more readable.

Indicating that the values are rounded wouldn't hurt, though.

@kmsquire
Copy link
Member

Perhaps something to indicate how the rounding is done?

julia> randn(5,5)
5x5 Array{Float64,2} (rounded to 6 significant digits):
-1.58891 0.0413407 -0.249306 -0.214036 0.822793
-0.38166 2.42342 -0.537856 0.795775 -0.62094
-0.398926 -0.929601 -0.0779216 -1.90433 0.471998
0.744185 -0.642462 -0.381877 -0.84185 -1.24628
-1.60374 1.63264 -0.889873 -0.00555298 -0.243353

On Mon, Oct 13, 2014 at 10:37 AM, Stefan Karpinski <[email protected]

wrote:

Not a bad idea. It's annoying that it would take up space for every value
though. Maybe indicating in the array description that the values are
rounded would be good. E.g.:

julia> randn(5,5)5x5 Array{Float64,2} (values rounded):
-1.58891 0.0413407 -0.249306 -0.214036 0.822793
-0.38166 2.42342 -0.537856 0.795775 -0.62094
-0.398926 -0.929601 -0.0779216 -1.90433 0.471998
0.744185 -0.642462 -0.381877 -0.84185 -1.24628
-1.60374 1.63264 -0.889873 -0.00555298 -0.243353


Reply to this email directly or view it on GitHub
#8667 (comment).

@quinnj
Copy link
Member

quinnj commented Oct 14, 2014

+100 for adding a snippet about rounding values. I think this is just the solution to #6608 that we need.

@JeffBezanson
Copy link
Member

Yes it's a fine idea, but in #6608 the output was actually wrong: it
printed with the wrong number of significant digits given the rounding used.

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

7 participants