为什么该划分未正确执行? [英] Why is this division not performed correctly?

查看:98
本文介绍了为什么该划分未正确执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中遇到一个奇怪的问题:除法未正确执行:

I've a strange issue in Python: the division is not performed correctly:

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

这些是结果:

100
50
100
40
0

谢谢

推荐答案

上面的行为对于Python 2是正确的。 code> / 在Python 3中已修复。在Python 2中,您可以使用:

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.

或将数字之一更改为 float

>>> 5.0 / 2
2.5

这篇关于为什么该划分未正确执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆