# Precision and adding

**URL:** <https://discourse.julialang.org/t/precision-and-adding/7690>\
**Category:** General Usage\
**Created:** [December 11, 2017, 12:08pm UTC](https://discourse.julialang.org/t/precision-and-adding/7690 "2017-12-11T12:08:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [December 11, 2017, 12:08pm UTC](https://discourse.julialang.org/t/precision-and-adding/7690/1 "2017-12-11T12:08:21Z")

</div>

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:

```julia
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.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [December 11, 2017, 12:18pm UTC](https://discourse.julialang.org/t/precision-and-adding/7690/2 "2017-12-11T12:18:13Z")

</div>

> <https://stackoverflow.com/questions/588004/is-floating-point-math-broken>

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [December 11, 2017, 12:24pm UTC](https://discourse.julialang.org/t/precision-and-adding/7690/3 "2017-12-11T12:24:45Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [December 11, 2017, 12:30pm UTC](https://discourse.julialang.org/t/precision-and-adding/7690/4 "2017-12-11T12:30:22Z")

</div>

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
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
julia> [0.1+0.2, 1.]
2-element Array{Float64,1}:
 0.30000000000000004
 1.0      

```

**Both platform** :

```julia
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:

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

```

?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [December 11, 2017, 12:33pm UTC](https://discourse.julialang.org/t/precision-and-adding/7690/5 "2017-12-11T12:33:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [December 11, 2017, 12:35pm UTC](https://discourse.julialang.org/t/precision-and-adding/7690/6 "2017-12-11T12:35:23Z")

</div>

> [@Christopher\_Fisher](#):
>
> It occurs in Atom, but not the REPL.

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.
