What is the difference between “==” and “===” comparison operators in Julia?

前端 未结 2 710
礼貌的吻别
礼貌的吻别 2021-02-05 07:40

What is the difference between == and === comparison operators in Julia?

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-05 08:11

    === means that it's actually the same object, i.e. the variables point to the same spot in memory. == means that the objects have the same values. For example:

    julia> A = rand(5,5) #Make an array
    5x5 Array{Float64,2}:
     0.349193  0.408216   0.703084  0.163128   0.815687
     0.211441  0.0185634  0.378299  0.0734293  0.187445
     0.667637  0.139323   0.286794  0.359962   0.229784
     0.476224  0.49812    0.648244  0.831006   0.1787
     0.960756  0.488886   0.195973  0.148958   0.200619
    
    julia> B = A # This sets the pointer of B to the pointer of A
    5x5 Array{Float64,2}:
     0.349193  0.408216   0.703084  0.163128   0.815687
     0.211441  0.0185634  0.378299  0.0734293  0.187445
     0.667637  0.139323   0.286794  0.359962   0.229784
     0.476224  0.49812    0.648244  0.831006   0.1787
     0.960756  0.488886   0.195973  0.148958   0.200619
    
    julia> B === A # Same spot in memory
    true
    
    julia> B[1,1]=2 #Change a value of B
    2
    
    julia> B
    5x5 Array{Float64,2}:
     2.0       0.408216   0.703084  0.163128   0.815687
     0.211441  0.0185634  0.378299  0.0734293  0.187445
     0.667637  0.139323   0.286794  0.359962   0.229784
     0.476224  0.49812    0.648244  0.831006   0.1787
     0.960756  0.488886   0.195973  0.148958   0.200619
    
    julia> A #Also changes A since they point to the same spot
    5x5 Array{Float64,2}:
     2.0       0.408216   0.703084  0.163128   0.815687
     0.211441  0.0185634  0.378299  0.0734293  0.187445
     0.667637  0.139323   0.286794  0.359962   0.229784
     0.476224  0.49812    0.648244  0.831006   0.1787
     0.960756  0.488886   0.195973  0.148958   0.200619
    
    julia> B = copy(A) #Now make B a copy of A, no longer the same pointer
    5x5 Array{Float64,2}:
     2.0       0.408216   0.703084  0.163128   0.815687
     0.211441  0.0185634  0.378299  0.0734293  0.187445
     0.667637  0.139323   0.286794  0.359962   0.229784
     0.476224  0.49812    0.648244  0.831006   0.1787
     0.960756  0.488886   0.195973  0.148958   0.200619
    
    julia> B === A # Now this is false
    false
    
    julia> B == A # This is still true
    true
    
    julia> B[1,1] = 1 #Changing B
    1
    
    julia> B
    5x5 Array{Float64,2}:
     1.0       0.408216   0.703084  0.163128   0.815687
     0.211441  0.0185634  0.378299  0.0734293  0.187445
     0.667637  0.139323   0.286794  0.359962   0.229784
     0.476224  0.49812    0.648244  0.831006   0.1787
     0.960756  0.488886   0.195973  0.148958   0.200619
    
    julia> A #Now does not change A
    5x5 Array{Float64,2}:
     2.0       0.408216   0.703084  0.163128   0.815687
     0.211441  0.0185634  0.378299  0.0734293  0.187445
     0.667637  0.139323   0.286794  0.359962   0.229784
     0.476224  0.49812    0.648244  0.831006   0.1787
     0.960756  0.488886   0.195973  0.148958   0.200619
    

提交回复
热议问题