Python 中的三角函数计算器

问题描述 投票:0回答:0

我正在制作一个三角函数计算器,用于计算角度的正弦、余弦和正切。输入是我们要计算的角度。 Python 的输出(答案)通常与我的计算器相同,但有时 Python 和计算器的答案不同(某些特定整数会发生这种情况)。 我的代码:

import math
sin = int(input("Enter the number: "))
sin = math.sin(math.radians(sin))
print(sin)

例如输入为20[sin(20)],输出为0.3420201433256687(Python和计算器答案相同)。 但是当它是 30 [sin(30)] 时,计算器的答案是 0,5 (0.5) 而 Python 的输出是 0.49999999999999994。 我试过其他一些数字,输出仍然正常。 cos和tan也一样。

在此之前,我只是使用“math.sin()”作为代码,所有的输出都与计算器不同。我将输入从整数更改为弧度,输出开始正确,但现在我遇到了上述新问题。我试着把输入转换成“float”,我以为是因为数字的类型,但还是一样。

python math calculator trigonometry python-3.11
© www.soinside.com 2019 - 2024. All rights reserved.