網站首頁 編程語言 正文
前言:
大家好??!又是我TUSTer_!? python里有自帶的庫math,cmath,和函數pow來開根號。下邊讓我帶你了解他們吧!記得一鍵三連!
一,math庫
使用math庫里的sqr()函數,在math庫里邊有很多數學函數,如三角函數sin(),pi-π等等:
import math print(math.sqr(4)) 輸出結果:2Epsilon = 10e-16 def fab_h(x): ''' 求實數的絕對值 :param x: R ''' if x >= 0: return x else: return x * -1 def sqrt_h(x, n=2.0): ''' n倍根號下x 牛頓迭代法 ''' val = x last = 0.0 if n == 2.0: while (fab_h(val - last) > Epsilon): last = val val = (val + x / val) / 2 return val while (fab_h(val - last) > Epsilon): last = val val = ((n-1)*val + x / val**(n-1)) / n return val
二,cmath庫
cmath多用于復數,負數的開平方。
# importing cmath library import cmath # using cmath.sqrt() method gfg = cmath.sqrt(3) print(gfg)
輸出:
(1.7320508075688772+0j)
三,pow()函數
pow(x,y)的意思是返回x的y次方,如pow(x,2)就是返回x的平方,就是x^2,
#!/usr/bin/python # -*- coding: UTF-8 -*- import math # 導入 math 模塊 print "math.pow(100, 2) : ", math.pow(100, 2) # 使用內置,查看輸出結果區別 print "pow(100, 2) : ", pow(100, 2) print "math.pow(100, -2) : ", math.pow(100, -2) print "math.pow(2, 4) : ", math.pow(2, 4)
輸出:
math.pow(100, 2) : ?10000.0
pow(100, 2) : ?10000
math.pow(100, -2) : ?0.0001
math.pow(2, 4) : ?16.0
math.pow(3, 0) : ?1.0
python不同開根號速度對比
import time import math import numpy as np def timeit1(): s = time.time() for i in range(750000): z=i**.5 print ("Took %f seconds" % (time.time() - s)) def timeit2(arg=math.sqrt): s = time.time() for i in range(750000): z=arg(i) print ("Took %f seconds" % (time.time() - s)) def timeit3(arg=np.sqrt): s = time.time() for i in range(750000): z=arg(i) print ("Took %f seconds" % (time.time() - s)) def timeit4(): s = time.time() for i in range(750000): z=math.pow(i,.5) print ("Took %f seconds" % (time.time() - s)) timeit1() timeit2() timeit3() timeit4()
Took 0.152364 seconds
Took 0.061580 seconds
Took 1.016529 seconds
Took 0.215403 seconds
總結
原文鏈接:https://blog.csdn.net/TUSTer_/article/details/122548314
- 上一篇:C#算法之回文數_C#教程
- 下一篇:python多線程方法詳解_python
相關推薦
- 2022-03-19 .NET中的Husky工具及安裝方式_實用技巧
- 2022-05-05 Entity?Framework使用LINQ操作實體_實用技巧
- 2022-07-18 python中數組array和列表list的基本用法及區別解析_python
- 2022-07-30 Qt實現不規則窗體
- 2022-11-01 Golang解析yaml文件操作指南_Golang
- 2022-02-02 uni 修改數據頁面不重新渲染
- 2022-08-06 .Net?Core中使用EFCore生成反向工程_實用技巧
- 2022-06-06 webpack5.6.0解決報The ‘mode‘ option has not been set,
- 最近更新
-
- 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同步修改后的遠程分支