Reputation: 63
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.
Upvotes: 4
Views: 6159
Reputation: 104050
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
>>>
Upvotes: 3
Reputation: 601609
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
Upvotes: 10
Reputation: 182761
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))
Upvotes: 2
Reputation: 10543
Convert your input from degrees to radians before calling math.sin
Upvotes: 2
Reputation: 30332
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
Upvotes: 2
Reputation: 45039
Python's sin and cos take radians not degrees. You can convert using the math.radians function. Basically, you are using the wrong units.
Upvotes: 3