【问题标题】:Why is there a difference between scipy.special.comb and math.comb?为什么 scipy.special.comb 和 math.comb 之间有区别?
【发布时间】:2021-04-29 09:55:08
【问题描述】:

有人能解释一下为什么这不相等吗?

import scipy
import math
sum(math.comb(250, i) for i in range(0, 251)) == sum(scipy.special.comb(250, i) for i in range(0, 251))

但是,例如,是吗?

sum(math.comb(25, i) for i in range(0, 26)) == sum(scipy.special.comb(25, i) for i in range(0, 26))

谢谢你:)

【问题讨论】:

    标签: python math scipy comb


    【解决方案1】:

    从您发现的documentation 中,您必须像这样将“精确”标志设置为 True:

    scipy.special.comb(250, i, exact=True)
    

    然后您的代码将读取

    import scipy.special as ssp
    import math
    print(sum(math.comb(250, i) for i in range(0, 251)) == sum(ssp.comb(250, i, exact=True) for i in range(0, 251)))
    

    并输出“真”。

    文档说

    精确布尔值,可选

    如果精确为假,则使用浮点精度,否则计算精确长整数。

    【讨论】:

      猜你喜欢
      • 2010-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-08
      • 2018-12-23
      • 2010-11-07
      • 2014-07-20
      相关资源
      最近更新 更多