Comparing tuple

iex(120)> {Decimal.new("0.000050"), "a4b54f67-7f22-40d3-b9d3-2519c318b971"} > {Decimal.new("1.2"), "ee6a2e74-72a3-4e12-9bb2-31b5a30b288e"}
true
iex(121)> Decimal.new("0.000005") > Decimal.new("1.2")                                                                                    
false
iex(122)> "a4b54f67-7f22-40d3-b9d3-2519c318b971" >  "ee6a2e74-72a3-4e12-9bb2-31b5a30b288e"                                                
false

Could anyone explain why the first (tuples) comparison is true ?

iex(29)> inspect Decimal.new("0.000050"), structs: false
"%{__struct__: Decimal, coef: 50, exp: -6, sign: 1}"
iex(30)> inspect Decimal.new("1.2"), structs: false
"%{__struct__: Decimal, coef: 12, exp: -1, sign: 1}"

Decimals are structs. They compare as maps do, by key/value pairs. In this case specifically, the coef value is larger in the first Decimal.

Decimal.compare/2 compares them numerically, however, comparing tuples containing Decimals does not use this mechanism because BEAM at the low level knows nothing about structs and their semantics, only maps.

7 Likes

thanks, Greg. Now it all makes sense.