Overflow behavior

The manual reads “In Julia, exceeding the maximum representable value of a given type results in a wraparound behavior” with an example along these lines:

julia> typemax(Int64)+1
-9223372036854775808

which does wrap around on 64-bit system, but other “smaller” integral types, e.g. Int32, Int16, etc. don’t overflow and wrap around:

julia> typemax(Int8)+1
256

Am I reading it wrong?

Literal constants like 1 are of the default Int type, which is Int64 on a 64-bit machine. So, typemax(Int8)+1 is an Int8 + Int64, which promotes the operands to Int64 and hence does not overflow. You are thinking of:

julia> typemax(Int8) + Int8(1)
-128
5 Likes

Thanks. I was typing just that. :grinning: