Python Forum
Problem with the math.sin(x) function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with the math.sin(x) function
#1
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)
Reply
#2
(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))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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.
Reply
#4
Did you see my edited post?

>>> from math import sin, radians
>>> sin(radians(40))
0.6427876096865393
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  math problem for experts miltmobley 5 933 Jul-09-2023, 07:14 AM
Last Post: Gribouillis
  math.log versus math.log10 stevendaprano 10 2,314 May-23-2022, 08:59 PM
Last Post: jefsummers
  Why getting ValueError : Math domain error in trig. function, math.asin() ? jahuja73 3 3,707 Feb-24-2021, 05:09 PM
Last Post: bowlofred
  matrices math problem lokoprof 1 2,184 Aug-27-2018, 07:48 PM
Last Post: perfringo
  Math problem python Dhaval 1 2,899 Jun-05-2017, 10:28 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020