Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算R中的*整数*二项系数_R_Integer_Binomial Coefficients - Fatal编程技术网

计算R中的*整数*二项系数

计算R中的*整数*二项系数,r,integer,binomial-coefficients,R,Integer,Binomial Coefficients,从n中选择k个对象的方法数,即二项式系数n/(k!(n-k)!)是一个整数,当n和k是整数时。如何计算该值以确保结果正确且为整数类型?choose函数返回带整数参数的双偶数: > typeof(choose(4L, 2L)) [1] "double" 与手动计算一样,例如n-choose-2=n(n-1)/2 当然,我可以使用as.integer()强制将其转换为整数,但我担心机器精度: > as.integer(3.999999999999999) [1] 3 >

从n中选择k个对象的方法数,即二项式系数n/(k!(n-k)!)是一个整数,当n和k是整数时。如何计算该值以确保结果正确且为整数类型?
choose
函数返回带整数参数的双偶数:

> typeof(choose(4L, 2L))  
[1] "double"  
与手动计算一样,例如n-choose-2=n(n-1)/2

当然,我可以使用
as.integer()
强制将其转换为整数,但我担心机器精度:

> as.integer(3.999999999999999)
[1] 3
> as.integer(3.9999999999999999)
[1] 4

round()
(默认值为
digits=0
)舍入到最接近的整数,但返回一个双精度类型的值。如果我可以确定,向
as.integer(round(…)
提供一个以双精度格式存储的整数可以保证舍入到正确的整数,而不会被机器精度绊倒,那么
as.integer(round(choose(n,k)))
是可以接受的。是这样吗?或者有没有一种方法可以替代choose()为整数参数返回整数?

一种方法是使用VeryLargeIntegers包。功能是:

比诺姆(n,k)

e、 g.比诺姆(1000,50)甚至比诺姆(10000000,50)

学习如何生成非常大的整数也是明智的cf:as.vli('12345678901213456789')

这个包不是完全没有bug的,更大的计算需要一段时间


Jo博士。

typeof((4L*(4L-1L))%%%2L)
将为您提供整数,如果您选择使用
%%/
而不是
/
,您将获得整数。我不确定这是否是@ChrisW想要的。。。因为
5%/%2=2
事实上,我认为这并不能回答我的问题/(k!(n-k)!)将始终返回一个整数如果n和k是整数,则无需对其进行
取整
操作,只需执行
as.integer(选择(n,k))
。仅当输入为非整数时才需要舍入。
> as.integer(3.999999999999999)
[1] 3
> as.integer(3.9999999999999999)
[1] 4