python中负数的开方问题

Python中开方有好多方法 ** math

import math
print(8 ** 1/3)  # 2.6666666666666665
print(8 ** 1.0/3)  # 2.6666666666666665
print(8.0 ** 1/3)  # 2.6666666666666665

print(math.pow(8, 1/3))  # 2.0
print(math.pow(8, 1.0/3))  # 2.0

# 负数的开方要先加上绝对值
print(math.pow(abs(-8), 1/3) * (-1) if -8 < 0 else 1)  # -2.0
发布了46 篇原创文章 · 获赞 7 · 访问量 1083

猜你喜欢

转载自blog.csdn.net/qq_38888209/article/details/105696372