2

我写了一个函数,它应该计算斐波那契数到第 n ^ 步:

fibonnacciNumbers <- function(len) {    
    fibvals <- numeric(len)
    fibvals[1] <- 1
    fibvals[2] <- 1
    for (i in 3:len) { 
        fibvals[i] <- fibvals[i-1]+fibvals[i-2]
    }
    return(fibvals)
}

(fibonnacciNumbers(100))

但是,我的输出如下所示:

 [1] 1.000000e+00 1.000000e+00 2.000000e+00 3.000000e+00 5.000000e+00
  [6] 8.000000e+00 1.300000e+01 2.100000e+01 3.400000e+01 5.500000e+01
 [11] 8.900000e+01 1.440000e+02 2.330000e+02 3.770000e+02 6.100000e+02
 [16] 9.870000e+02 1.597000e+03 2.584000e+03 4.181000e+03 6.765000e+03
 [21] 1.094600e+04 1.771100e+04 2.865700e+04 4.636800e+04 7.502500e+04
 [26] 1.213930e+05 1.964180e+05 3.178110e+05 5.142290e+05 8.320400e+05
 [31] 1.346269e+06 2.178309e+06 3.524578e+06 5.702887e+06 9.227465e+06
 [36] 1.493035e+07 2.415782e+07 3.908817e+07 6.324599e+07 1.023342e+08
 [41] 1.655801e+08 2.679143e+08 4.334944e+08 7.014087e+08 1.134903e+09
 [46] 1.836312e+09 2.971215e+09 4.807527e+09 7.778742e+09 1.258627e+10
 [51] 2.036501e+10 3.295128e+10 5.331629e+10 8.626757e+10 1.395839e+11
 [56] 2.258514e+11 3.654353e+11 5.912867e+11 9.567220e+11 1.548009e+12
 [61] 2.504731e+12 4.052740e+12 6.557470e+12 1.061021e+13 1.716768e+13

为什么会有这些舍入误差?他们来自哪里?

我很感谢你的回答!!!

4

1 回答 1

4

完全没有错误,数字足够大,R 可以更好地使用科学记数法,如果您希望数字不采用科学记数法,请尝试设置print例如的位数print(..., digits=19)。另请注意,您可以通过执行设置选项options(digits=19),然后您可以运行fibonnacciNumbers(100)以获得所需的输出。

> print(fibonnacciNumbers(100), digits=19)
  [1]                     1                     1                     2                     3
  [5]                     5                     8                    13                    21
  [9]                    34                    55                    89                   144
 [13]                   233                   377                   610                   987
 [17]                  1597                  2584                  4181                  6765
 [21]                 10946                 17711                 28657                 46368
 [25]                 75025                121393                196418                317811
 [29]                514229                832040               1346269               2178309
 [33]               3524578               5702887               9227465              14930352
 [37]              24157817              39088169              63245986             102334155
 [41]             165580141             267914296             433494437             701408733
 [45]            1134903170            1836311903            2971215073            4807526976
 [49]            7778742049           12586269025           20365011074           32951280099
 [53]           53316291173           86267571272          139583862445          225851433717
 [57]          365435296162          591286729879          956722026041         1548008755920
 [61]         2504730781961         4052739537881         6557470319842        10610209857723
 [65]        17167680177565        27777890035288        44945570212853        72723460248141
 [69]       117669030460994       190392490709135       308061521170129       498454011879264
 [73]       806515533049393      1304969544928657      2111485077978050      3416454622906707
 [77]      5527939700884757      8944394323791464     14472334024676220     23416728348467684
 [81]     37889062373143904     61305790721611584     99194853094755488    160500643816367072
 [85]    259695496911122560    420196140727489664    679891637638612224   1100087778366101888
 [89]   1779979416004713984   2880067194370816000   4660046610375530496   7540113804746346496
 [93]  12200160415121876992  19740274219868225536  31940434634990100480  51680708854858326016
 [97]  83621143489848426496 135301852344706760704 218922995834555203584 354224848179261997056
于 2013-10-25T18:08:00.083 回答