如何防止结果成为复数? (-27 的立方根)

How can I prevent the result from being a complex number? (Cube root of -27)

我找到了以下解决方案(反正可能并不难):

n1, n2 = -27, 3
root = ( n1 ** (1/n2))
print(root)

(1.5000000000000004+2.598076211353316j)

除非我犯了一个非常愚蠢的错误,否则它应该打印 -3。

要求负数的实立方根,请使用:

number = -27
if number > 0:
    real_root = number ** (1/3)
elif number < 0:
    real_root = - (abs(number) ** (1/3))
elif number == 0:
    real_root = 0