Operation does not match

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

-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?

Correct answer is:

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
1 Like

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> (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

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