I have programmed a maths program in python but the only problem is with the trigonometry section that some answers need to be in degrees and not radians. How do I convert into degrees? I have tried x = input("value: ") y = math.degrees(x) #gives me the wrong answer and: x = input("input: ") y = x*180/math.pi #still wrong output
Please post a test case,with wrong output and desired output.
math.cos(x) =>length of the adjacent side of the right triangle inscribed in a circle with angle x radians
math.radians(x) =>returns radians 1 deg => 0.01745 rad 1 rad =>57.30659 deg
180 degrees is the same measure as pi radians; so the conversion you have set up is good; 180 x radians * ----- = degrees pi the logic of course being that you divide out the pi and stretch it to fit the degrees.
How are you determining the value attributed to the input of 'x' ? if that value is off, it would produce erroneous results :)
This: def cosine(): print ' ' b = input(90) x = math.cos(b) y = math.degrees(x) print ' ' return y Returns: -25.6727271154 and def cosine(): print ' ' b = input(90) x = math.cos(b) y = x*(180/math.pi) print ' ' return y returns the same value pure radians is def cosine(): print ' ' b = input(90) x = math.cos(b) print ' ' return x returns: -0.448073616129
Input is actually the angle that the user inputs but for all of these I have just used 90
if your input is in degrees; change it to radians before passing it thru the function and see how that helps :) In javascript, the input is read AS radians to begin with. b = 90*Math.PI/180 x = Math.cos(b) Might help :)
In that little tidbit; the raw input is translated as radians ...
this is the revised version that converts the input into radians; calcultaes the cosine, and then converts it back as degrees...
The answer that gives me is: 6.12323399574e-17
the cos of 90 is not 6.12323399574e-17 as far as I know it's 0
Join our real-time social learning platform and learn together with your friends!