Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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\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.

like image 355
user1080694 Avatar asked Nov 30 '22 03:11

user1080694


2 Answers

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
like image 181
Sven Marnach Avatar answered Dec 05 '22 03:12

Sven Marnach


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

like image 32
Winston Ewert Avatar answered Dec 05 '22 03:12

Winston Ewert