带你了解Python妙开根号的三种方式
作者:TUSTer_ 时间:2021-10-18 08:27:56
前言:
大家好啊!又是我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
标签:Python,根号
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python使用pyecharts库画地图数据可视化的实现
2023-04-18 09:44:25
![](https://img.aspxhome.com/file/2023/0/70890_0s.jpg)
GraphQL在Django中的使用教程
2023-02-20 18:11:31
![](https://img.aspxhome.com/file/2023/3/79513_0s.png)
学习Python3 Dlib19.7进行人脸面部识别
2022-03-18 00:01:30
![](https://img.aspxhome.com/file/2023/5/76035_0s.png)
sql中 RecordCount=-1问题
2008-03-02 17:18:00
CSS Border使用小分享
2010-08-12 14:34:00
![](https://img.aspxhome.com/file/UploadPic/20108/12/box-model-47s.png)
我的论坛源代码(九)
2023-11-15 05:50:05
Javascript中的isNaN函数使用说明
2023-08-27 10:10:02
语义化的HTML与搜索引擎优化(如何编写纯语义的HTML进行搜索引擎优化)
2009-12-28 12:42:00
Python爬虫工程师面试问题总结
2023-06-10 13:23:20
只有mdf文件的数据库附加失败的修复方法分享(置疑、只读)
2012-02-12 15:55:17
教你用FrontPage2003轻松布局网页
2008-02-24 17:05:00
![](https://img.aspxhome.com/file/UploadPic/20082/24/frontpage2003_79s.gif)
仿淘宝网站的导航标签效果!
2008-11-05 12:37:00
![](https://img.aspxhome.com/file/UploadPic/up/2008110512540282.gif)
python-try-except:pass的用法及说明
2022-07-21 13:22:24
python使用scapy模块实现ARP扫描的过程
2023-07-16 11:55:26
![](https://img.aspxhome.com/file/2023/0/85070_0s.png)
基于JS实现动态跟随特效的示例代码
2023-06-30 11:15:16
![](https://img.aspxhome.com/file/2023/5/55865_0s.png)
Python使用matplotlib绘图无法显示中文问题的解决方法
2023-07-30 22:16:30
![](https://img.aspxhome.com/file/2023/2/61462_0s.png)
python判断字符串是否纯数字的方法
2022-09-09 03:25:38
ASP中3种分页显示的性能比较
2007-08-15 13:37:00
python迭代器与生成器详解
2021-03-24 00:34:24
django缓存配置的几种方法详解
2022-08-29 02:37:16