Python Forum
Problem with the math.sin(x) function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Problem with the math.sin(x) function (/thread-16292.html)



Problem with the math.sin(x) function - Carson147 - Feb-21-2019

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)



RE: Problem with the math.sin(x) function - buran - Feb-21-2019

(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))



RE: Problem with the math.sin(x) function - Carson147 - Feb-21-2019

(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.


RE: Problem with the math.sin(x) function - buran - Feb-21-2019

Did you see my edited post?

>>> from math import sin, radians
>>> sin(radians(40))
0.6427876096865393



RE: Problem with the math.sin(x) function - Carson147 - Feb-21-2019

(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.