# Operation does not match

**URL:** <https://discourse.julialang.org/t/operation-does-not-match/111210>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [March 5, 2024, 5:25pm UTC](https://discourse.julialang.org/t/operation-does-not-match/111210 "2024-03-05T17:25:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin\_sanchez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin_sanchez/32/207525_2.png) [@martin\_sanchez](https://discourse.julialang.org/u/martin_sanchez)\
**Post date:** [March 5, 2024, 5:25pm UTC](https://discourse.julialang.org/t/operation-does-not-match/111210/1 "2024-03-05T17:25:52Z")

</div>

If I make `A=[2/3 1/3; -1/3 1/3]`. `v=[1,-4]`, and make `A*v` I get

```julia
-0.6666666666666666
 -1.6666666666666665

```

Which is fine except that i I make `-5/3` I get `-1.6666666666666667`. Is this just rounding error being different for `5/3` and `1/3*5`?

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [March 5, 2024, 6:01pm UTC](https://discourse.julialang.org/t/operation-does-not-match/111210/2 "2024-03-05T18:01:49Z")

</div>

Correct answer is:

```julia
julia> -5/3 # or approximately;
-1.6666666666666667

but yes, I believe a rounding error and not surprising, since:

julia> bitstring(-1.6666666666666667)
"1011111111111010101010101010101010101010101010101010101010101011"

julia> bitstring(-1.6666666666666665) # not as close to the correct value in decimal, but as close, or actually closer in binary?!
"1011111111111010101010101010101010101010101010101010101010101010"

but in general you do not expect only one bit off. You can expect correctly rounded for one primitive operation, but when it's matrix multiply, it's no longer one, implicitly more, and adding up, unlike for with rationals:

julia> A=[2//3 1//3; -1//3 1//3]; v=[1,-4];
julia> A*v
2-element Vector{Rational{Int64}}:
 -2//3
 -5//3

```

---

<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:** [March 5, 2024, 7:30pm UTC](https://discourse.julialang.org/t/operation-does-not-match/111210/3 "2024-03-05T19:30:45Z")

</div>

Yes, it’s just roundoff errors. Because floating-point arithmetic may accumulate roundoff errors on every operation, it doesn’t follow the ordinary rules of algebra exactly in general. For example, it isn’t associative:

```julia
julia> (5 * 1) / 3
1.6666666666666667

julia> 5 * (1/3)
1.6666666666666665

```

This is isn’t specific to Julia, but has to do with how computers do arithmetic: see [PSA: floating-point arithmetic](https://discourse.julialang.org/t/psa-floating-point-arithmetic/8678)

PS. I reformatted your post to quote your code to make it easier to read. In the future, see [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530)
