Python 中的fabs
方法用於返回一個數字的絕對值。
可以通過導入數學模塊來使用。
Python 中的數學模塊可以用來實現不同的基本數學運算,如加法、減法、除法和乘法。
它還可以用於執行涉及指數、對數和三角學的計算。
句法
在 Python 中使用 fabs 的語法是-
math.fabs(x)
是時候看看一些程序,以便更好地理解。
計劃 1:
在第一個節目中,我們將討論晶圓廠方法的基礎。
#working of fabs()
import math
# initializing the variables a, b, and c
a = -20.3
b = -30.66
c = 19.13
#printing the values
print ("The fabs of a is: ", math.fabs(a))
print ("The fabs of b is: ", math.fabs(b))
print ("The fabs of c is: ", math.fabs(c))
輸出:
The fabs of a is: 20.3
The fabs of b is: 30.66
The fabs of c is: 19.13
說明:
讓我們來看看這個程序的解釋-
- 因為要用晶圓廠(),所以導入了
math
模塊。 - 之後,我們用正負值初始化變數 a、b 和 c。
- 下一步,我們將這些值傳遞給晶圓廠()並列印出來。
- 在執行程序時,它會顯示所需的輸出。
程序 2:
在下一個程序中,我們將對這些涉及晶圓廠()使用的數字執行一些基本操作。
下面的程序說明了同樣的情況-
#working of fabs()
import math
# initializing the variables a, b, and c
a = -20.3
b = -30.66
c = 19.13
# calculating the sum
print ("The sum of a and b is: ", (a + b))
print ("The sum of b and c is: ", (b + c))
# printing the values
d = math.fabs(a)
e = math.fabs(b)
f = math.fabs(c)
#calculating sum after fabs
print ("The sum of d and e is: ", (d + e))
print ("The sum of e and f is: ", (e + f))
輸出:
The sum of a and b is: -50.96
The sum of b and c is: -11.530000000000001
The sum of d and e is: 50.96
The sum of e and f is: 49.79
說明:
讓我們看看我們在上面的程序中做了什麼-
- 既然要用 fabs(),我們導入了
math
模塊。 - 之後,我們用正負值初始化變數 a、b 和 c。
- 在下一步中,我們已經列印了 a + b 和 b + c 的總和。
- 現在我們已經在 fabs() 中傳遞了這三個變數,並將返回值存儲在變數 d、e 和 f 中。
- 我們再次計算總和,以檢查在進入晶圓廠之前和之後使用評估值獲得的結果之間的差異。
- 在執行程序時,它會顯示所需的輸出。
計劃 3:
最後,在最後一個程序中,我們將對數字再進行一次數學運算。
#working of fabs()
import math
#initialising the variables a, b and c
a = -20.3
b = -30.66
c = 19.13
#calculating the product
print ("The product of a and b is: ", (a * b))
print ("The product of b and c is: ", (b * c))
#printing the values
d = math.fabs(a)
e = math.fabs(b)
f = math.fabs(c)
#calculating product after fabs
print ("The product of d and e is: ", (d * e))
print ("The product of e and f is: ", (e * f))
輸出:
The product of a and b is: 622.398
The product of b and c is: -586.5258
The product of d and e is: 622.398
The product of e and f is: 586.5258
說明:
讓我們來看看這個程序的解釋-
- 既然要用 fabs(),我們導入了
math
模塊。 - 之後,我們用正負值初始化變數 a、b 和 c。
- 在下一步中,我們已經列印了 a b 和 b c 的產品。
- 現在我們已經在 fabs() 中傳遞了這三個變數,並將返回值存儲在變數 d、e 和 f 中。
- 我們再次計算了產品,以檢查在進入晶圓廠之前和之後使用評估值獲得的結果之間的差異。
- 在執行程序時,它會顯示所需的輸出。
結論
在本教程中,我們學習了晶圓廠()以及如何在 Python 程序中使用它們來使我們的工作更加輕鬆。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/278429.html