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()方法都用于查找数字的绝对值。 它们用于相同的目的,但有区别,我们将在本教程中讨论。

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中的内置方法–它接受一个数字并返回给定数字的绝对值。

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()方法可以接受正或负整数值,正或负浮点值,并根据给定的数字类型返回绝对值。 如果数字是整数,则返回整数值;如果数字是浮点数,则返回浮点值。

Syntax:

句法:

    abs(n)

Python code to demonstrate example of abs() method

Python代码演示abs()方法的示例

# Python code to demonstrate example of 
# abs() method

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("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的值是整数,因此,它们的绝对值也整数。 c和d的值是浮点数,因此它们的绝对值也是浮点数。

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()方法也是一个内置函数,但是它是在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()方法还用于查找给定数字的绝对值,它还接受一个数字并返回给定数字的绝对值。

fabs() method can accept positive or negative integer value, positive or negative float value and returns the absolute value of float type.

fabs()方法可以接受正或负整数值,正或负浮点值并返回浮点类型的绝对值。

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()方法之间的区别 (Difference between abs() and fabs() methods )

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

abs()和fabs()方法之间主要有两个区别

  1. 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()方法是标准的内置方法,为此,无需导入模块。 但是, fabs()方法是在math模块中定义的,为此,我们需要首先导入math模块。

  2. 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()方法根据给定的数字类型返回整数值或浮点值。 但是,无论给定的数字是整数类型还是浮点类型, fabs()方法都只返回浮点值。

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

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值