python数学模块(math/decimal模块)
mylove136 · 181浏览 · 发布于2022-09-28
这篇文章主要介绍了python数学模块(math/decimal模块),文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
一, math模块
math库是python提供的内置数学类函数库,math库不支持复数类型,仅支持整数和浮点数运算。
常数 | 说明 | 实例 |
---|---|---|
math.pi | 圆周率Π | math.pi输出结果:3.141592653589793 |
math.e | 自然常数e | math.e输出结果:2.718281828459045 |
math.inf | 正无穷大, -math.inf是负无穷大 | math.inf输出 inf |
math.nan | 非浮点数标记,NaN | math.nan输出结果:nan |
2. math库常用函数
函数名 | 说明 |
---|---|
math.ceil(f) | 向上取整,返回值:整数值 |
math.floor(f) | 向下取整,返回值:整数 |
round(f) | 四舍五入,返回值:整数 |
math.fabs(f) | 获取绝对值操作,返回值:浮点数 |
abs(num) | 获取绝对值操作,返回值:根据传入的参数而定 |
math.fmod(x,y) | 返回x/y的余数,返回值:浮点数 |
math.pow(x,n) | 返回x的n次方,返回值:浮点型 |
math.sqrt(num) | 对num开平方,返回值:浮点数 |
fsum(seq) | 返回序列中所有元素的和,返回值:浮点数 |
sum(seq) | 将一个序列的数值进行相加求和,返回值:根据序列中数值的类型变化 |
math.modf(num) | 将一个浮点数拆成小数和整数部分组成的元组,返回值:元组 |
math.trunc(f) | 返回浮点数的整数部分,返回值:整数 |
math.copysign(n1,n1) | 将第二个数的正负号赋值给第一个数,返回值:浮点数 |
math.factorial(x) | 返回x的阶乘,如果x不是整数或为负数将引发ValueError,返回值:整数 |
math.gcd(x,y) | 返回整数x和y的最大公约数,返回值:整数 |
3.math库使用示例
# -*- coding: utf-8 -*- import math # math库常用变量 print("math.pi = ", math.pi) print('math.e = ', math.e) print('math.inf = ', math.inf) print('math.nan = ', math.nan) # math库常用函数 print('math.ceil()向上取整,math.ceil(2.3) = ', math.ceil(2.3)) print('math.ceil()向上取整,math.ceil(2.5) = ', math.ceil(2.5)) print('math.ceil()向上取整,math.ceil(2.0) = ', math.ceil(2.0)) print('math.ceil()向上取整,math.ceil(2.8) = ', math.ceil(2.8)) print('math.ceil()向上取整,math.ceil(-2.8) = ', math.ceil(-2.8)) print('math.floor()向下取整,math.floor(2.3) = ', math.floor(2.3)) print('math.floor()向下取整,math.floor(2.5) = ', math.floor(2.5)) print('math.floor()向下取整,math.floor(2.0) = ', math.floor(2.0)) print('math.floor()向下取整,math.floor(2.8) = ', math.floor(2.8)) print('math.floor()向下取整,math.floor(-2.8) = ', math.floor(-2.8)) print('round()四舍五入,round(2.3) = ', round(2.3)) print('round()四舍五入,roundr(2.5) = ', round(2.5)) print('round()四舍五入,round(2.0) = ', round(2.0)) print('round()四舍五入,round(2.8) = ', round(2.8)) print('round()四舍五入,round(-2.8) = ', round(-2.8)) print('math.fabs()获取绝对值,math.fabs(2.3) = ', math.fabs(2.3)) print('math.fabs()获取绝对值,math.fabs(-2.3) = ', math.fabs(-2.3)) print('math.fabs()获取绝对值,math.fabs(-2.0) = ', math.fabs(-2.0)) print('math.fabs()获取绝对值,math.fabs(-2) = ', math.fabs(-2)) print('abs()获取绝对值,abs(2.3) = ', abs(2.3)) print('abs()获取绝对值,abs(-2.3) = ', abs(-2.3)) print('abs()获取绝对值,abs(-2.0) = ', abs(-2.0)) print('abs()获取绝对值,abs(-2) = ', abs(-2)) print('math.fmod(x,y)获取x/y的余数,math.fmod(2,3) = ' ,math.fmod(2,3)) print('math.pow(x,y)获取x的n次方,math.pow(2,3) = ', math.pow(2,3)) print('math.sqrt()获取开放根,math.sqrt(4) = ', math.sqrt(4)) print('fsum()获取序列中所有元素的和,fsum([1,2,3,4,5,6]) = ', math.fsum([1,2,3,4,5,6])) print('sum()获取序列中所有元素的和,sum([1,2,3,4,5,6]) = ', sum([1,2,3,4,5,6])) print('math.modf()获取浮点数的小数和整数部分,math.modf(2.3) = ', math.modf(2.3)) print('math.trunc()获取浮点数的整数部分,math.trunc(2.3) = ', math.trunc(2.3)) print('math.copysign(n1,n2)把第二个数的正负号赋值给第一个浮点数,math.copysign(-2.3,1) = ', math.copysign(-2.3,1)) print('math.copysign(n1,n2)把第二个数的正负号赋值给第一个浮点数,math.copysign(2.3,-1) = ', math.copysign(2.3,-1)) print('math.gcd(x,y)获取x和y的最大公约数,math.gcd(16,24) = ', math.gcd(16,24)) try: print('math.factorial()获取阶乘,math.factorial(3) = ', math.factorial(3)) print('math.factorial()获取阶乘,math.factorial(2.3) = ', math.factorial(2.3)) print('math.factorial()获取阶乘,math.factorial(-2) = ', math.factorial(-2)) except ValueError as e: print(e) finally: pass
二, decimal模块
decimal模块提供了一个Decimal数据类型用于浮点数计算。相比内置的二进制浮点数实现float,Decimal有助于金融应用和其它需要精确十进制表达的场合,控制精度,控制舍入以适应法律或者规定要求,确保十进制数位精度,或者用户希望计算结果与手算相符的场合。
Decimal重现了手工的数学运算,确保了二进制浮点数无法精确保有的数据精度。高精度使Decimal可以执行二进制浮点数无法进行的模运算和等值测试。
1. 什么时候使用decimal
python中小数相加可能计算结果不对,是由于科学计算精度问题,如果需要处理这个问题就需要用到decimal模块。
2. 使用decimal
设置精度:decimal.getcontext().prec = num,num为有效数字个数
设置小数位数:quantize()
注意:decimal.getcontext().prec 和 quantize()不能同时使用,如果同时使用会提示错误:decimal.InvalidOperation: [<class ‘decimal.InvalidOperation’>]
3. decimal使用示例
# -*- coding: utf-8 -*- import decimal """ decimal.getcontext().prec = 3 # 设置有效数字是3位 print(decimal.Decimal(2.32) + decimal.Decimal(3.01)) decimal.getcontext().prec = 2 # 设置有效数字是2位 print(decimal.Decimal(2.32) + decimal.Decimal(3.01)) """ # quantize()设置小数位数 num = decimal.Decimal(1.23456789).quantize(decimal.Decimal('0.000')) print(num)
相关推荐
PHP实现部分字符隐藏
沙雕mars · 1322浏览 · 2019-04-28 09:47:56
Java中ArrayList和LinkedList区别
kenrry1992 · 906浏览 · 2019-05-08 21:14:54
5月语言排行榜:R 跌出前二十,Python 紧咬 C++
manongba · 685浏览 · 2019-05-09 17:27:24
Tomcat 下载及安装配置
manongba · 966浏览 · 2019-05-13 21:03:56
什么是SpringBoot
iamitnan · 1084浏览 · 2019-05-14 22:20:36
分类专栏
最新发布
最热排行
0评论