What defines floating point precision in python?

独自空忆成欢 提交于 2019-12-19 03:24:23

问题


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. 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


回答1:


Please check with the Python documentation:

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




回答2:


It's a peril of using floating-point numbers.

If you need precision at this deep a level, use the Decimal type.

More here: http://docs.python.org/library/decimal.html



来源:https://stackoverflow.com/questions/5164882/what-defines-floating-point-precision-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!