Difference between double equals and triple equals for String Comparison in Elixir

前端 未结 1 1738
無奈伤痛
無奈伤痛 2021-01-01 10:27

I was reading a book on Elixir: Introducing Elixir.

On string compare it says that:

Elixir offers two options for comparing string equality, <

相关标签:
1条回答
  • 2021-01-01 11:00

    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
    

    And for !==

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

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

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

    From the 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.

    0 讨论(0)
提交回复
热议问题