Fibonacci numbers becoming negative after a certain term

前端 未结 1 1567
隐瞒了意图╮
隐瞒了意图╮ 2020-12-21 19:06

I wrote this program in Fortran to display the Fibonacci numbers up to the x-th term:

program fibonacci
implicit none
integer :: x,p,c,i,t !initializes limit         


        
相关标签:
1条回答
  • 2020-12-21 19:21

    I'm no fortran expert, but I did take a class once...

    Apparently you're using a four byte integer (http://www-classes.usc.edu/engr/ce/108/text/fbk01.htm) for these. After, 1836311903 you're exceeding the maximum integer value (2147483647) and the calculations are overflowing.

    You have two options for calculating fibonacci numbers with more precision. First, you can find a system/fortran compiler combination that support 8 or 16 byte integers. Support in gfortran at least, seems to by system specific. Another option is to use a multiple precision library like gmp.

    0 讨论(0)
提交回复
热议问题