# Multiplication of 2 positive numbers gives a negative product

**URL:** https://discourse.julialang.org/t/multiplication-of-2-positive-numbers-gives-a-negative-product/80323
**Category:** New to Julia
**Tags:** question, integer-overflow
**Created:** [May 1, 2022, 11:39am UTC](https://discourse.julialang.org/t/multiplication-of-2-positive-numbers-gives-a-negative-product/80323 "2022-05-01T11:39:00Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Brinkhuis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brinkhuis/32/18752_2.png) [@Brinkhuis](https://discourse.julialang.org/u/Brinkhuis)
#### Post date: [May 1, 2022, 11:39am UTC](https://discourse.julialang.org/t/multiplication-of-2-positive-numbers-gives-a-negative-product/80323/1 "2022-05-01T11:39:00Z")

</div>

In Julia 1.7.2 multiplication of `3037000691` and `3037000693` returns the negative product `-9223370870501072753`. The product I expected is `9223373203208478863`.

```
function m(a::BigInt, b::BigInt)::BigInt
    a * b
end

```

This function gives the same negative result.

Any ideas on how to get the correct answer in Julia 1.7.2 to `3037000691 * 3037000693` ?

P.S.  
I did run into this issue doing some math on (big) twin prime numbers.

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [May 1, 2022, 11:47am UTC](https://discourse.julialang.org/t/multiplication-of-2-positive-numbers-gives-a-negative-product/80323/2 "2022-05-01T11:47:34Z")

</div>

```julia
julia> m(big(3037000691), big(3037000693))
9223373203208478863

```

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [May 1, 2022, 11:54am UTC](https://discourse.julialang.org/t/multiplication-of-2-positive-numbers-gives-a-negative-product/80323/3 "2022-05-01T11:54:59Z")

</div>

Julia’s integers are [machine integers](https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/) with defined [overflow behavior](https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/#Overflow-behavior) - the negative result is expected. Using `BigInt` (like shown above by @jishnub) uses the GMP library in the background, which has [arbitrary precision](https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/#Arbitrary-Precision-Arithmetic) arithmetic which does not have overflow.

Do you have a runnable example where one of the two behaviors I described is not as expected?

---

<div class="post-metadata">

### Author: ![Brinkhuis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brinkhuis/32/18752_2.png) [@Brinkhuis](https://discourse.julialang.org/u/Brinkhuis)
#### Post date: [May 1, 2022, 12:03pm UTC](https://discourse.julialang.org/t/multiplication-of-2-positive-numbers-gives-a-negative-product/80323/4 "2022-05-01T12:03:16Z")

</div>

Thanks for your answer. How should I (re)write the function to return the correct answer? I would like to use the arguments `037000691` and `3037000693` instead of `big(037000691)` and `big(3037000693)`.

---

<div class="post-metadata">

### Author: ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)
#### Post date: [May 1, 2022, 12:16pm UTC](https://discourse.julialang.org/t/multiplication-of-2-positive-numbers-gives-a-negative-product/80323/5 "2022-05-01T12:16:54Z")

</div>

You can always explicitly convert if you know you always want the answer as a `BigInt`

```julia
function m(a, b)
    big(a) * big(b)
end

```

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [May 1, 2022, 12:34pm UTC](https://discourse.julialang.org/t/multiplication-of-2-positive-numbers-gives-a-negative-product/80323/6 "2022-05-01T12:34:13Z")

</div>

The widen function can be used to generically convert to a wide enough type to store all possible arithmetic results. Ie `widen(a) * widen(b)`.

---

<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 1, 2022, 12:34pm UTC](https://discourse.julialang.org/t/multiplication-of-2-positive-numbers-gives-a-negative-product/80323/7 "2022-05-01T12:34:45Z")

</div>

Without knowing anything more about your use case, except that you want to multiply two numbers, the only useful advice is to use `big` or some other extended precision type.

Tell us more about what you need these numbers _for_, maybe we can give better input. Maybe you don’t actually _need_ to multiply, or maybe you can divide them by something first.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [May 1, 2022, 12:57pm UTC](https://discourse.julialang.org/t/multiplication-of-2-positive-numbers-gives-a-negative-product/80323/8 "2022-05-01T12:57:17Z")

</div>

Given Julia’s promotion rules, we could do “half the promotions” as we type:

```julia
big(a) * b
widen(a) * b

```

---

<div class="post-metadata">

### Author: ![LeePhillips](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leephillips/32/205514_2.png) [@LeePhillips](https://discourse.julialang.org/u/LeePhillips)
#### Post date: [May 1, 2022, 1:37pm UTC](https://discourse.julialang.org/t/multiplication-of-2-positive-numbers-gives-a-negative-product/80323/9 "2022-05-01T13:37:00Z")

</div>

> [@Brinkhuis](#):
>
> This function gives the same negative result.

In fact it gives a MethodError, unless you supply Big arguments, as @jishnub shows.

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [May 1, 2022, 2:14pm UTC](https://discourse.julialang.org/t/multiplication-of-2-positive-numbers-gives-a-negative-product/80323/10 "2022-05-01T14:14:53Z")

</div>

> `widen(a) * b`

That still fails for large numbers, depending on their types:

```julia
julia> a = 10^18
julia> b = Int128(10)^38

julia> widen(a) * b
-2659601278039724572920059057942822912

julia> big(a) * b
100000000000000000000000000000000000000000000000000000000

```

---

<div class="post-metadata">

### Author: ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)
#### Post date: [May 1, 2022, 2:39pm UTC](https://discourse.julialang.org/t/multiplication-of-2-positive-numbers-gives-a-negative-product/80323/11 "2022-05-01T14:39:56Z")

</div>

> [@Brinkhuis](#):
>
> returns the negative product `-9223370870501072753` . The product I expected is `9223373203208478863` .

Believe it or not the negative value is the correct (according to Int64 modular math), so expected by some.

> [@Brinkhuis](#):
>
> This function gives the same negative result.

Not it doesn’t:

```julia
julia> m(3037000691, 3037000693)
ERROR: MethodError: no method matching m(::Int64, ::Int64)

```

You probably also had such a function defined.

You additionally want:

```julia
julia> function m(a::Integer, b::Integer)
           m(big(a), big(b))
       end

and maybe also (to not convert to big twice):

julia> function m(a::Int64, b::Int64)
           big(widen(a) * widen(b))
       end

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [May 1, 2022, 4:25pm UTC](https://discourse.julialang.org/t/multiplication-of-2-positive-numbers-gives-a-negative-product/80323/12 "2022-05-01T16:25:32Z")

</div>

> [@aplavin](#):
>
> That still fails for large numbers, depending on their types:

You widened Int64, you should have widened Int128 in your example:

```julia-auto
a * widen(b) 
100000000000000000000000000000000000000000000000000000000

```
