# Math operation known issue? a^b

**URL:** <https://discourse.julialang.org/t/math-operation-known-issue-a-b/38839>\
**Category:** General Usage\
**Tags:** question, special-issue, math, integer-overflow\
**Created:** [May 5, 2020, 10:17pm UTC](https://discourse.julialang.org/t/math-operation-known-issue-a-b/38839 "2020-05-05T22:17:53Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rougem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rougem/32/14635_2.png) [@Rougem](https://discourse.julialang.org/u/Rougem)\
**Post date:** [May 5, 2020, 10:17pm UTC](https://discourse.julialang.org/t/math-operation-known-issue-a-b/38839/1 "2020-05-05T22:17:53Z")

</div>

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!

![math](https://global.discourse-cdn.com/julialang/original/2X/f/fd308436496812ea40814ab884e64f9e9013fd70.jpeg)  
Issue:

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [May 5, 2020, 10:21pm UTC](https://discourse.julialang.org/t/math-operation-known-issue-a-b/38839/2 "2020-05-05T22:21:57Z")

</div>

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
julia> 20.0^20
1.048576e26

```

(although floats have their own set of issues: [PSA: floating-point arithmetic](https://discourse.julialang.org/t/psa-floating-point-arithmetic/8678))

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 5, 2020, 10:24pm UTC](https://discourse.julialang.org/t/math-operation-known-issue-a-b/38839/3 "2020-05-05T22:24:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rougem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rougem/32/14635_2.png) [@Rougem](https://discourse.julialang.org/u/Rougem)\
**Post date:** [May 6, 2020, 7:48am UTC](https://discourse.julialang.org/t/math-operation-known-issue-a-b/38839/4 "2020-05-06T07:48:49Z")

</div>

Thank you! Floats worked pretty well.

 ![math2.](https://global.discourse-cdn.com/julialang/original/3X/a/3/a3c837c8a1d43f55559cf23bfd53633199549223.jpeg)

---

<div class="post-metadata">

**Author:** ![Rougem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rougem/32/14635_2.png) [@Rougem](https://discourse.julialang.org/u/Rougem)\
**Post date:** [May 6, 2020, 7:50am UTC](https://discourse.julialang.org/t/math-operation-known-issue-a-b/38839/5 "2020-05-06T07:50:34Z")

</div>

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

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/e/7edd2dab4ad08b8f47e50c15ab813b0dc8f19b4a.png)

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [May 6, 2020, 7:55am UTC](https://discourse.julialang.org/t/math-operation-known-issue-a-b/38839/6 "2020-05-06T07:55:13Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 6, 2020, 8:11am UTC](https://discourse.julialang.org/t/math-operation-known-issue-a-b/38839/7 "2020-05-06T08:11:15Z")

</div>

You’re misreading what Oscar suggested: he is using the `big""` string macro (see docs [here](https://docs.julialang.org/en/v1/base/numbers/#Core.@big_str)), which is constructing a `BigInt` rather than a string object.

```julia
julia> big"20"^20
104857600000000000000000000

```

---

<div class="post-metadata">

**Author:** ![Rougem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rougem/32/14635_2.png) [@Rougem](https://discourse.julialang.org/u/Rougem)\
**Post date:** [May 6, 2020, 8:11am UTC](https://discourse.julialang.org/t/math-operation-known-issue-a-b/38839/8 "2020-05-06T08:11:49Z")

</div>

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?

 ![IMG-20200506-WA0000](https://global.discourse-cdn.com/julialang/original/3X/f/1/f10d498c3797a2b1a1ef8ba4325f3a68e6729a4d.jpeg)

---

<div class="post-metadata">

**Author:** ![Rougem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rougem/32/14635_2.png) [@Rougem](https://discourse.julialang.org/u/Rougem)\
**Post date:** [May 6, 2020, 8:12am UTC](https://discourse.julialang.org/t/math-operation-known-issue-a-b/38839/9 "2020-05-06T08:12:55Z")

</div>

😀 certainly, I did. Thank you.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 6, 2020, 8:38am UTC](https://discourse.julialang.org/t/math-operation-known-issue-a-b/38839/10 "2020-05-06T08:38:32Z")

</div>

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_

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 6, 2020, 9:00am UTC](https://discourse.julialang.org/t/math-operation-known-issue-a-b/38839/11 "2020-05-06T09:00:23Z")

</div>

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
julia> typeof(20)
Int64

julia> typeof(20000000000000000000)
Int128

julia> typeof(200000000000000000000000000000000000000)
BigInt

```

It doesn’t work for floats, though

```julia
julia> 1.7e308
1.7e308

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

julia> big"1.8e308"
1.800000000000000000000000000000000000000000000000000000000000000000000000000002e+308

```

---

<div class="post-metadata">

**Author:** ![Rougem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rougem/32/14635_2.png) [@Rougem](https://discourse.julialang.org/u/Rougem)\
**Post date:** [May 6, 2020, 9:17am UTC](https://discourse.julialang.org/t/math-operation-known-issue-a-b/38839/12 "2020-05-06T09:17:20Z")

</div>

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