简体   繁体   English

Python中的三角函数

[英]Trigonometry in Python

I made this calculator to try and calculate trigonometry in python. 我做了这个计算器来尝试在python中计算三角函数。 I've only just started learning how to use this program and I haven't been able to find anything so far that makes sense to me. 我才刚刚开始学习如何使用该程序,到目前为止,我还没有找到对我有意义的任何东西。 The problem with this is that I keep getting different answers to those that come up on my scientific calculator. 问题在于,对于科学计算器上的答案,我一直得到不同的答案。

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'))

All trigonometric function from the math module require their argument to be in radians, not in degrees. math模块中的所有三角函数均要求其自变量为弧度而不是度。 You can use math.radians to do the conversion. 您可以使用math.radians进行转换。

import math

degrees = 90
radians = math.radians(degrees)

print(math.sin(radians)) # 1.0

Python's trig functions assume that the input is in radians, and you're entering degrees. Python的trig函数假定输入为弧度,并且您正在输入度数。

First convert your degrees to radians by multiplying degrees by math.pi/180.0 and see if those match better. 首先,将度数乘以math.pi/180.0degrees转换为弧度,然后查看它们是否匹配得更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM