赞
踩
math库是Python提供的内置数学类函数库
首先使用保留字import引用该库
第一种:import math
对math库中函数采用math.<b>()形式使用
>>>import math
>>>math.ceil(10.2)
11
第二种,from math import <函数名>
对math库中函数可以直接采用<函数名>()形式使用
>>>from math import floor
>>>floor(10.2)
10
常数 | 数学表示 | 描述 |
math.pi | π | 圆周率,值为3.141592653589793 |
math.e | e | 自然对数,值为2.718281828459045 |
math.inf | ∞ | 正无穷大,负无穷大为-math.inf |
math.nan | 非浮点数标记,NaN(Not a Number) |
math.fsum([x,y,...])函数在数学求和运算中十分有用,参考如下例子;
>>>0.1 +0.2 +0.3
0.6000000000000001
>>>import math
>>>math.fsum([0.1,0.2,0.3])
0.6
浮点数,如0.1、0.2和0.3,在Python解释器内部表示时存在一个小数点后若干位的精度尾数,当浮点数进行运算时,这个精度尾数可能会影响输出结果。因此, 在涉及浮点数运算及结果比较时,建议采用math库提供的函数,而不直接使用 Python提供的运算符。
函数 | 数学表示 | 描述 |
---|---|---|
math.pow(x,y) | 返回x的y次幂 | |
math.exp(x) | 返回e的x次幂,e是自然对数 |
函数 | 数学表示 | 描述 |
math.degree(x) | 角度x的弧度值转角度值 | |
math.radians(x) | 角度x的角度值转弧度值 | |
math.hypot(x,y) | 返回(x,y)坐标到原点(0,0)的距离 | |
math.sin(x) | sin x | 返回x的正弦函数值,x是弧度值 |
math.cos(x) | cos x | 返回x的余弦函数值,x是弧度值 |
math.tan(x) | tan x | 返回x的正切函数值,x是弧度值 |
math.asin(x) | arcsin x | 返回x的反正弦函数值,x是弧度值 |
math.acos(x) | arccos x | 返回x的反余弦函数值,x是弧度值 |
math.atan(x) | arctan x | 返回x的反正切函数值,x是弧度值 |
math.atan2(y,x) | arctan y/x | 返回y/x的反正切函数值,x是弧度值 |
math.sinh(x) | sinh x | 返回x的双曲正弦函数值 |
math.cosh(x) | cosh x | 返回x的双曲余弦函数值 |
math.tanh(x) | tanh x | 返回x的双曲正切函数值 |
math.asinh(x) | arcsinh x | 返回x的反双曲正弦函数值 |
math.acosh(x) | arccosh x | 返回x的反双曲余弦函数值 |
math.atanh(x) | arctanh x | 返回x的反双曲正切函数值 |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。