Python 的小数精度与 C 相比如何?

发布于 2024-09-08 07:13:53 字数 264 浏览 2 评论 0原文

我正在查看黄金比例公式 找到第 n 个斐波那契数,这让我很好奇。

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

I was looking at the Golden Ratio formula for finding the nth Fibonacci number, and it made me curious.

I know Python handles arbitrarily large integers, but what sort of precision do you get with decimals? Is it just straight on top of a C double or something, or does it use a a more accurate modified implementation too? (Obviously not with arbitrary accuracy. ;D)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光与爱终年不遇 2024-09-15 07:13:53

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

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

还有用于任意精度浮点数学的十进制模块

almost all platforms map Python floats to IEEE-754 “double precision”.

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

there's also the decimal module for arbitrary precision floating point math

嘴硬脾气大 2024-09-15 07:13:53

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

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

Python 2.6 还添加了 分数模块,它可能更适合某些问题。

这两种方法都会比使用浮点类型慢,但这是提高精度的代价。

Python floats use the double type of the underlying C compiler. As Bwmat says, this is generally IEEE-754 double precision.

However if you need more precision than that you can use the Python decimal module which was added in Python 2.4.

Python 2.6 also added the fraction module which may be a better fit for some problems.

Both of these are going to be slower than using the float type, but that is the price for more precision.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文