Python Forum

Full Version: Problem with the math.sin(x) function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I was trying to make a simple sine calculator using degrees. However when I tested some values they come close but doesn't match my phone's calculator, I'm not sure what went wrong.

Here's my code:
from math import sin
from math import degrees
from math import radians

number_1 = input('Enter an angle in degrees >')
int_1 = int(number_1)
degree = degrees(int_1)
answer = sin(degree)
print('The sin of', int_1, 'degrees is', answer)
(Feb-21-2019, 04:36 PM)Carson147 Wrote: [ -> ]However when I tested some values they come close but doesn't match my phone's calculator, I'm not sure what went wrong.
please provide sample input, output and presumably correct output from your phone
Actually I see the problem
on line 7 you convert the user input to degrees (but it is already in degrees). You need to convert the user input to radians
from math import sin
from math import degrees
from math import radians
 
user_input = input('Enter an angle in degrees >')
degs = int(user_input)
rads = radians(degs)
answer = sin(rads)
print('The sin of {} degrees is {}'.format(degs, answer))
(Feb-21-2019, 04:42 PM)buran Wrote: [ -> ]
(Feb-21-2019, 04:36 PM)Carson147 Wrote: [ -> ]However when I tested some values they come close but doesn't match my phone's calculator, I'm not sure what went wrong.
please provide sample input, output and presumably correct output from your phone

For example, in my phone's calculator I inputted sin(40) and the result was 0.6427876096 (I made sure it was in degree mode too). On the other hand if I input 40 in the prompt of the program, it will output an answer of of -0.9992262926310146.
Did you see my edited post?

>>> from math import sin, radians
>>> sin(radians(40))
0.6427876096865393
(Feb-21-2019, 05:03 PM)buran Wrote: [ -> ]Did you see my edited post?

Yes, it now works as expected, I didn't read the function note in the python library correctly. Nevertheless thanks very much for the help, and sorry I didn't know there was a tag feature when posting code.