Reputation: 781
How does Julia compute big numbers?
For example, this works as expected:
julia> 10^18
1000000000000000000
But for bigger numbers, there is a problem with integers:
julia> 10^19
-8446744073709551616
julia> 10^20
7766279631452241920
But it works if a decimal number is used:
julia> 10.0^20
1.0e20
Do you know why?
Upvotes: 3
Views: 2502
Reputation: 382
Check this documentation page: https://docs.julialang.org/en/release-0.4/manual/integers-and-floating-point-numbers/
As you can see, Int64 have a max length of: 2^63 - 1 ~ 1.0 * 10^19 So your 10^19 is greater than this max value. That's why there is a problem. You can try to convert your 10 to another type.
10.0^20
works because 10.0
is a float, so it has a superior max value.
If you want unlimited precision for integers, you can use BigInts
:
julia> BigInt(10)^100
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Upvotes: 10