python數(shù)學(xué)模塊(math/decimal模塊)
一, math模塊
math庫是python提供的內(nèi)置數(shù)學(xué)類函數(shù)庫,math庫不支持復(fù)數(shù)類型,僅支持整數(shù)和浮點(diǎn)數(shù)運(yùn)算。
常數(shù) | 說明 | 實(shí)例 |
---|---|---|
math.pi | 圓周率Π | math.pi 輸出結(jié)果:3.141592653589793 |
math.e | 自然常數(shù)e | math.e 輸出結(jié)果:2.718281828459045 |
math.inf | 正無窮大, -math.inf是負(fù)無窮大 | math.inf 輸出 inf |
math.nan | 非浮點(diǎn)數(shù)標(biāo)記,NaN | math.nan 輸出結(jié)果:nan |
2. math庫常用函數(shù)
函數(shù)名 | 說明 |
---|---|
math.ceil(f) | 向上取整,返回值:整數(shù)值 |
math.floor(f) | 向下取整,返回值:整數(shù) |
round(f) | 四舍五入,返回值:整數(shù) |
math.fabs(f) | 獲取絕對值操作,返回值:浮點(diǎn)數(shù) |
abs(num) | 獲取絕對值操作,返回值:根據(jù)傳入的參數(shù)而定 |
math.fmod(x,y) | 返回x/y的余數(shù),返回值:浮點(diǎn)數(shù) |
math.pow(x,n) | 返回x的n次方,返回值:浮點(diǎn)型 |
math.sqrt(num) | 對num開平方,返回值:浮點(diǎn)數(shù) |
fsum(seq) | 返回序列中所有元素的和,返回值:浮點(diǎn)數(shù) |
sum(seq) | 將一個序列的數(shù)值進(jìn)行相加求和,返回值:根據(jù)序列中數(shù)值的類型變化 |
math.modf(num) | 將一個浮點(diǎn)數(shù)拆成小數(shù)和整數(shù)部分組成的元組,返回值:元組 |
math.trunc(f) | 返回浮點(diǎn)數(shù)的整數(shù)部分,返回值:整數(shù) |
math.copysign(n1,n1) | 將第二個數(shù)的正負(fù)號賦值給第一個數(shù),返回值:浮點(diǎn)數(shù) |
math.factorial(x) | 返回x的階乘,如果x不是整數(shù)或?yàn)樨?fù)數(shù)將引發(fā)ValueError,返回值:整數(shù) |
math.gcd(x,y) | 返回整數(shù)x和y的最大公約數(shù),返回值:整數(shù) |
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庫常用函數(shù) 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的余數(shù),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()獲取浮點(diǎn)數(shù)的小數(shù)和整數(shù)部分,math.modf(2.3) = ', math.modf(2.3)) print('math.trunc()獲取浮點(diǎn)數(shù)的整數(shù)部分,math.trunc(2.3) = ', math.trunc(2.3)) print('math.copysign(n1,n2)把第二個數(shù)的正負(fù)號賦值給第一個浮點(diǎn)數(shù),math.copysign(-2.3,1) = ', math.copysign(-2.3,1)) print('math.copysign(n1,n2)把第二個數(shù)的正負(fù)號賦值給第一個浮點(diǎn)數(shù),math.copysign(2.3,-1) = ', math.copysign(2.3,-1)) print('math.gcd(x,y)獲取x和y的最大公約數(shù),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
數(shù)據(jù)類型用于浮點(diǎn)數(shù)計(jì)算。相比內(nèi)置的二進(jìn)制浮點(diǎn)數(shù)實(shí)現(xiàn)float,Decimal
有助于金融應(yīng)用和其它需要精確十進(jìn)制表達(dá)的場合,控制精度,控制舍入以適應(yīng)法律或者規(guī)定要求,確保十進(jìn)制數(shù)位精度,或者用戶希望計(jì)算結(jié)果與手算相符的場合。
Decimal重現(xiàn)了手工的數(shù)學(xué)運(yùn)算,確保了二進(jìn)制浮點(diǎn)數(shù)無法精確保有的數(shù)據(jù)精度。高精度使Decimal
可以執(zhí)行二進(jìn)制浮點(diǎn)數(shù)無法進(jìn)行的模運(yùn)算和等值測試。
1. 什么時候使用decimal
python中小數(shù)相加可能計(jì)算結(jié)果不對,是由于科學(xué)計(jì)算精度問題,如果需要處理這個問題就需要用到decimal模塊。
2. 使用decimal
設(shè)置精度:decimal.getcontext().prec = num,num為有效數(shù)字個數(shù)
設(shè)置小數(shù)位數(shù):quantize()
注意:decimal.getcontext().prec 和 quantize()不能同時使用,如果同時使用會提示錯誤:decimal.InvalidOperation: [<class ‘decimal.InvalidOperation’>]
3. decimal使用示例
# -*- coding: utf-8 -*- import decimal """ decimal.getcontext().prec = 3 # 設(shè)置有效數(shù)字是3位 print(decimal.Decimal(2.32) + decimal.Decimal(3.01)) decimal.getcontext().prec = 2 # 設(shè)置有效數(shù)字是2位 print(decimal.Decimal(2.32) + decimal.Decimal(3.01)) """ # quantize()設(shè)置小數(shù)位數(shù) num = decimal.Decimal(1.23456789).quantize(decimal.Decimal('0.000')) print(num)
到此這篇關(guān)于python數(shù)學(xué)模塊(math/decimal模塊)的文章就介紹到這了,更多相關(guān)python數(shù)學(xué)模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解Python中math和decimal模塊的解析與實(shí)踐
- Python內(nèi)置數(shù)學(xué)函數(shù)和math模塊使用指南
- python常用模塊(math itertools functools sys shutil)使用講解
- Python標(biāo)準(zhǔn)庫之Math,Random模塊使用詳解
- Python中非常實(shí)用的Math模塊函數(shù)教程詳解
- 表格梳理python內(nèi)置數(shù)學(xué)模塊math分析詳解
- python math模塊的基本使用教程
- 一看就懂得Python的math模塊
- Python中Random和Math模塊學(xué)習(xí)筆記
- Python math 模塊完全指南
相關(guān)文章
Python3編碼問題 Unicode utf-8 bytes互轉(zhuǎn)方法
今天小編就為大家分享一篇Python3編碼問題 Unicode utf-8 bytes互轉(zhuǎn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10Python報錯之如何解決matplotlib繪圖中文顯示成框框問題
這篇文章主要介紹了Python報錯之如何解決matplotlib繪圖中文顯示成框框問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09Python操作Excel工作簿的示例代碼(\*.xlsx)
這篇文章主要介紹了Python操作Excel工作簿的示例代碼(\*.xlsx),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03python自動獲取微信公眾號最新文章的實(shí)現(xiàn)代碼
這篇文章主要介紹了python自動獲取微信公眾號最新文章,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07pycharm實(shí)現(xiàn)print輸出保存到txt文件
這篇文章主要介紹了pycharm實(shí)現(xiàn)print輸出保存到txt文件,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python 中開發(fā)pattern的string模板(template) 實(shí)例詳解
這篇文章主要介紹了Python 中開發(fā)pattern的string模板(template) 實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04