math.cos(x) not returning correct value?

后端 未结 3 772
一向
一向 2021-01-25 22:45

I just started using python, and am having difficulty with a very basic program. I want to calculate the cosine of -20 degrees. It is my understanding that the default value is

相关标签:
3条回答
  • 2021-01-25 23:04

    Per the Python documentation:

    math.degrees(x)

    Convert angle x from radians to degrees.

    That means you are attempting to convert -20 radians to degrees which isn't desired.

    Also per the documentation:

    math.cos(x)

    Return the cosine of x radians.

    This means math.cos finds the cosine of the passed argument in radians, not degrees. That means your code currently changes -20 radians to degrees, then finds the cosine of that as if it were radians... you can see why that's a problem.

    You need to convert -20 degrees to radians, and then find the cosine. Use math.radians:

    math.cos(math.radians(-20))
    
    0 讨论(0)
  • 2021-01-25 23:13

    You need to input in radians, so do

    math.cos(math.radians(-20))
    

    math.radians(-20) converts -20 degrees to radians.

    0 讨论(0)
  • 2021-01-25 23:23

    math.degrees takes a number of radians and produces a number of degrees. You need the opposite conversion - you have a number of degrees, and you need to produce a number of radians you can pass to math.cos. You need math.radians:

    math.cos(math.radians(-20))
    
    0 讨论(0)
提交回复
热议问题