gpt4 book ai didi

elixir - Elixir 中字符串比较的双重等于和三重等于之间的区别

转载 作者:行者123 更新时间:2023-12-03 10:28:11 25 4
gpt4 key购买 nike

我正在读一本关于 Elixir 的书:Introducing Elixir .

在字符串比较它说:

Elixir offers two options for comparing string equality, == and === operators. The == operator is generally the simplest though the other produces the same result.



如果他们的意思相同,那么拥有两个运算符的目的是什么?

最佳答案

想到的一个例子是浮点数——它使用与字符串相同的比较函数:

iex> 1 == 1    #true
iex> 1 == 1.0 #true
iex> 1 === 1 #true
iex> 1 === 1.0 #false

对于 !==
iex> 1 != 2    #true
iex> 1 != 1.0 #false
iex> 1 !== 2 #true
iex> 1 !== 1.0 #true

值得注意的是,这些函数使用以下 Erlang 表达式:
Elixir | Erlang
== | ==
=== | =:=
!= | /=
!== | =/=

来自 Erlang documentation :

When comparing an integer to a float, the term with the lesser precision is converted into the type of the other term, unless the operator is one of =:= or =/=. A float is more precise than an integer until all significant figures of the float are to the left of the decimal point. This happens when the float is larger/smaller than +/-9007199254740992.0. The conversion strategy is changed depending on the size of the float because otherwise comparison of large floats and integers would lose their transitivity.

关于elixir - Elixir 中字符串比较的双重等于和三重等于之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32347090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com