一、处理数学的函数
1.自带的函数
比如abs()求绝对值,round()四舍五入
2.math库
头文件
import math
math库的函数使用时必须加上"math.",如math.e,math.fabs()
math库的函数的返回值大多都是float类型的。
二、函数
1.常数
数学常数 | 值 |
---|---|
math.pi | 数学常数π = 3.141592…… |
math.e | 数学常数e = 2.718281…. |
math.tau | 数学常数τ = 6.283185…… |
import math
print(math.pi)
#3.141592653589793
print(math.e)
#2.718281828459045
print(math.tau)
#6.283185307179586
#必须加上math.
2.数学函数
(1)数的处理
数的处理 | 功能 |
---|---|
math.fabs(x) | 返回float类型的绝对值x |
abs(x) | 返回float或者int类型的绝对值x |
math.ceil(x) | 向上取整,返回大于等于x的最小的整数 |
math.floor(x) | 向下取整返回小于等于x的最小的整数 |
round(x[,n]) | 返回浮点数x的五舍六入值。n是保留小数点的位数,默认为0 |
import math
#绝对值
print(math.fabs(-123))
#123.0
print(math.fabs(-123.55))
#123.55
print(abs(-123))
#123
print(abs(-123.55))
#123.55
#取整
print(math.ceil(10.0))
#10
print(math.ceil(-10.0))
#-10
print(math.ceil(10.9))
#11
print(math.ceil(-10.9))
#-10
print(math.floor(10.0))
#10
print(math.floor(-10.0))
#-10
print(math.floor(10.9))
#10
print(math.floor(-10.9))
#-11
#五舍六入
print(round(10.4))
#10
print(round(10.5))
#10
print(round(10.6))
#11
print(round(-10.4))
#-10
print(round(-10.5))
#-10
print(round(-10.6))
#-11
print(round(10.55,1))
#10.6
(2)指数对数
指数对数 | 功能 |
---|---|
math.pow(x,y) | 返回x的y次方,即x**y |
math.sqrt(x) | 返回√x,即x**0.5 |
math.exp(x) | 返回e的x次方,即e**x |
math.log(x[, base]) | 返回以base为基数的x的对数。base:底数,默认为 e |
math.log10(x) | 返回以10为基数的x的对数 |
import math
print(math.pow(4,2))
#16.0
print(math.sqrt(9))
#3.0
print(math.exp(2))
#7.38905609893065
print(math.log(100,10))
#2.0
print(math.log10(100))
#2.0
(3)函数集成
函数集成 | 功能 |
---|---|
math.factorial(x) | 返回 x!。如果x不是积分或者是负的,就会产生ValueError。 |
math.modf(x) | 以元组的类型返回x的小数部分与整数部分 |
max(x1,x2…) | 返回给定参数的最大值,参数可以为序列 |
min(x1,x2…) | 返回给定参数的最小值,参数可以为序列 |
cmp(x,y) | 比较两个数。如果x<y返回-1,如果x==y返回0,如果x>y返回1。 Python3没有 |
import math
print(math.factorial(3))
#6
#print(math.factorial(3.5))
#ValueError
print(math.modf(3.14))
#(0.14000000000000012, 3.0)
print(max(1,2,3))
#3
print(max(1.5,2.5,3.5))
#3.5
print(max(1.3,3,6))
#6
#print(cmp(5,6))
#NameError
(4)角度
角度 | 功能 |
---|---|
math.degrees(x) | 将角x从弧度转换成角度 |
math.radians(x) | 把角x从度转换成弧度 |
math.sin(x) | 返回 弧度x 的正弦 |
math.cos(x) | 返回弧度 x 的余弦 |
math.tan(x) | 返回弧度 x 的正切 |
math.asin(x) | 返回弧度 x 的反正弦 |
math.acos(x) | 返回弧度 x 的反余弦 |
math.atan(x) | 返回弧度 x 的反正切 |
math.atan2(x,y) | 返回坐标(x,y)的反正切 math.atan2(a,b)=math.atan(a/b) |
import math
#弧度转化为角度:2Π→360°
print(math.degrees(2*math.pi))
#360.0
#角度转化为弧度:180°→Π
print(math.radians(180))
#3.141592653589793
#sin(30°)=0.5
print(math.sin(math.radians(30)))
#0.49999999999999994
print(math.sin(math.pi/6))
#0.49999999999999994
#反函数:asin(0.5)=30°,atan(√3)=60°
print(math.degrees(math.asin(0.5)))
#30.000000000000004
print(math.degrees(math.atan(math.sqrt(3))))
#59.99999999999999
print(math.degrees(math.atan(1.5)))
#56.309932474020215
print(math.degrees(math.atan2(3,2)))
#56.309932474020215
(5)随机数
https://blog.csdn.net/sandalphon4869/article/details/89680906
参考:
http://www.runoob.com/python/python-numbers.html
https://blog.csdn.net/qq_38092017/article/details/76216137