python三角函數改寫

python提供的三角函數是對弧度進行運算,並且存在不確定尾數的情況,爲代碼簡潔及計算準確,利用原三角函數進行改寫,併爲計算精度,保留足夠有效位數。

import math
rad = 180 / math.pi

def sin(degree):
	degree = degree / rad
	sin = math.sin(degree)
	return round(sin,15)

def cos(degree):
	degree = degree / rad
	cos = math.cos(degree)
	return round(cos,15)

def tan(degree):
	degree = degree / rad
	tan = math.tan(degree)
	return round(tan,15)

def asin(x):
	asin = math.asin(x) * rad
	return  round(asin,14)

def acos(x):
	acos = math.acos(x) * rad
	return  round(acos,13)

def atan(x):
	atan = math.atan(x) * rad
	return  round(atan,16)

測試示例

sin(30) = 0.5

cos(60) = 0.5

tan(45) = 1.0

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章