日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python中abs()和fabs()方法之间的区别

發(fā)布時間:2023/12/1 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python中abs()和fabs()方法之间的区别 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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)置方法–它接受一個數(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()方法可以接受正或負整數(shù)值,正或負浮點值,并根據(jù)給定的數(shù)字類型返回絕對值。 如果數(shù)字是整數(shù),則返回整數(shù)值;如果數(shù)字是浮點數(shù),則返回浮點值。

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.23

See 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的值是浮點數(shù),因此它們的絕對值也是浮點數(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()方法也是一個內(nèi)置函數(shù),但是它是在math模塊中定義的,因此要使用fabs()方法 ,我們需要首先導入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ù)字的絕對值,它還接受一個數(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()方法可以接受正或負整數(shù)值,正或負浮點值并返回浮點類型的絕對值。

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.23

abs()和fabs()方法之間的區(qū)別 (Difference between abs() and fabs() methods )

There are mainly two differences between abs() and fabs() methods,

abs()和fabs()方法之間主要有兩個區(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()方法是標準的內(nèi)置方法,為此,無需導入模塊。 但是, fabs()方法是在math模塊中定義的,為此,我們需要首先導入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ù)值或浮點值。 但是,無論給定的數(shù)字是整數(shù)類型還是浮點類型, fabs()方法都只返回浮點值。

  • 翻譯自: https://www.includehelp.com/python/abs-vs-fabs-methods.aspx

    總結(jié)

    以上是生活随笔為你收集整理的Python中abs()和fabs()方法之间的区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。