Understanding Julia Int Overflow Behavior

Based on the Python / Matlab background, I would like to better understand how Julia Int64 overflow behavior works.

From the documentation :

In Julia, exceeding the maximum representable value of this type leads to step-by-step behavior.

julia> x = typemax(Int64)
9223372036854775807

julia> x + 1
-9223372036854775808

Now I have done some experimentation with numbers that are clearly larger than typemax (Int64), but the behavior that I see does not match the documentation. It seems that things do not always just wrap themselves around. Is only one wrapping allowed?

julia> x = (10^10)^(10^10)
0
julia> x = 10^10^10^10 
1   # ??

julia> x = 10^10^10^10^10  
10  # I'd expect it to be 1? 1^10 == 1?

julia> x = 10^10^10^10^10^10 
10000000000  # But here 10^10 == 10000000000, so now it works?


julia> typemax(Int64) > 10^19
true
julia > typemax(Int64) > -10^19
true

Can anyone shed light on the behavior that I see?

EDIT:

Why is 9 overflowing correctly and 10 is not working?

julia> 9^(10^14)
-1193713557845704703
julia> 9^(10^15)
4900281449122627585
julia> 10^(10^2)
0
julia> 10^(10^3)
0

Julia 0.5.0 (2016-09-19)

+4
source share
1 answer

PEMDAS, , . -.

julia> 10^(10^10) #happens to overflow to 0
0

julia> 10^(10^(10^10)) # same as 10 ^ 0
1

julia> 10^(10^(10^(10^(10^10)))) # same as x = 10^(10^(10^(10^(10000000000)))) -> 10^(10^(10^(0))) -> 10^(10^(1)) -> 10^ 10
10000000000

. , , BigInt .

+5

Source: https://habr.com/ru/post/1656386/


All Articles