Python的三角函数返回意外值 [英] Python's trigonmetric function return unexpected values

查看:115
本文介绍了Python的三角函数返回意外值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

90度的正弦不是1吗? 这样,它显示为51.2左右吗? Google的计算器也可以吗? 顺便说一句:这是我的python计算器

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

此一个和其他触发函数就是问题所在. 在大多数情况下,这只是一个简单的python计算器,但我想添加一些触发函数.

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.

推荐答案

您不希望将sin()的返回值转换为度数-返回值不是角度.相反,您想将参数转换为弧度,因为math.sin()需要弧度:

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

这篇关于Python的三角函数返回意外值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆