Julia Multiplication Matrix Reverse Rounding Error

julia> A = [1 2; 3 4]
2x2 Array{Int64,2}:
 1  2
 3  4

julia> A * inv(A)
2x2 Array{Float64,2}:
 1.0          0.0
 8.88178e-16  1.0

Why is the result in the lower left corner 8.88178e-16and not 0.0like in the upper right corner? How to perform an operation to get a result [1.0 0.0; 0.0 1.0].

Is there a way to display the result at will, but actually perform the operation on the symbol, as it would be possible in muPad or some other symbolic evaluator?

+4
source share
1 answer

As mentioned in the comments, the best way is to use Rational. Ex. A = Rational{Int}[1 2;3 4], To get the result in the desired format A*float(inv(A)). Credits for @mschauer.

, , , . , . . .

Julia , . - , . :

julia> A = Rational{Int}[1 2;3 4] 2x2 Array{Rational{Int64},2}: 1//1 2//1 3//1 4//1

julia> A*(inv(A)) 2x2 Array{Rational{Int64},2}: 1//1 0//1 0//1 1//1

, , .

julia> float(A*(inv(A))) 2x2 Array{Float64,2}: 1.0 0.0 0.0 1.0

, . , , , . , , , , .

+4

Source: https://habr.com/ru/post/1609724/


All Articles