简体   繁体   English

Python与C的十进制精度相比如何?

[英]How does the decimal accuracy of Python compare to that of C?

I was looking at the Golden Ratio formula for finding the nth Fibonacci number, and it made me curious. 我一直在寻找黄金比例公式来找到第n个斐波那契数,这让我感到好奇。

I know Python handles arbitrarily large integers, but what sort of precision do you get with decimals? 我知道Python可以处理任意大的整数,但是小数会带来什么样的精度? Is it just straight on top of a C double or something, or does it use aa more accurate modified implementation too? 它只是直接在C double之类的东西之上,还是使用更精确的修改实现? (Obviously not with arbitrary accuracy. ;D) (显然不是任意精度的。

almost all platforms map Python floats to IEEE-754 “double precision”. 几乎所有平台都将Python浮点数映射为IEEE-754“双精度”。

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

there's also the decimal module for arbitrary precision floating point math 还有用于任意精度浮点数学的十进制模块

Python floats use the double type of the underlying C compiler. Python浮点型使用基础C编译器的double类型。 As Bwmat says, this is generally IEEE-754 double precision. 正如Bwmat所说,这通常是IEEE-754的双精度。

However if you need more precision than that you can use the Python decimal module which was added in Python 2.4. 但是,如果您需要的精度更高,则可以使用Python 2.4中添加的Python 十进制模块

Python 2.6 also added the fraction module which may be a better fit for some problems. Python 2.6还添加了分数模块 ,该模块可能更适合某些问题。

Both of these are going to be slower than using the float type, but that is the price for more precision. 这两种方法都将比使用float类型的方法慢,但这就是提高精度的代价。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM