+ 33

Code problem or ... ? (fibonacci series)

Why after the 46th number, it resualts negative number ? 🤔 even it doesn't calculate properly after 46th number https://code.sololearn.com/caP8649IZ2fH/?ref=app

29th Mar 2018, 6:48 AM
AliR૯za
AliR૯za - avatar
5 odpowiedzi
+ 5
Try using #include <stdint.h> and uint64_t instead of int so you can use the maximum number an int can store
29th Mar 2018, 7:01 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 3
This will be due to an overflow error. Data types have a limit due to the way they store numbers, after the 46th you kind of 'loop around' into the negatives. Try using a long data type instead - however this has a limit too and at some point will suffer from the same issue
29th Mar 2018, 6:59 AM
Dan Walker
Dan Walker - avatar
+ 3
~ swim ~ I often used those long and short data types, but with the standardized types it's way easier to understand and maintain the code because you see directly how large every int it is
30th Mar 2018, 5:55 AM
Aaron Eberhardt
Aaron Eberhardt - avatar