Python 中的三角函数
Trigonometry in Python
我制作了这个计算器来尝试计算 python 中的三角函数。我才刚刚开始学习如何使用这个程序,到目前为止我还没有找到任何对我有意义的东西。这样做的问题是,我的科学计算器上出现的答案总是不同。
while True:
print('type "sine1" to find the value of the Opposite')
print('type "sine2" to find the value of the Hypotenuse')
print('type "cosine1" to find the value of the Adjacent')
print('type "cosine2" to find the value of the Hypotenuse')
print('type "tangent1" to find the value of the Opposite')
print('type "tangent2" to find the value of the Adjacent')
user_input = input(": ")
from math import sin, cos, tan
if user_input == 'sine1':
degrees = float(input('Enter the degrees: '))
hypotenuse = float(input('Enter the value of the hypotenuse: '))
result = str(hypotenuse * sin(degrees))
print('The answer is ' + result)
print(input('Press enter to quit: '))
break
elif user_input == 'sine2':
degrees = float(input('Enter the degrees: '))
opposite = float(input('Enter the value of the opposite: '))
result = str(opposite / sin(degrees))
print('The answer is ' + result)
print(input('Press enter to quit: '))
break
elif user_input == 'cosine1':
degrees = float(input('Enter the degrees: '))
hypotenuse = float(input('Enter the value of the hypotenuse: '))
result = str(hypotenuse * cos(degrees))
print('The answer is ' + result)
print(input('Press enter to quit: '))
break
elif user_input == 'cosine2':
degrees = float(input('Enter the degrees: '))
adjacent = float(input('Enter the value of the adjacent: '))
result = str(adjacent / cos(degrees))
print('The answer is ' + result)
print(input('Press enter to quit: '))
break
elif user_input == 'tangent1 ':
degrees = float(input('Enter the degrees: '))
adjacent = float(input('Enter the value of the adjacent: '))
result = str(hypotenuse * tan(degrees))
print('The answer is ' + result)
print(input('Press enter to quit: '))
break
elif user_input == 'tangent2':
degrees = float(input('Enter the degrees: '))
opposite = float(input('Enter the value of the opposite: '))
result = str(adjacent / cos(degrees))
print('The answer is ' + result)
print(input('Press enter to quit: '))
break
else:
print('invalid input, please close the program and try again... maybe learn how to spell first :P')
print(input('press enter to quit'))
Python 的三角函数假定输入以弧度为单位,而您输入的是度数。
首先将 degrees
乘以 math.pi/180.0
将您的度数转换为弧度,然后查看它们是否匹配得更好。
math
模块中的所有三角函数都要求其参数以弧度为单位,而不是以度为单位。您可以使用 math.radians
进行转换。
import math
degrees = 90
radians = math.radians(degrees)
print(math.sin(radians)) # 1.0
我制作了这个计算器来尝试计算 python 中的三角函数。我才刚刚开始学习如何使用这个程序,到目前为止我还没有找到任何对我有意义的东西。这样做的问题是,我的科学计算器上出现的答案总是不同。
while True:
print('type "sine1" to find the value of the Opposite')
print('type "sine2" to find the value of the Hypotenuse')
print('type "cosine1" to find the value of the Adjacent')
print('type "cosine2" to find the value of the Hypotenuse')
print('type "tangent1" to find the value of the Opposite')
print('type "tangent2" to find the value of the Adjacent')
user_input = input(": ")
from math import sin, cos, tan
if user_input == 'sine1':
degrees = float(input('Enter the degrees: '))
hypotenuse = float(input('Enter the value of the hypotenuse: '))
result = str(hypotenuse * sin(degrees))
print('The answer is ' + result)
print(input('Press enter to quit: '))
break
elif user_input == 'sine2':
degrees = float(input('Enter the degrees: '))
opposite = float(input('Enter the value of the opposite: '))
result = str(opposite / sin(degrees))
print('The answer is ' + result)
print(input('Press enter to quit: '))
break
elif user_input == 'cosine1':
degrees = float(input('Enter the degrees: '))
hypotenuse = float(input('Enter the value of the hypotenuse: '))
result = str(hypotenuse * cos(degrees))
print('The answer is ' + result)
print(input('Press enter to quit: '))
break
elif user_input == 'cosine2':
degrees = float(input('Enter the degrees: '))
adjacent = float(input('Enter the value of the adjacent: '))
result = str(adjacent / cos(degrees))
print('The answer is ' + result)
print(input('Press enter to quit: '))
break
elif user_input == 'tangent1 ':
degrees = float(input('Enter the degrees: '))
adjacent = float(input('Enter the value of the adjacent: '))
result = str(hypotenuse * tan(degrees))
print('The answer is ' + result)
print(input('Press enter to quit: '))
break
elif user_input == 'tangent2':
degrees = float(input('Enter the degrees: '))
opposite = float(input('Enter the value of the opposite: '))
result = str(adjacent / cos(degrees))
print('The answer is ' + result)
print(input('Press enter to quit: '))
break
else:
print('invalid input, please close the program and try again... maybe learn how to spell first :P')
print(input('press enter to quit'))
Python 的三角函数假定输入以弧度为单位,而您输入的是度数。
首先将 degrees
乘以 math.pi/180.0
将您的度数转换为弧度,然后查看它们是否匹配得更好。
math
模块中的所有三角函数都要求其参数以弧度为单位,而不是以度为单位。您可以使用 math.radians
进行转换。
import math
degrees = 90
radians = math.radians(degrees)
print(math.sin(radians)) # 1.0