# Linear Algebra Rounding Errors?

**URL:** <https://discourse.julialang.org/t/linear-algebra-rounding-errors/29387>\
**Category:** Specific Domains\
**Created:** [October 2, 2019, 2:00am UTC](https://discourse.julialang.org/t/linear-algebra-rounding-errors/29387 "2019-10-02T02:00:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![manderson](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@manderson](https://discourse.julialang.org/u/manderson)\
**Post date:** [October 2, 2019, 2:00am UTC](https://discourse.julialang.org/t/linear-algebra-rounding-errors/29387/1 "2019-10-02T02:00:59Z")

</div>

I’ve run into some confusion about rounding with some linear algebra things which is causing some trouble for me in an image reconstruction algorithm I’m working on. When I run the following example code, I sometimes see that the trace and the two sums are equal as I expect, but just as often one, or all three values will differ. I’m confused as to why this is happening, can anyone enlighten me?

```julia
using LinearAlgebra
A=rand(Float32,100);
T=zeros(Float32,100,100)
for i=1:100
      T[i,i]=A[i]
end
println("-----------------")
println("sum T: ",sum(T))
println("tr T: ",tr(T))
println("sum A: ",sum(A))

```

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [October 2, 2019, 2:38am UTC](https://discourse.julialang.org/t/linear-algebra-rounding-errors/29387/2 "2019-10-02T02:38:50Z")

</div>

Summing floating point numbers is not guaranteed to be exact, as information is lost if the ratio of two numbers is too big (1e10+1e-10, for example). If you depend on the comparison, you can use the `≈` operator

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 2, 2019, 2:48am UTC](https://discourse.julialang.org/t/linear-algebra-rounding-errors/29387/3 "2019-10-02T02:48:23Z")

</div>

Basically, what is happening is that `sum` and `tr` calculations compute the sum in a different order (more precisely, a different associativity), which gives slightly different results in floating-point arithmetic due to different roundoff-error accumulations.

(The `sum` function uses a fancier summation algorithm than a simple loop, called “pairwise” summation, that reduces the accumulated roundoff error.)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 2, 2019, 6:02am UTC](https://discourse.julialang.org/t/linear-algebra-rounding-errors/29387/4 "2019-10-02T06:02:33Z")

</div>

> [@manderson](#):
>
> rounding with some linear algebra things which is causing some trouble for me

In addition to what the others said, it would be useful to know _how_ it is causing trouble for you, so it could be mitigated.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 2, 2019, 6:24am UTC](https://discourse.julialang.org/t/linear-algebra-rounding-errors/29387/5 "2019-10-02T06:24:44Z")

</div>

If you are relying on exact floating point equality, then it’s is possible/probable that you are doing something risky.

---

<div class="post-metadata">

**Author:** ![manderson](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@manderson](https://discourse.julialang.org/u/manderson)\
**Post date:** [October 2, 2019, 8:04pm UTC](https://discourse.julialang.org/t/linear-algebra-rounding-errors/29387/6 "2019-10-02T20:04:24Z")

</div>

Thank you all for your clear responses. This all makes sense to me. I’m working on an iterative image reconstruction algorithm based on ADMM. I am in the process of streamlining some of the math and encountered this when vectorizing some matrix math involving scaled identity matrices. I was checking to make sure that the results matched and was puzzled when they weren’t but differed only by small amounts. To be clear, the algorithm doesn’t rely on any equalities in this way, just my lazy way of checking my math.

Thanks again!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 3, 2019, 7:43am UTC](https://discourse.julialang.org/t/linear-algebra-rounding-errors/29387/7 "2019-10-03T07:43:04Z")

</div>

That’s good to hear 🙂 In that case, you can follow @longemen3000’s suggestion and check out the `isapprox` function (can also be written as `≈`). It is well suited to this sort of checking.
