# Julia cannot add 2 big integers correctly

**URL:** <https://discourse.julialang.org/t/julia-cannot-add-2-big-integers-correctly/95774>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [March 9, 2023, 12:04am UTC](https://discourse.julialang.org/t/julia-cannot-add-2-big-integers-correctly/95774 "2023-03-09T00:04:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vsoler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vsoler/32/30667_2.png) [@vsoler](https://discourse.julialang.org/u/vsoler)\
**Post date:** [March 9, 2023, 12:04am UTC](https://discourse.julialang.org/t/julia-cannot-add-2-big-integers-correctly/95774/1 "2023-03-09T00:04:40Z")

</div>

I have come across what I consider a big problem.

I’m reading “Think Julia” by Allen Downey and Ben Lauwens, and I am now in the chapter about Dictionaries. I am specifically on the Memos subchapter.

I am calculating the fibonacci series of numbers. I use the following code:

```julia
known = Dict(0=>0, 1=>1)

function fibonacci(n)
    if n ∈ keys(known)
        return known[n]
    end
    res = fibonacci(n-1) + fibonacci(n-2)
    known[n] = res
    res
end

```

For small numbers it calculates well. The problem is with the “big numbers”.

```julia
fibonacci(1425)
7824277221020082018

fibonacci(1426)
8946276023139000775

fibonacci(1427)
-1676190829550468823

```

Wait a minute ! The sum of two positive numbers cannot give me a negative number.

```
   fibonacci(1425) + fibonacci(1426) should equal fibonacci(1427)

```

To me, it seems a problem of overflow. Somehow, I am calculating very big fibonacci numbers.

The problem is that Julia doesn’t warn me or even give me an error !!!

OK, let me try the calculation with the BigInt type of numbers. My new code is …

```julia
known = Dict{Int, BigInt}(0=>0, 1=>1)

function fibonacci(n)
    if n ∈ keys(known)
        return known[n]
    end
    res = fibonacci(n-1) + fibonacci(n-2)
    known[n] = res
    res
end

```

Let’s calculate the same fibonacci numbers.

```julia
fibonacci(1425)
2870135193715480114725730918763701706297989719713950663872181225188570407497069958099922697430757395929817284686166601789284155271891376602707398646646393487578030823647270568341447238508684080880943650098811795294138886743280637686860685621776451660752376535348947846577631510497401804806696542050

fibonacci(1426)
4643976295738910422782284314065068453793584638755326395700934561957574291597816213807062336695384839760502199019030434245092888600854131835376621590138445911467175656089921622533698591501454932822440811745532167720317101247893453436522997270972458827078034544593550904048414169482938822744045478343

fibinacci(1427) 
7514111489454390537508015232828770160091574358469277059573115787146144699094886171906985034126142235690319483705197036034377043872745508438084020236784839399045206479737192190875145830010139013703384461844343963014455987991174091123383682892748910487830411079942498750626045679980340627550742020393

```

This time, the sum of two positive numbers is positive, which is perfect.  
But the new results, calculated with BigInt have nothing to do with those calculated previously. They are different by many orders of magnitude.

This is where I begin to mistrust the calculations that Julia does for me.

1. Do you think that I am missing any important points?
2. The results that I am getting seem to me incoherent. What’s your opinion?

I appreciate any comments.

---

<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:** [March 9, 2023, 12:08am UTC](https://discourse.julialang.org/t/julia-cannot-add-2-big-integers-correctly/95774/2 "2023-03-09T00:08:04Z")

</div>

The results are the same mod 2^64. Julia’s `Int`s have overflow because `Int`s on a CPU have overflow and making your basic integer type a `BigInt` makes your language about 50x slower. For a lot more detail, see [Discussion about integer overflow](https://discourse.julialang.org/t/discussion-about-integer-overflow/69627)

---

<div class="post-metadata">

**Author:** ![vsoler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vsoler/32/30667_2.png) [@vsoler](https://discourse.julialang.org/u/vsoler)\
**Post date:** [March 9, 2023, 12:16am UTC](https://discourse.julialang.org/t/julia-cannot-add-2-big-integers-correctly/95774/3 "2023-03-09T00:16:41Z")

</div>

Thank Oscar\_Smith,

I understand, from your reply, that BigInt gives me the correct answer although it is 50x slower.

However, I consider that Julia should error in case of overflow, and it doesn’t.

Is there any code snippet to catch overflow problems? no doubt it would help a lot.

---

<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:** [March 9, 2023, 12:17am UTC](https://discourse.julialang.org/t/julia-cannot-add-2-big-integers-correctly/95774/4 "2023-03-09T00:17:51Z")

</div>

`Base.Checked` has operators that will throw errors on overflow. That said, the possibility of error throwing will also make your code noticeably slower.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [March 9, 2023, 9:18am UTC](https://discourse.julialang.org/t/julia-cannot-add-2-big-integers-correctly/95774/5 "2023-03-09T09:18:42Z")

</div>

Consider using [GitHub - JeffreySarnoff/SaferIntegers.jl: These integer types use checked arithmetic, otherwise they are as system types.](https://github.com/JeffreySarnoff/SaferIntegers.jl)

```julia
julia> @saferintegers known = Dict(0=>0, 1=>1)
Dict{SafeInt64, SafeInt64} with 2 entries:
  0 => 0
  1 => 1

julia> fibonacci(SafeInt(2334))   
ERROR: OverflowError: 7540113804746346429 + 4660046610375530309 overflowed for type Int64
Stacktrace:
 [1] throw_overflowerr_binaryop(op::Symbol, x::Int64, y::Int64)
   @ Base.Checked ./checked.jl:155
 [2] checked_add
   @ ./checked.jl:167 [inlined]
 [3] +(x::SafeInt64, y::SafeInt64)
   @ SaferIntegers ~/.julia/packages/SaferIntegers/Xn8ie/src/arith_ops.jl:35
 [4] fibonacci(n::SafeInt64) (repeats 2242 times)

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [March 9, 2023, 10:22am UTC](https://discourse.julialang.org/t/julia-cannot-add-2-big-integers-correctly/95774/6 "2023-03-09T10:22:23Z")

</div>

> [@vsoler](#):
>
> However, I consider that Julia should error in case of overflow, and it doesn’t.

There was a lot of discussion about this in the thread that Oscar linked. Detecting overflow by default comes at considerable expense. In part, catching overflow by default makes vectorization difficult. There is a cost to do what you ask.

What Julia does is give you choices. By default, it lets the processor go as fast as possible. However, we also provide the means to create your own primitive types and change how arithmetic works with them.

Above, the safer integer types have overriden arithmetic with safer arithmetic. It uses checked arithmetic that Oscar mentioned.

[https://docs.julialang.org/en/v1/base/math/#Base.Checked.checked\_add](https://docs.julialang.org/en/v1/base/math/#Base.Checked.checked_add)

```julia
julia> using Base.Checked     
                                      
julia> checked_add(28282828282, typemax(Int64))
ERROR: OverflowError: 28282828282 + 9223372036854775807 overflowed for type Int64
Stacktrace:
 [1] throw_overflowerr_binaryop(op::Symbol, x::Int64, y::Int64)
   @ Base.Checked ./checked.jl:155
 [2] checked_add(x::Int64, y::Int64)
   @ Base.Checked ./checked.jl:167
 [3] top-level scope
   @ REPL[17]:1

```

What I think is amazing about Julia is that I did not have to modify your function. I could change the overflow checking behavior by just changing the types used.

---

<div class="post-metadata">

**Author:** ![vsoler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vsoler/32/30667_2.png) [@vsoler](https://discourse.julialang.org/u/vsoler)\
**Post date:** [March 9, 2023, 11:10am UTC](https://discourse.julialang.org/t/julia-cannot-add-2-big-integers-correctly/95774/7 "2023-03-09T11:10:39Z")

</div>

@mkitti ,

I was not aware of the existence of this package, and in my case, it seems really helpful.

I’ll have a deeper look at it !!!

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [March 9, 2023, 12:55pm UTC](https://discourse.julialang.org/t/julia-cannot-add-2-big-integers-correctly/95774/8 "2023-03-09T12:55:29Z")

</div>

Just to clarify, integer overflow is not a Julia issue. Python numpy and many other languages/framework behave the same, beause as you have been said, checking is expensive. That’s said, you have been given tools that resolve this trade-off in favour of checking, just that is not the default in Julia.
