Why get 0 for 2^(3^4)?

In Julia REPL, I input 2^(3^4) or big(2^(3^4)), still give me 0.
So, how to get the right answer?

Julia is not overflow-aware for native numerical types. The answer is 2^81 which can’t be represented by the Int64 type, you only get the last 64 bits which are all zero. You must either use Float (i.e. 2.0^(3^4)) or a larger integer type like big"2"^(3^4) (this can accomodate arbitrarily large integers), or int128"2"^(3^4).

3 Likes

cool! But what’s the means for "2"

big"2" (and not "2" alone) is the string literal to construct an arbitrary precision number

5 Likes

And note the difference between big(2^(3^4)) on one side and big(2)^(3^4)) on the other.
In the first case the computtion is made using Int64 numbers, and only the result is converted to BigInt, when “it is too late” and the number has already overflown.
In the second case instead the operation involves at least one BigInt and all operands are casted to BigInt leading to the right result.

4 Likes

Again, so let’s do this work

x=2
y=81

# big"x"^y
# big"$(x)"^y
big(x)^y

So, I stiil confused with big"x".

macros are expanded at parse time which is before x has a value. a string macro can only see the literal contents of the string.

julia> x = 2; y = big"2";

julia> typeof(x)
Int64

julia> typeof(y)
BigInt