Request: I can't understand why a result of matrix multiplication in Julia like this

Hello, I’m a student using Julia in my project making deep learning model.
I usually use ‘matrix multiplication’ a lot in my code.
then, I found some result is weird.
I think it’s better to show some examples than to explain.

julia> temp = randn(3,3)
3×3 Array{Float64,2}:
 -0.354025  -0.12966    0.178489
 -1.26627   -0.648564  -1.39475
 -1.86577    0.401252   1.16448

julia> temp1 = rand(3,2)
3×2 Array{Float64,2}:
 0.346108  0.895262
 0.852009  0.508729
 0.201902  0.831357

julia> out = temp * temp1
3×2 Array{Float64,2}:
 -0.196965   -0.234519
 -1.27245    -2.62313
 -0.0687769  -0.498126

julia> out[1] # result_1
-0.19696523721990802

julia> sum(temp[1,:] .* temp1[:,1]) # result_2
-0.19696523721990805

as far as I know, ‘the result_1’ and the ‘result_2’ have to be same.
I don’t know why the result like this.

Floating point numbers have finite precision, so they cannot represent all possible values. That means that doing math on floating point numbers will introduce small errors when the results need to be rounded to the nearest value that can be represented as a floating-point number. Those errors will be different depending on exactly how the calculations are ordered or combined, so you may get slightly different results by computing the same value in two different ways. This is normal and expected for all floating-point math in any programming language.

You can learn more about the topic here: PSA: floating-point arithmetic

8 Likes

oh…I see. Thank you for answering.:slight_smile: