I (as a noob) have a basic question:
x = typemax(Int32)
x = x + 1
i get Int64. So we go from Int32 to Int64
y = typemax(Int64)
y = y + 1
y now is -9223372036854775808
Why we cannot go to Int128? Does this depend on the system architecture?
It’s much simpler than this. 1 is an int64 (on 64 bit machines). Thus, when you add an int32 to an int64, you get an int64.
julia> typemax(Int32) + Int32(1)
julia> typemax(Int64) + Int128(1)
Julia does not and will not do automatic widening of
Int because that has gigantic, negative performance implications.
One thing you can do is use a pacakge like SaferIntegers.jl
julia> using SaferIntegers
julia> @saferintegers begin
typemax(Int) + 1
ERROR: OverflowError: 9223372036854775807 + 1 overflowed for type Int64
 throw_overflowerr_binaryop(::Symbol, ::Int64, ::Int64) at ./checked.jl:154
 checked_add at ./checked.jl:166 [inlined]
 + at /home/mason/.julia/packages/SaferIntegers/lLDKG/src/arith_ops.jl:21 [inlined]
 +(::Int64, ::SafeInt64) at /home/mason/.julia/packages/SaferIntegers/lLDKG/src/arith_ops.jl:71
 top-level scope at REPL:2
This way, these things are explicitly caught and you can use more appropriate number types.