简体   繁体   English

什么在python中定义浮点精度?

[英]What defines floating point precision in python?

I learnt of the "exactly equal to" operator in Erlang, which compares not only values , but also data types of numbers, and I was curious about how things work in Python and its lone "equals to" operator. 我了解到Erlang中的“完全等于”运算符,该运算符不仅比较值 ,而且还比较数字的数据类型,我很好奇Python中的工作方式及其唯一的“等于”运算符。 So after making sure that 所以在确保

>>> 1 == 1.0 
True

I wondered about the floating point precision, and got to this 我想知道浮点精度,并达到了

>>> 0.9999999999999999 == 1
False
>>> 0.99999999999999999 == 1
True
>>>

Could someone explain how floating point precision is determined here? 有人可以解释一下如何确定浮点精度吗? It works the same in both 2.7.1 and 3.1.2 它在2.7.1和3.1.2中的工作原理相同

Please check with the Python documentation: 请检查Python文档:

http://docs.python.org/tutorial/floatingpoint.html http://docs.python.org/tutorial/floatingpoint.html

It's a peril of using floating-point numbers. 使用浮点数会带来危险。

If you need precision at this deep a level, use the Decimal type. 如果您需要此深度级别的精度,请使用Decimal类型。

More here: http://docs.python.org/library/decimal.html 此处更多信息: http : //docs.python.org/library/decimal.html

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

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