Math operation known issue? a^b

Today is my first day with Julia. While, working with the command line I realized that some results were negative and wrong. I am new, so if I made a mistake please let me know. What is wrong?

Thanks a lot!


No, this is expected. It’s called integer overflow. Once you reach the highest representable number it wraps around to the lowest. This is how integer works on computers.

If you use floats instead it works:

julia> 20.0^20

(although floats have their own set of issues: PSA: floating-point arithmetic)


If you want really big numbers, use big"20"^1000 to use arbitrary precision (but slow) math.


Thank you! Floats worked pretty well.

I tried this suggestion. Because “” makes a string it added up all the 20’s together.

@rougem Welcome to Julia. Please continue to ask questions. People here astore numbers re very friendly.

One thing you should know - this behaviour with integers or floating point numbers is because computers really store numbers as binary digits. We somehow have to translate how those binary digits represent oru familiar decimal numbers.

I do not want to complicate things but there have been ternary computer architectures, notably by the Russians. Also the decatron tube baseed machine we saw at Bletchley Park in the UK.

But binary is the way of all computers these days.

You’re misreading what Oscar suggested: he is using the big"" string macro (see docs here), which is constructing a BigInt rather than a string object.

julia> big"20"^20

Hi John,

Thank you for your kind message. I tried the same with python. It did not cause problems with integers or floats. What is the difference between Julia and Phyton? Do you know?

:grinning: certainly, I did. Thank you.

Python trades safety for speed: it automatically checks whether an operation is going to overflow, if so it autopromotes the numbers to a larger format. This is slow

1 Like

Instead of big"20" you could also write big(20). This also works for floats: big(20.0) is a BigFloat, while big(20) is a BigInt.

Julia is also smart about parsing long integers. If you write:

julia> typeof(20)

julia> typeof(20000000000000000000)

julia> typeof(200000000000000000000000000000000000000)

It doesn’t work for floats, though

julia> 1.7e308

julia> 1.8e308
ERROR: syntax: overflow in numeric constant "1.8e308"
 [1] top-level scope at REPL[269]:0

julia> big"1.8e308"
1 Like

Thanks a lot for all these great info :woman_technologist: they will be verg useful once I start coding. Fingers crossed.