# Matrix multiplication and vector multiplication give different results!

**URL:** <https://discourse.julialang.org/t/matrix-multiplication-and-vector-multiplication-give-different-results/130177>\
**Category:** Numerics\
**Created:** [June 24, 2025, 4:27pm UTC](https://discourse.julialang.org/t/matrix-multiplication-and-vector-multiplication-give-different-results/130177 "2025-06-24T16:27:32Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [June 24, 2025, 5:03pm UTC](https://discourse.julialang.org/t/matrix-multiplication-and-vector-multiplication-give-different-results/130177/4 "2025-06-24T17:03:52Z")

</div>

Yes, this is covered in that PSA a bit further down:

> [@PSA: floating-point arithmetic](https://discourse.julialang.org/t/psa-floating-point-arithmetic/8678/8):
>
> Note there are still some ways you might get non-deterministic results:
> 
> - BLAS or LAPACK operations
> - `@fastmath`: which lets the compiler do a _lot_ of manipulations
> - `@simd`: which allows re-association of arithmetic operations to exploit SIMD operations.
> - `muladd`: allows use of either `a*b+c` or `fma(a,b,c)` depending on which is faster. This is a tricky one, as we’re increasingly making use of it (such as the recent libm work).

With lots of stuff in hardware, doing things like vectorization or reassociation (changing the order of operations) will change results.

```julia
julia> v = rand(10000);

julia> sum(v)
5004.669545612722

julia> let s = 0.0
           for x in v
               s += x
           end
           s
       end
5004.669545612701

```

Here’s an easy way to see why this happens:

```julia
julia> (1 + 1e20) - 1e20
0.0

julia> 1 + (1e20 - 1e20)
1.0

```

Matrix multiplication does all sorts of stuff that can change the order of operations, or even use reduced / improved accuracy fma operations, etc. This will naturally cause discrepancies like those you saw.

This is why people are advised to use `isapprox` (`≈`) instead of equality:

```julia
julia> m12[:, 400] == m12col400
false

julia> m12[:, 400] ≈ m12col400
true

```

These two results are equal up to the sorts of resolution guarenteed by the algorithms we use.

---

_[View the full topic](https://discourse.julialang.org/t/matrix-multiplication-and-vector-multiplication-give-different-results/130177)._
