python math模块常用函数

  1. math.atan() 对于两点分别是为(x1,y1) 和(x2,y2)形成的直线,math.atan()可以求其斜率(反正切值)对应角度:
    angle = atan( (y2-y1)/(x2-x1) )
  2. math.atan2() 对于两点分别是为(x1,y1) 和(x2,y2)形成的直线,math.atan2()可以求其斜率(反正切值)对应角度:angle = atan2( y2-y1, x2-x1 ),与math.atan()只需一个参数不同,math.atan2()使用两个参数使得x2-x1=0时依然不会出错,而使用math.atan需要提前判断x2-x1的值,故建议使用math.atan2()
print (atan2(-0.50,-0.50) ) #-2.35619449019
print (atan2(0.50,0.50) )   #0.785398163397 等于pi/4
print (atan2(5,5) )         #0.785398163397 等于pi/4
print (atan2(-10,10))       #-0.785398163397 等于-pi/4
print (atan2(10,20))        #0.463647609001
  1. math.degrees() math.atan()与math.atan2()都是求其参数的反正切值,最终得到弧度(如pi/4,pi/2,pi,2pi),而math.degrees() 将弧度转换为角度(如45,90,180,360)
print(math.degrees(3))    #171.887338539
print(math.degrees(-3))   #-171.887338539
print(math.degrees(0))    #0.0
print(math.degrees(pi))   #180.0
print(math.degrees(pi/2)) #90.0
print(math.degrees(pi/4)) #45.0
  1. math.radians() 与math.degrees()相反,math.radians()是将角度转化为弧度
print(math.radians(90))  #1.5707963267948966
print(math.radians(180)) #3.141592653589793
print(math.radians(360)) #6.283185307179586
  1. math.cos() math.cos() 返回x的弧度的余弦值
print(math.cos(math.pi))   #-1.0
print(math.cos(2*math.pi)) #1.0
  1. math.sin() math.sin() 返回x的弧度的余弦值
print(math.sin(math.pi))   #-1.22460635382e-16
print(math.sin(math.pi/2)) #1.0
  1. math.fabs() abs() 方法返回数字的绝对值,如math.fabs(-20) 返回20.0
print(math.fabs(-math.pi))   #-3.141592653589793
print(math.fabs(math.pi))    #3.141592653589793

你可能感兴趣的:(python)