Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

math - Python's trigonmetric function return unexpected values

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
") #operator
        if a == "+":
            b = input("arg1
") #inarg1
            c = input("arg2
") #inarg2
            z = b + c
            print z
        elif a == "-":
            b = input("arg1
") #inarg1
            c = input("arg2") #inarg2
            z = b - c
            print z
        elif a == "/":
            b = input("arg1
") #inarg1
            c = input("arg2
") #inarg2
            z = b / c
            print z
        elif a == "*":
            b = input("arg1
") #inarg1
            c = input("arg2]n") #inarg2
            z = b * c
            print z
        elif a == "^":
            b = input("arg1
") #inarg1
            c = input("arg2
") #inarg2
            z = b ** c
        elif a == "sin":
            b = input("arg1
") #inarg1
            var = math.degrees(math.sin(b))
            print var
        elif a == "asin":
            b = input("arg1
") #inarg1
            var = math.degrees(math.asin(b))
            print var
        elif a == "cos":
            b = input("arg1
") #inarg1
            var = math.degrees(math.cos(b))
            print var
        elif a == "acos":
            b = input("arg1
") #inarg1
            var = math.degrees(math.acos(b))
            print var
        elif a == "tan":
            b = input("arg1
") #inarg1
            var = math.degrees(math.tan(b))
            print var
        elif a == "atan":
            b = input("arg1
") #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
") #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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...