【问题标题】:Does the julia round function contain an error for large numbers?julia round 函数是否包含大数的错误?
【发布时间】:2018-09-04 20:39:54
【问题描述】:

julia round 函数在阶乘 (75) 之前似乎可以正常工作,但在阶乘 76 处中断。这是 round 函数中的错误吗?

julia>round(factorial(big(75)), sigdigits=2)
2.5e+109

julia>round(factorial(big(76)), sigdigits=2)
1.900000000000000000000000000000000000000000000000000000000000000000000000000006e+111

【问题讨论】:

    标签: julia precision biginteger digits arbitrary-precision


    【解决方案1】:

    您必须提高 BigFloat 计算的精度才能获得正确的结果,例如像这样:

    julia> setprecision(1000) do
           round(factorial(big(76)), sigdigits=2)
           end
    1.9e+111
    

    问题的根源在于,当舍入 Julia 时,将 {base}^{number of digits to round} 表示为适当的浮点数。在这种情况下,BigFloat(10)^-110 在默认精度下对于所需的位数来说不够精确。

    【讨论】:

    • 这是一个很好的答案,准确地解释了我的问题。
    猜你喜欢
    • 2023-03-28
    • 2016-07-20
    • 2017-11-03
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-13
    相关资源
    最近更新 更多