为了找到距离,我使用 **0.5 来求平方根。这会返回负数吗?
def distance(x1, y1, x2, y2):
return ((x1-x2)**2 + (y1-y2)**2)**0.5
最佳答案
是的,该函数可以返回负数。这需要足够 splinter 的输入参数。
class Whack(int):
def __pow__(self, other, modulo=None):
if other == 0.5:
return -42
return Whack()
def __sub__(self, other):
return Whack()
__add__ = __sub__
演示:
>>> x1 = y1 = x2 = y2 = Whack()
>>> distance(x1, y1, x2, y2)
-42
如果你限制传入“正常”数字,例如 python 的内置 float 或整数,那么答案是否定的它不能返回负数,因为它们不在数学运算的范围内 **0.5
。
关于python - **0.5 可以返回负数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37468746/