python如何计算幂函数

655Z技术栈 Python编程 3小时前 4

在python中使用pow函数计算幂函数,具体方法如下:

pow:pow()函数的作用是用于计算 x 的 n 次幂函数,其中n为整数。

pow()函数使用方法:

class Solution:

def myPow(self, x: float, n: int) -> float:

# x为大于0的数,因为负数无法开平方(不考虑复数情况)

if x>1:

low,high = 0,x

else:

low,high =x,1

while True:

r = (low+high)/2

judge = 1

for i in range(n):

judge *= r

if x >1 and judge>x:break # 对于大于1的数,如果当前值已经大于它本身,则无需再算下去

if x if abs(judge-x)print(pow(x,1/n)) # pow函数计算结果

return r

else:

if judge>x:

high = r

else:

low = r

提供PHP及ThinkPHP框架的定制开发、代码优化,PHP修改、ThinkPHP修改。

邮箱:yvsm@163.com 微信:yvsm316 QQ:316430983
关闭

用微信“扫一扫”