Linux 拨号vps windows公众号手机端

python如何计算幂函数

lewis 6年前 (2019-03-29) 阅读数 10 #程序编程
文章标签 python幂函数

在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

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门