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

Python中abs()和fabs()方法之间的区别相关推荐

  1. python中序列类型和数组之间的区别_「Python」序列构成的数组

    一.Python 标准库的序列类型分为: 容器序列: 能够存放不同类型数据的序列(list.tuple.collections.deque). 扁平序列: 只能容纳一种类型的数据(str.bytes. ...

  2. java listfiles 使用_Java中list()和listFiles()方法之间的区别

    java.io包的名为File的类表示系统中的文件或目录(路径名).为了获得目录中所有现有文件的列表,此类提供了list()和ListFiles()方法. 它们之间的主要区别是该列表()方法返回一个字 ...

  3. java 方法 函数 区别_Java中的构造函数和方法之间的区别

    Java方法一种方法用于探索对象的行为. 我们可以在方法的前面加上访问修饰符. 方法必须具有返回类型,例如void,任何原始类型(int,char,float等),任何Object类型(Integer ...

  4. python调用包中的方法_python 中不同包 类 方法 之间的调用详解

    目录结构如下: 在hello.py中导入ORM.py这个文件的时候,采用 import ORMPackage.ORM 或者 import ORM u = User(id = 123, name='co ...

  5. Pandas中map,applymap和apply方法之间的区别

    本文翻译自:Difference between map, applymap and apply methods in Pandas Can you tell me when to use these ...

  6. python中permute_Python layers.Permute方法代码示例

    本文整理汇总了Python中keras.layers.Permute方法的典型用法代码示例.如果您正苦于以下问题:Python layers.Permute方法的具体用法?Python layers. ...

  7. python while if 区别_对python中for、if、while的区别与比较方法

    如下所示: if应用举例: #if 若条件成立,只执行一次 #if 条件:如果条件成立,执行条件后的代码块内容,不成立,直接跳过代码块 #判断如果年龄age小于18,输出未成年 #=一个等号表示赋值的 ...

  8. python中类方法与实例方法的区别-Python中的对象,方法,类,实例,函数用法分析...

    本文实例分析了Python中的对象,方法,类,实例,函数用法.分享给大家供大家参考.具体分析如下: Python是一个完全面向对象的语言.不仅实例是对象,类,函数,方法也都是对象. class Foo ...

  9. python grpc 并发_在Python中使用gRPC的方法示例【h】

    本文介绍了在Python中使用gRPC的方法示例,分享给大家,具体如下: 使用Protocol Buffers的跨平台RPC系统. 安装 使用 pip gRPC由两个部分构成,grpcio 和 gRP ...

最新文章

  1. css3动画、2D与3D效果
  2. 简单的bean分页输出
  3. WPS for MacOS如何设置自动编号
  4. 【坑】Sketch算法——Count-Min Sketch和Universal Sketch
  5. Shell expr的用法 bc 命令 let命令
  6. 【Elasticsearch】搜索引擎从0到1 有赞 视频笔记
  7. 3D开发-AR.js Nginx HTTPS服务搭建
  8. python pymysql 下载_Python PyMySQL模块下载和安装
  9. C语言程序设计 课程设计报告
  10. python参数类型为uint8,将图像数据类型从uint16转换为uint8
  11. 当上拉加载更多即ion-content遇上slideBox的时候
  12. r610服务器维修,戴尔服务器R610
  13. centsos7网络连接激活失败_宽带连接时出现711错误的解决方法 | 小马激活官网
  14. odoo 重写unlink方法
  15. 软件与证书之间的关系
  16. hdwiki 软件包结构
  17. 互联网日报 | 5月2日 星期日 | 五一档总票房破5亿;中国联通在香港正式推出5G服务;欧盟首次对苹果发起反垄断诉讼
  18. 工赋开发者社区 | 新一波JavaScript Web框架
  19. oracle数据库dba面试题,DBA笔试题5:SQL汇总
  20. ImageNet 1k and 22k

热门文章

  1. mysql数据库计算全部女生_使用mysql存储过程-统计某个数据库下的所有表的记录数...
  2. 《H5 移动营销设计指南》 读书笔记整理
  3. 基于Vue开发一个日历组件
  4. Background-size完美兼容IE
  5. 什么是css sprites,如何使用?
  6. Zookeeper实现注册与发现
  7. Gson解析Json格式数据
  8. 【Kissy WaterFall】实行手动加载数据
  9. Jquery对复选框的操作
  10. ajax 复制到“剪贴板”