溫馨提示×

python如何計算冪函數

九三
1458
2021-02-18 09:25:22
欄目: 編程語言

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 <1 and judgeif abs(judge-x)<0.0000001: # 判斷是否達到精度要求

print(pow(x,1/n)) # pow函數計算結果

return r

else:

if judge>x:

high = r

else:

low = r

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女