目录 一, math模块 2. math库常用函数 3.math库使用示例 二, decimal模块 1. 什么时候使用decimal 2. 使用decimal 3. decimal使用示例 一, math模块 math库是python提供的内置数学类函数库,math库不支
          目录
- 一, math模块
- 2. math库常用函数
- 3.math库使用示例
- 二, decimal模块
- 1. 什么时候使用decimal
- 2. 使用decimal
- 3. decimal使用示例
一, math模块
math库是python提供的内置数学类函数库,math库不支持复数类型,仅支持整数和浮点数运算。
math.pimath.pi输出结果:3.141592653589793math.emath.e输出结果:2.718281828459045math.inf-math.inf是负无穷大
math.inf输出 infmath.nanmath.nan输出结果:nan2. math库常用函数
math.ceil(f)math.floor(f)round(f)math.fabs(f)abs(num)math.fmod(x,y)math.pow(x,n)math.sqrt(num)fsum(seq)sum(seq)math.modf(num)math.trunc(f)math.copysign(n1,n1)math.factorial(x)math.gcd(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)
到此这篇关于python数学模块(math/decimal模块)的文章就介绍到这了,更多相关python数学模块内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!
