模块1:math库的使用

来自CloudWiki
58.56.20.141讨论2018年3月14日 (三) 10:17的版本 函数库的引用
跳转至: 导航搜索

函数库的引用

  • 两种方式
  • 第1种:
>>>import math
>>>math.ceil(10.2)
  • 第2种:
>>>from math import *
>>>print(floor(x))
  • 对于这一种方式,库中的所有函数可直接使用

math库解析

数学常数

math.pi
math.e
math.inf
math.nan
  • 示例代码:
>>> from math import *
>>> pi
3.141592653589793
>>> e
2.718281828459045
>>> inf
inf
>>> nan
nan

数值表示函数

math.fabs(x)
math.fmod(x,y):求余数
math.fsum([x,y,...])
math.ceil(x)
math.floor(x)
math.factorial(x)
math.gcd(a,b)
  • 示例代码:
>>> x = 2.5
>>> fabs(x)
2.5
>>> x, y = 7,3
>>> fmod(x, y)
1.0
>>> fsum([1.5,2.5,3.3])
7.3
>>> x = 2.5
>>> ceil(x)
3
>>> floor(x)
2
>>> y = 3
>>> factorial(y)
6 
>>> gcd(12,8)
4

幂对数函数

pow(x,y)
exp(x)
expml(x)
sqrt(x)
log(x[,base])
log1p(x)
log2(x)
log10(x)
  • 代码:
>>> x , y = 3,5
>>> pow(x, y)
243.0
>>> exp(x)
20.085536923187668
>>> sqrt(4)
2.0
>>> log(x,2)
1.5849625007211563
>>> log1p(x)
1.3862943611198906
>>> log10(x)
0.47712125471966244 

三角运算函数

math.degrees(x)
math.radians(x)
math.sin(x)
math.cos(x)
math.tan(x)
asin(x)
acos(x)
atan(x)
atan2(y,x)
sinh(x)
cosh(x)
tanh(x)
asinh(h)
acosh(x)
atanh(x)
  • 实例代码:
>>> radians(90)
1.5707963267948966
>>> degrees(pi/2)
90.0
>>> a , b = 3, 4
>>> hypot(a ,b)
5.0
>>> sin(pi/2)
1.0
>>> cos(pi/2)
6.123233995736766e-17
>>> tan(pi/3)
1.7320508075688767
>>> asin(1/2)
0.5235987755982989
>>> acos(1/2)
1.0471975511965979
>>> degrees(asin(1/2))
30.000000000000004
>>> degrees(acos(1/2))
60.00000000000001

=高等特殊函数

下一节 实例3:天天向上的力量