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


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

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

This is not the case with BigInt and BigFloat

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

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

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

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

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.


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.



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.