Python trigonometric function returns 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 result 

Isn't that a sine of 90 degrees 1? With that, does it appear like something around 51.2? Does Google Calculator do 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 trigger functions are a problem. For the most part, it was a simple python calculator, but I wanted to add some trigger functions.

+4
source share
6 answers

You do not want o to convert the return value of sin() to degrees - the return value is not an angle. Instead, you want to convert the argument to radians, since math.sin() expects radians:

 >>> math.sin(math.radians(90)) 1.0 
+10
source

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

+3
source

Most mathematical functions, including Python mathematical functions, use radians as a measure for trigonometric procedures .

For comparison:

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

You use degrees, but the sin function expects radians (see the documentation: help(math.sin) ). 90 Β° - Ο€ / 2.

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

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

+2
source
  var = math.degrees(math.sin(b)) 

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

sin of 90 radians .894 ..894 radians is 51 degrees. So why you get this answer, but all this is wrong.

You probably want:

  var = math.sin(math.radians(b)) 
+2
source

Source: https://habr.com/ru/post/1386010/


All Articles