官术网_书友最值得收藏!

When are two values or objects equal or identical?

Whether two values are equal or not can be decided by the == operator, for example, 5 == 5 and 5 == 5.0 are both true. Equivalent to this operator is the isequal() function:

isequal(5, 5)   #> true 
isequal(5, 5.0) #> true 

Both the preceding statements return true, because objects such as numbers are immutable and they are compared at the bits level.

To see whether the two objects x and y are identical, they must be compared with the === operator. The result is a Bool value, true or false: x === y -> Bool, for example:

5 === 5 #> true 
5 === 5.0 #> false

For objects that are more complex, such as strings, arrays, or objects that are constructed from composite types, the addresses in the memory are compared to check whether they point to the same memory location. For immutable object such as struct, this gets optimized so that instances with the same value point to the same object:

struct Vector3D
x::Float64
y::Float64
z::Float64
end

q = Vector3D(4.0, 3.14, 2.71)
r = Vector3D(4.0, 3.14, 2.71)
isequal(q, r) #> true
q === r #> true

However, if objects are mutable, they are different objects even if they have the same value, as follows:

mutable struct MVector3D
x::Float64
y::Float64
z::Float64
end

q = MVector3D(4.0, 3.14, 2.71)
r = MVector3D(4.0, 3.14, 2.71)
isequal(q, r) #> false
q === r #> false
主站蜘蛛池模板: 会昌县| 灵石县| 上栗县| 黄陵县| 平江县| 三亚市| 靖江市| 涿州市| 砀山县| 永靖县| 嵊泗县| 米易县| 汾西县| 南漳县| 襄樊市| 咸丰县| 漯河市| 汽车| 海淀区| 恭城| 同江市| 抚远县| 丰宁| 荃湾区| 马公市| 新龙县| 德庆县| 芜湖县| 麟游县| 乌拉特后旗| 吉隆县| 炎陵县| 高尔夫| 云阳县| 济阳县| 山阴县| 仁寿县| 肇州县| 华宁县| 南京市| 深水埗区|