網站首頁 編程語言 正文
一, 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)
原文鏈接:https://blog.csdn.net/sinat_41752325/article/details/127065279
相關推薦
- 2022-10-07 numpy中數組拼接、數組合并方法總結(append(),?concatenate,?hstack,
- 2022-09-17 C++實現圖的遍歷算法(DFS,BFS)的示例代碼_C 語言
- 2022-04-26 python?moviepy?的用法入門篇_python
- 2022-07-20 nginx?添加http_stub_status_module模塊_nginx
- 2022-05-21 生產級K8S基礎環境部署配置流程_服務器其它
- 2022-04-28 C#使用BackgroundWorker控件_C#教程
- 2022-06-06 詳解如何自定義Dubbo Filter(含dubbo2.7.X及以上版本和2.6.X及以下版本兩種寫
- 2022-05-09 Python數據結構與算法之鏈表,無序鏈表詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支