【问题标题】:How does the decimal accuracy of Python compare to that of C?Python 的小数精度与 C 相比如何?
【发布时间】:2011-03-13 04:38:55
【问题描述】:

我正在查看 Golden Ratio formula 寻找第 n 个斐波那契数,这让我很好奇。

我知道 Python 可以处理任意大的整数,但是小数的精度如何?它是直接在 C double 或其他东西之上,还是它也使用了更准确的修改实现? (显然不是任意准确的。;D)

【问题讨论】:

    标签: python c programming-languages floating-accuracy language-implementation


    【解决方案1】:

    几乎所有平台都将 Python 浮点数映射到 IEEE-754 “双精度”。

    http://docs.python.org/tutorial/floatingpoint.html#representation-error

    还有用于任意精度浮点数学的decimal module

    【讨论】:

      【解决方案2】:

      Python 浮点数使用底层 C 编译器的 double 类型。正如 Bwmat 所说,这通常是 IEEE-754 双精度。

      但是,如果您需要更高的精度,可以使用 Python 2.4 中添加的 Python decimal module

      Python 2.6 还添加了fraction module,这可能更适合一些问题。

      这两者都将比使用浮点类型慢,但这是更精确的代价。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-08-04
        • 1970-01-01
        • 1970-01-01
        • 2014-02-08
        • 2018-09-17
        • 1970-01-01
        • 2018-07-01
        • 2015-10-31
        相关资源
        最近更新 更多