python - **0.5 可以返回负数吗?

标签 python python-2.7

为了找到距离,我使用 **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/

相关文章:

python - 在 Sagemath 中积分并绘制分段函数

shell - Python,捕获 shell 命令的整个输出

python - 如何在 Python 的 Windows 机器上将 CRLF 转换为 LF

python - 轮询最大 wait_time,除非 python 2.7 中的条件

python - 更改音频数据时出现噪音

Python Webdrivermanager .install() 不适用于自定义 Webdriver 安装位置中的 Edge

python - 类型错误 : conv2d_v2() got an unexpected keyword argument 'filter

python - 在 python 中使用边界框更新 OpenCV 跟踪器

python - DNS 响应未到达目的地

python - 模块已安装但无法导入