# Long Math Operation result is 0, In Python isn't

**URL:** <https://discourse.julialang.org/t/long-math-operation-result-is-0-in-python-isnt/73720>\
**Category:** New to Julia\
**Tags:** question, integer-overflow\
**Created:** [December 28, 2021, 7:17pm UTC](https://discourse.julialang.org/t/long-math-operation-result-is-0-in-python-isnt/73720 "2021-12-28T19:17:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jogrefer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jogrefer/32/32260_2.png) [@Jogrefer](https://discourse.julialang.org/u/Jogrefer)\
**Post date:** [December 28, 2021, 7:17pm UTC](https://discourse.julialang.org/t/long-math-operation-result-is-0-in-python-isnt/73720/1 "2021-12-28T19:17:49Z")

</div>

Hi everyone 🙂  
Well, I’m new in this in general and just testing the REPL with random numbers.  
Why this is happening and what I’ve to learn previous to declare operations like this one?

In Julia (1.7.0):

```julia
n = 123 * 123123567 * 5675675 * 86786 * 456456 * 78678 * 354345345 * 567567 * 3545 * 678678 * 234^23423 * 238^345354
--
0  

```

To confirm if the result is truly 0 I’ve tested in Python:  
In Python (3.10.1)

```julia
n
--
30332308162763280714160902682533172328953144277024172702616 

```

I’ve already test with Real, Int8, Int16, Int64, Float64 before each operation and every print in Julia gives me 0.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [December 28, 2021, 7:27pm UTC](https://discourse.julialang.org/t/long-math-operation-result-is-0-in-python-isnt/73720/2 "2021-12-28T19:27:22Z")

</div>

> [@Jogrefer](#):
>
> `234^23423`

The problem is that integers overflow, and thus:

```julia
julia> 234^23423
0

julia> 235^23423
-5572363361863899709

```

Maybe someone knows a better FAQ link, this is one thread about it:

> [@Why is 2^63 negative?](https://discourse.julialang.org/t/why-is-2-63-negative/48513):
>
> Hi wave Making my first steps into julia, coming from R and python. Following the getting started guide, I noticed the following: 2^62 # 4611686018427387904 2^63 # \<--- This is negative? # -9223372036854775808 2^63 \> 1 # false 2^64 # 0 # this is zero, but that makes sense if it is a 64 bit float I’d be interested to understand why? For reference, python and R below: 2^63 \> 1 # true 2^63 \> 1 # [1] TRUE

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [December 28, 2021, 7:31pm UTC](https://discourse.julialang.org/t/long-math-operation-result-is-0-in-python-isnt/73720/3 "2021-12-28T19:31:00Z")

</div>

```julia
234^23423 * 238^345354

```

At first glance, you can tell that these numbers do not fit into a `64` bit integer, and hence we’ll only get the lower 64 bits (which will all be zero, as `true_answer % (2^64) == 0`, which you can tell from the fact that both numbers are even and both exponents are greater than 64).  
Multiplying the other numbers by `0` will of course result in an answer of `0`.

Convert the numbers to `BigInt` first.

```julia
n = big"123" * 123123567 * 5675675 * 86786 * 456456 * 78678 * 354345345 * 567567 * 3545 * 678678 * big(234)^23423 * big"238"^34535

```

This is probably enough to be correct (multipications should go left to right, so making the first `big` should keep all intermediates big, and then we also need the two exponents).  
The number is very large.  
Far larger than the number you said Python gave.  
I’d post it, but it is several times over the character limit.

---

<div class="post-metadata">

**Author:** ![trahflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trahflow/32/30585_2.png) [@trahflow](https://discourse.julialang.org/u/trahflow)\
**Post date:** [December 28, 2021, 7:31pm UTC](https://discourse.julialang.org/t/long-math-operation-result-is-0-in-python-isnt/73720/4 "2021-12-28T19:31:09Z")

</div>

Use `BigInt`, e.g by: `big"234"^23423`.  
Also note, that in python the `^` operator is the `xor` operator, while in julia it is the exponentiation operator.  
So unless you actually want `xor`s, the result you got in python is not the one you expect.  
(In python, the exponentiation operator is `**`)

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [December 28, 2021, 7:33pm UTC](https://discourse.julialang.org/t/long-math-operation-result-is-0-in-python-isnt/73720/5 "2021-12-28T19:33:07Z")

</div>

> [@trahflow](#):
>
> Also note, that in python the `^` operator is the `xor` operator

Ah, that’s why the number was so small in Python.  
You can also use `⊻` (type via `/xor`) in Julia.

Using `xor`, I now get:

```julia
julia> n = big"123" * 123123567 * 5675675 * 86786 * 456456 * 78678 * 354345345 * 567567 * 3545 * 678678 * 234 ⊻ 23423 * 238 ⊻ 34535
30332308162763280714160902682533172328953144277024172521589

```

---

<div class="post-metadata">

**Author:** ![Jogrefer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jogrefer/32/32260_2.png) [@Jogrefer](https://discourse.julialang.org/u/Jogrefer)\
**Post date:** [December 28, 2021, 8:00pm UTC](https://discourse.julialang.org/t/long-math-operation-result-is-0-in-python-isnt/73720/6 "2021-12-28T20:00:48Z")

</div>

Thank You 😄 @trahflow @Elrod

At first I’ve tested the `BigInt` but just at the beginning. Now I write:

> n = BigInt(123123 \* 3453453 \* 564456456 \* 234234 \* 7567567 \* 5345345 \* 567567 \* (big(5523)^238489) \* (big(837218)^2342340))

And I think the result is right (_Really big number_)

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [December 28, 2021, 8:10pm UTC](https://discourse.julialang.org/t/long-math-operation-result-is-0-in-python-isnt/73720/7 "2021-12-28T20:10:49Z")

</div>

When you write:

```julia
n = BigInt(123123 * 3453453 * 564456456 * 234234 * 7567567 * 5345345 * 567567 * (big(5523)^238489) * (big(837218)^2342340))

```

Note that

```julia
123123 * 3453453 * 564456456 * 234234 * 7567567 * 5345345 * 567567

```

will be calculated with all of them as `Int`s, which will overflow.  
Additionally,

```julia
123123 * 3453453 * 564456456 * 234234 * 7567567 * 5345345 * 567567 * (big(5523)^238489) * (big(837218)^2342340)

```

will be a `BigInt`, therefore calling `BigInt` on the above result will not do anything.  
Hence:

```julia
n = BigInt(123123) * 3453453 * 564456456 * 234234 * 7567567 * 5345345 * 567567 * (big(5523)^238489) * (big(837218)^2342340)

```

is what you want.  
Making the first of these a `BigInt` will keep the accumulation in `BigInt` as we move from left to right.  
And, we also still need the exponents to be `BigInt` as well, hence I left the `big` on them.

---

<div class="post-metadata">

**Author:** ![jondea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jondea/32/39086_2.png) [@jondea](https://discourse.julialang.org/u/jondea)\
**Post date:** [January 2, 2022, 7:20pm UTC](https://discourse.julialang.org/t/long-math-operation-result-is-0-in-python-isnt/73720/8 "2022-01-02T19:20:15Z")

</div>

Two packages you may find interesting/useful:

- [CheckedArithmetic.jl](https://github.com/JuliaMath/CheckedArithmetic.jl) provides a macro which checks for over/underflows in calculations.

- [SafeREPL.jl](https://github.com/rfourquet/SafeREPL.jl) changes all number literals in the REPL to a different type, BigInts by default, so that you don’t get under/overflow. This will make most calculations slower, but is useful if you care more about correctness than speed.
