Precision and adding

I am using Julia .6.1 and get a odd result when adding across columns of an array with specific values. Consider:

x = [.10 .20;.10 .30]

x[:,1] + x[:,2]

Output:

Float64[2]
0.30000000000000004
0.400

Julia .5 provides the correct output. Interestingly, in .6.1 sum(x,2) works and x with different values also works.

1 Like

Thanks for the link. It appears that the problem is even more specific than I initially thought. It occurs in Atom, but not the REPL.

Do you have any advice for avoiding these situations?

There is still something interesting how is array represented on screen. I tested in on REPL:

Julia 0.6.1, OS: Linux (x86_64-linux-gnu):

julia> [0.1+0.2, 1.]
2-element Array{Float64,1}:
 0.3
 1.0

Julia 0.7.0-DEV.2815, OS: Linux (x86_64-linux-gnu):

julia> [0.1+0.2, 1.]
2-element Array{Float64,1}:
 0.30000000000000004
 1.0      

Both platform:

julia> 0.1 + 0.2
0.30000000000000004

julia> repr([0.1+0.2, 1.])
"[0.3, 1.0]"

julia> show([0.1+0.2, 1.])
[0.3, 1.0]

julia> [0.1+0.2, 1.][1]
0.30000000000000004

Is difference here:

ccall(:jl_toplevel_eval_in, Any, (Any, Any), m, e)

?

The value is the same, but the number of significant digits printed has in some situations changed between 0.6 and 0.7.

1 Like

The result is the same, printing methods are different.

If you worry about printing results, you can format them as you want:

@printf "%.2f" pi

If you want to truncate numbers to have specific precision, you can round them:

round(pi, 2)

Although I doubt you even need to do it in practice.

2 Likes