[erlang-questions] Strange behaviour of =:= applied to int and int64

Jeff Schultz jws@REDACTED
Wed Sep 7 16:03:04 CEST 2011


On Wed, Sep 07, 2011 at 05:42:19PM +0400, Dmitry Groshev wrote:
> I'm using R14B03 and writing a little NIF library. Today I've
> encountered a strange behaviour of =:= when it is used against number
> that is set in sources and term that was constructed as
> enif_make_unit64. Here is an example:

Is it possible that you've made a "big" integer with a value of 5 in
your NIF?  I've not looked at how BEAM handles this, but one common
arrangement is to use tagged immediate values for "small" integers,
and tagged pointers to some big-integer implementation for *all* other
integers.  The lack of overlap between the representations is then
used as an opportunity for optimisation because no small integer can
equal a big integer.

As we have

    1> 1==1.0.
    true
    2> 1=:=1.0.
    false

it would be unsurprising if =:=/2 (a.k.a. "exactly equal to") were
false when applied to 5 as a "small" integer and 5 as a "big" integer.


> (circa_dev@REDACTED)1> {ok, T} = ecirca:new(5, last, small).
> {ok,{ecirca,#Ref<0.0.0.57>,<<>>,<0.48.0>,small}}
> (circa_dev@REDACTED)2> {ok, A} = ecirca:size(T).
> {ok,5}
> (circa_dev@REDACTED)3> B = 5.
> 5
> (circa_dev@REDACTED)4> A == B.
> true
> (circa_dev@REDACTED)5> A =:= B.
> false
> (circa_dev@REDACTED)6> A.
> 5
> (circa_dev@REDACTED)7> B.
> 5


    Jeff Schultz



More information about the erlang-questions mailing list