Elixir中的字符串比较,双等于和三等于之间的区别 [英] Difference between double equals and triple equals for String Comparison in Elixir

查看:77
本文介绍了Elixir中的字符串比较,双等于和三等于之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一本关于Elixir的书:介绍Elixir

I was reading a book on Elixir: Introducing Elixir.

在字符串比较中说:


Elixir提供了两个比较字符串相等性的选项, == === 运算符。 == 运算符通常是最简单的,尽管另一个运算符会产生相同的结果。

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

如果两个运算符含义相同,那么有什么目的呢?

Whats the purpose of having two operators if they mean the same thing?

推荐答案

浮现在下的一个例子是浮点数-

One example that comes to mind is floats - which use the same comparison functions as strings:

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表达式:

It is worth noting that these functions use the following Erlang expressions:

Elixir | Erlang
==     | ==
===    | =:=
!=     | /=
!==    | =/=

来自Erlang文档


当将整数与浮点数进行比较时,术语越小除非运算符是=:=或= / =之一,否则precision会转换为其他术语的类型。浮点数比整数更精确,直到浮点数的所有有效数字都在小数点的左侧为止。当浮点数大于/小于+/- 9007199254740992.0时,就会发生这种情况。转换策略会根据浮点数的大小而更改,因为否则比较大浮点数和整数会失去其可传递性。

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中的字符串比较,双等于和三等于之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆