Python中abs()和fabs()方法之间的区别
In python, abs() method and fabs() method both are used to find the absolute value of a number. They are used for the same purpose but they have a difference, which we are going to discuss in this tutorial.
在python中, abs()方法和fabs()方法都用于查找數(shù)字的絕對值。 它們用于相同的目的,但有區(qū)別,我們將在本教程中討論。
Python abs()方法 (Python abs() method)
abs() method is a built-in method in python – it accepts a number and returns the absolute value of the given number.
abs()方法是python中的內(nèi)置方法–它接受一個(gè)數(shù)字并返回給定數(shù)字的絕對值。
abs() method can accept positive or negative integer value, positive or negative float value and returns an absolute value based on the given number type. If the number is an integer it returns an integer value, if the number is a float it returns a float value.
abs()方法可以接受正或負(fù)整數(shù)值,正或負(fù)浮點(diǎn)值,并根據(jù)給定的數(shù)字類型返回絕對值。 如果數(shù)字是整數(shù),則返回整數(shù)值;如果數(shù)字是浮點(diǎn)數(shù),則返回浮點(diǎn)值。
Syntax:
句法:
abs(n)Python code to demonstrate example of abs() method
Python代碼演示abs()方法的示例
# Python code to demonstrate example of # abs() methoda = 10 # +ve integer b = -10 # -ve integer c = 10.23 # +ve float d = -10.23 # -ve float# printing absolute values using abs() method print("abs(a): ", abs(a)) print("abs(b): ", abs(b)) print("abs(c): ", abs(c)) print("abs(d): ", abs(d))Output
輸出量
abs(a): 10 abs(b): 10 abs(c): 10.23 abs(d): 10.23See the output – Values of a and b are integers, thus, their absolute values are also integers. Values of c and d are floats, thus, their absolute values are also float values.
看到輸出- a和b的值是整數(shù),因此,它們的絕對值也整數(shù)。 c和d的值是浮點(diǎn)數(shù),因此它們的絕對值也是浮點(diǎn)數(shù)。
Python fabs()方法 (Python fabs() method)
fabs() method is also a built-in function but it is defined in math module, so to use fabs() method, we need to import math module first.
fabs()方法也是一個(gè)內(nèi)置函數(shù),但是它是在math模塊中定義的,因此要使用fabs()方法 ,我們需要首先導(dǎo)入math模塊 。
The fabs() method is also used to find the absolute value of a given number, it also accepts a number and returns the absolute value of the given number.
fabs()方法還用于查找給定數(shù)字的絕對值,它還接受一個(gè)數(shù)字并返回給定數(shù)字的絕對值。
fabs() method can accept positive or negative integer value, positive or negative float value and returns the absolute value of float type.
fabs()方法可以接受正或負(fù)整數(shù)值,正或負(fù)浮點(diǎn)值并返回浮點(diǎn)類型的絕對值。
Syntax:
句法:
math.fabs(n)Python code to demonstrate example of fabs() method
Python代碼演示fabs()方法的示例
# Python code to demonstrate example of # fabs() method# importing math module import math a = 10 # +ve integer b = -10 # -ve integer c = 10.23 # +ve float d = -10.23 # -ve float# printing absolute values using abs() method print("fabs(a): ", math.fabs(a)) print("fabs(b): ", math.fabs(b)) print("fabs(c): ", math.fabs(c)) print("fabs(d): ", math.fabs(d))Output
輸出量
fabs(a): 10.0 fabs(b): 10.0 fabs(c): 10.23 fabs(d): 10.23abs()和fabs()方法之間的區(qū)別 (Difference between abs() and fabs() methods )
There are mainly two differences between abs() and fabs() methods,
abs()和fabs()方法之間主要有兩個(gè)區(qū)別 ,
abs() method is a standard built-in method, for this, there is no need to import a module. But, the fabs() method is defined in the math module, for this we need to import the math module first.
abs()方法是標(biāo)準(zhǔn)的內(nèi)置方法,為此,無需導(dǎo)入模塊。 但是, fabs()方法是在math模塊中定義的,為此,我們需要首先導(dǎo)入math模塊。
abs() method returns either an integer value or a float value based on given number type. But, fabs() method returns only float value, no matter given number is an integer type or a float type.
abs()方法根據(jù)給定的數(shù)字類型返回整數(shù)值或浮點(diǎn)值。 但是,無論給定的數(shù)字是整數(shù)類型還是浮點(diǎn)類型, fabs()方法都只返回浮點(diǎn)值。
翻譯自: https://www.includehelp.com/python/abs-vs-fabs-methods.aspx
總結(jié)
以上是生活随笔為你收集整理的Python中abs()和fabs()方法之间的区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 千足金回收多少钱一克啊?
- 下一篇: 哈尔滨治疗输卵管堵塞最好的医院推荐