Python's trigonmetric function return unexpected values

北慕城南 提交于 2019-11-28 08:26:43

问题


import math
print "python calculator"
print "calc or eval"
while 0 == 0:
    check = raw_input() #(experimental evaluation or traditional calculator)
    if check == "eval":
        a = raw_input("operator\n") #operator
        if a == "+":
            b = input("arg1\n") #inarg1
            c = input("arg2\n") #inarg2
            z = b + c
            print z
        elif a == "-":
            b = input("arg1\n") #inarg1
            c = input("arg2") #inarg2
            z = b - c
            print z
        elif a == "/":
            b = input("arg1\n") #inarg1
            c = input("arg2\n") #inarg2
            z = b / c
            print z
        elif a == "*":
            b = input("arg1\n") #inarg1
            c = input("arg2]n") #inarg2
            z = b * c
            print z
        elif a == "^":
            b = input("arg1\n") #inarg1
            c = input("arg2\n") #inarg2
            z = b ** c
        elif a == "sin":
            b = input("arg1\n") #inarg1
            var = math.degrees(math.sin(b))
            print var
        elif a == "asin":
            b = input("arg1\n") #inarg1
            var = math.degrees(math.asin(b))
            print var
        elif a == "cos":
            b = input("arg1\n") #inarg1
            var = math.degrees(math.cos(b))
            print var
        elif a == "acos":
            b = input("arg1\n") #inarg1
            var = math.degrees(math.acos(b))
            print var
        elif a == "tan":
            b = input("arg1\n") #inarg1
            var = math.degrees(math.tan(b))
            print var
        elif a == "atan":
            b = input("arg1\n") #inarg1
            var = math.degrees(math.atan(b))
            print var
    elif check == "calc" :
        x = input() #takes input as expression
        print x #prints expression's result

Isn't the sine of 90 degrees 1? With this it shows up as something around 51.2? Google's calculator does this too? BTW: this is my python calculator

            b = input("arg1\n") #inarg1
            var = math.degrees(math.sin(b))
            print var

This one and other trig functions are the problem. For the most part, this was just a simple python calculator, but I wanted to add some trig functions.


回答1:


You don't want o convert the return value of sin() to degrees -- the return value isn't an angle. You instead want to convert the argument to radians, since math.sin() expects radians:

>>> math.sin(math.radians(90))
1.0



回答2:


Python's sin and cos take radians not degrees. You can convert using the math.radians function. Basically, you are using the wrong units.




回答3:


Most math functions, including Python's math functions, use radians as the measure for trigonometric routines.

Compare:

>>> math.sin(90)
0.8939966636005579
>>> math.sin(3.1415926535)
8.979318433952318e-11
>>> math.cos(180)
-0.5984600690578581
>>> math.cos(2*3.1415926535)
1.0
>>> 



回答4:


You are using degrees, but the sin function expects radians (see the documentation: help(math.sin)). 90° is 𝜋/2.

>>> import math
>>> math.sin(math.pi/2)
1.0
>>> math.radians(90) - math.pi/2
0.0



回答5:


Convert your input from degrees to radians before calling math.sin




回答6:


        var = math.degrees(math.sin(b))

This code does not do what you think it does. It takes the sin of b and then converts that answer (which is not in radians!) from radians to degrees.

The sin of 90 radians is .894. .894 radians is 51 degrees. So that's why you get that answer, but it's all wrong.

You probably want:

        var = math.sin(math.radians(b))


来源:https://stackoverflow.com/questions/8483080/pythons-trigonmetric-function-return-unexpected-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!