Mathematica 中 == 和 === 的区别
我的印象是 =
是一个赋值,==
是一个数字比较,而 ===
是一个符号比较(以及就像在其他一些语言中一样,==
等于
和 ===
等于
但是,看看。下面看来情况不一定如此...
In: x == x
Out: True
In: x === x
Out: True
In: 5 == 5
Out: True
In: 5 === 5
Out: True
In: x = 5
Out: 5
In: 5 == x
Out: True
In: 5 === x
Out: True
In: 5 5 == 5x
Out: True
In: 5 5 === 5x
Out: True
In: x == y
Out: x == y
In: x === y
Out: False
In: y = x
Out: 5
In: x == y
Out: True
In: x === y
Out: True
那么 Mathematica 中 == 和 === 到底有什么区别?我一直在查看文档,但我仍然不太明白它。
I was under the impression that =
is an assignment, ==
is a numeric comparison, and ===
is a symbolic comparison (as well as in some other languages ==
being equal to
and ===
being identical to
. However, looking at the following it would appear that this is not necessarily the case...
In: x == x
Out: True
In: x === x
Out: True
In: 5 == 5
Out: True
In: 5 === 5
Out: True
In: x = 5
Out: 5
In: 5 == x
Out: True
In: 5 === x
Out: True
In: 5 5 == 5x
Out: True
In: 5 5 === 5x
Out: True
In: x == y
Out: x == y
In: x === y
Out: False
In: y = x
Out: 5
In: x == y
Out: True
In: x === y
Out: True
So what exactly is the difference between == and === in Mathematica? I have been looking at the documentation but I still don't quite understand it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一个重要的区别是
===
始终返回True
或False
。==
可以返回未计算的值(这就是它对于表示方程很有用的原因。)有一些有趣的情况,其中
==
返回未计算的值,在编程时值得注意。例如:这可能会影响
If[foo=={},,]
等内容。One important difference is that
===
always returnsTrue
orFalse
.==
can return unevaluated (which is why it's useful for representing equations.)There are some interesting cases where
==
returns unevaluated that are worth being aware of while programming. For example:which can affect things like
If[foo=={}, <true>, <false>]
.==
和===
非常相似,如果左侧和右侧相等,则返回True
。它们不同的一个例子是当您比较不同表示格式的数字时。尽管它们在数字上相同(这就是
==
返回True
的原因),但它们并不完全相同。仅供参考,它们在内部是不同的功能。
==
是Equal
,而===
是SameQ
。==
and===
are very similar in the sense that it returnsTrue
if the lhs and rhs are equal. One example where they differ is when you compare numbers in different representation formats.Although they are the same numerically, (which is why
==
returnsTrue
), they aren't exactly identical.FYI, they are different functions internally.
==
isEqual
, whereas===
isSameQ
.Equal
指语义相等,而SameQ
指句法相等。例如,Sin[x]^2+Cos[x]^2
和1
是相同的数字,因此它们在语义上是相等的。由于如果不进行更多转换就无法确定这一点,因此Equal
返回未计算的值。但是,实际表达式不同,因此SameQ
给出False
。请注意,对
Real
数字有特殊处理,如果a
和SameQ[a,b]
可以返回True
code>b 最后一个二进制数字不同。要进行更严格的身份测试,请使用Order[a,b]==0
SameQ
对于语法不同的表达式可以返回True
,因为表达式头可以自动对参数进行排序。您可以使用保留属性来防止自动排序。例如Equal
refers to semantic equality whereasSameQ
is syntactic equality. For instance,Sin[x]^2+Cos[x]^2
and1
are the same number, so they are equal semantically. Since this can not be determined without more transformations,Equal
returns unevaluated. However, actual expressions are different, soSameQ
givesFalse
.Note that there's special handling of
Real
numbers,SameQ[a,b]
can returnTrue
ifa
andb
differ in the last binary digit. To do more restrictive identity testing, useOrder[a,b]==0
SameQ
can returnTrue
for expressions that are syntactically different because expression heads may sort arguments automatically. You can prevent automatic sorting by using holding attributes. For instance和
参考来自此处和此处。
and
Reference from here and here.
我将引导您阅读 Leonid Shifrin 撰写的一本优秀著作的第 2.5 节:平等检查。
I direct you to section 2.5: Equality checks of an excellent book by Leonid Shifrin.