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:
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”.
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 …
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.
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.
- Do you think that I am missing any important points?
- The results that I am getting seem to me incoherent. What’s your opinion?
I appreciate any comments.