Julia Integer types have a minimum and maximum value.
When calculations go beyond those boundaries, results are 'incorrect'.
for i in Int8[5, 10, 15, 20]
println(i, " -> ", i^2)
end
Result:
5 -> 25
10 -> 100
15 -> -31
20 -> -112
Is there an efficient way in Julia to raise an exception when this occurs?
This to prevent this from happening without noticing.
Maybe there are better solutions than raising an exception.
Julia normally does not error on integer overflow as this results in faster code execution (and this is how arithmetic works on the CPU level)
You can however use
@saferintegersmacro to control the overflow.In case of loops you need to use one of
Safe*types:And the second option (perhaps less convenient):