Why is not big(1) === big(1)

For most immutable numeric types a === b if a == b. For example:

julia> Int128(1) === Int128(1)
true

This is not the case with BigInt and BigFloat

julia> BigInt(1) === BigInt(1)
false

julia> BigFloat(1) === BigFloat(1)
false

Comparison using == still works as expected, so big(1) == big(1), but this does not extend to comparing structs that contain BigInt or BigFloat (unless == is explicitly overloaded) because == defaults to ===.

julia> immutable T
         x::BigInt
       end

julia> T(1) == T(1)
false

Is there a reason for this behavior, or it it a bug?

(I’ve tried this under 0.5.1 and 0.6.0-pre.alpha.76.)

=== is for comparing identity, not for comparing values. This is not a bug and is expected given that BigInt and BigFloat are mutable types.

The fact that BigFloat and BigInt are implemented as mutable types and therefore can be distinguished even when two objects holds the same value is an implementation detail that you shouldn’t need to care about if you just want to write generic code that handles numbers. If you are doing something specific to these types and somehow cares about their identity then you’ll need too handle this.

1 Like

OK. Thanks! I didn’t realize BigInt and BigFloat were mutable.

If they are considered numbers, they should be immutable and === should work: this has already been discussed (here for example).
The implementation detail is the underlying C library and for performance reason I understand that it is better to reuse the space allocated, making the type mutable.

1 Like

No.

That’s unrelated.

No to what? That numbers are immutable or that === should work?

Both. They don’t need to be immutable and === doesn’t need to work the same as == for numbers.