0
点赞
收藏
分享

微信扫一扫

Python:实现计算 sin 函数算法(附完整源码)


Python:实现计算 sin 函数算法

from math import factorial, radians
def sin(
angle_in_degrees: float, accuracy: int = 18, rounded_values_count: int = 10
) -> float:

# Simplify the angle to be between 360 and -360 degrees.
angle_in_degrees = angle_in_degrees - ((angle_in_degrees // 360.0) * 360.0)

# Converting from degrees to radians
angle_in_radians = radians(angle_in_degrees)

result = angle_in_radians
a = 3
b = -1

for _ in range(accuracy):
result += (b * (angle_in_radians**a)) / factorial(a)

b = -b # One positive term and the next will be negative and so on...
a += 2 # Increased by 2 for every term.

return round(result, rounded_values_count)


if __name__ == "__main__":
__import__("doctest").testmod()


举报

相关推荐

0 条评论