0

当我在 R 中逐行打印向量时cat(),结果的舍入方式与通常的输出不同:

> dbinom(0:10, 10, 0.95)
 [1] 9.765625e-14 1.855469e-11 1.586426e-09 8.037891e-08 2.672599e-06
 [6] 6.093525e-05 9.648081e-04 1.047506e-02 7.463480e-02 3.151247e-01
[11] 5.987369e-01
> options(scipen=999)
> dbinom(0:10, 10, 0.95)
 [1] 0.00000000000009765625 0.00000000001855468750 0.00000000158642578125
 [4] 0.00000008037890625000 0.00000267259863281252 0.00006093524882812524
 [7] 0.00096480810644531680 0.01047505944140628489 0.07463479852001964066
[10] 0.31512470486230492739 0.59873693923837867370
> cat(dbinom(0:10, 10, 0.95), sep = "\n")
0.00000000000009765625
0.00000000001855469
0.000000001586426
0.00000008037891
0.000002672599
0.00006093525
0.0009648081
0.01047506
0.0746348
0.3151247
0.5987369

如何保留小数位?

4

1 回答 1

1

试试这个sprintf

> cat(sprintf("%.20f", dbinom(0:10, 10, 0.95)),sep="\n")
0.00000000000009765625
0.00000000001855468750
0.00000000158642578125
0.00000008037890625000
0.00000267259863281251
0.00006093524882812514
0.00096480810644531680
0.01047505944140628489
0.07463479852001966841
0.31512470486230481637
0.59873693923837867370

我还应该提到,使用浮点计算,任何超过 15 位的精度可能充其量都是虚假的。请注意,0.31512470486230492739您的数据和0.31512470486230481637我的数据不匹配超过 15 位。

于 2013-06-24T06:39:31.410 回答