我使用的是 Python 2.5
我有一个角度(以弧度为单位)。我想把它四舍五入到最近的四分之一圆。因此,例如:
WH_MOUSE
答案 0 :(得分:3)
代码:
import numpy as np
def nearest_quarter(ang):
return (((round(((ang)/np.pi)/0.5))/4)*(2*np.pi)) % (2*np.pi)
测试用例:
nearest_quarter(3)
Out[37]: 3.141592653589793
nearest_quarter(1.4)
Out[38]: 1.5707963267948966
nearest_quarter(6)
Out[39]: 0.0