Why is this division not performed correctly?

我是研究僧i 提交于 2019-11-28 01:09:02
user225312

The above behavior is true for Python 2. The behavior of / was fixed in Python 3. In Python 2 you can use:

from __future__ import division

and then use / to get the result you desire.

>>> 5 / 2
2
>>> from __future__ import division
>>> 5 / 2
2.5

Since you are dividing two integers, you get the result as an integer.

Or, change one of the numbers to a float.

>>> 5.0 / 2
2.5

It is done correctly.

50/60 = 0

Maybe you are looking for 50.0/60.0 = 0.83333333333333337, you can cast your variables to float to get that:

print  float(pointB[1]-pointA[1]) / (pointB[0]-pointA[0])

This is how integer division works in python. Either use floats or convert to float in your calculation:

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