Python 的小数精度与 C 相比如何?
我正在查看黄金比例公式 找到第 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
几乎所有平台都将 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
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.