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不是整數或為負數將引發(fā)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有助于金融應用和其它需要精確十進制表達的場合,控制精度,控制舍入以適應法律或者規(guī)定要求,確保十進制數位精度,或者用戶希望計算結果與手算相符的場合。
Decimal重現了手工的數學運算,確保了二進制浮點數無法精確保有的數據精度。高精度使Decimal可以執(zhí)行二進制浮點數無法進行的模運算和等值測試。
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數學模塊內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
前女友發(fā)來加密的"520快樂.pdf",我用python破解開之后,卻發(fā)現
520收到前女友發(fā)來的加密PDF文件,說打開之后有驚喜,難道是要復合?我用python破解開之后,卻發(fā)現...python干貨+劇情滿滿收藏收藏2021-08-08
通過python爬蟲mechanize庫爬取本機ip地址的方法
python中的mechanize算是一個比較古老的庫了,在python2的時代中,使用的多一些,在python3以后就很少使用了,現在已經是2202年了,可能很多人都沒聽說過mechanize,這不要緊,我們先來簡單的講解一下,如何使用mechanize,感興趣的朋友一起看看吧2022-08-08

