math.cos(x)没有返回正确的值? [英] math.cos(x) not returning correct value?
问题描述
我刚刚开始使用python,但是在使用一个非常基本的程序时遇到了困难.我想计算-20度的余弦.据我了解,默认值以弧度表示,因此这是我尝试的以下代码:
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 in radians, so this is the following code i tried:
import math
print math.cos(math.degrees(-20))
这将输出(-.7208 ...),答案实际上是(.9397 ...).我确定这是一个非常基本的解决方案,但是我尝试了很多不同的方法,但不会输出正确的结果.预先感谢!
This outputs (-.7208...), where the answer is actually (.9397...). I'm sure this has a pretty basic solution but I've tried so many different things and it will not output the correct results. Thanks in advance!
推荐答案
根据 将角度 x 从弧度转换为度. Convert angle x from radians to degrees. 这意味着您试图将-20弧度转换为不需要的度数. That means you are attempting to convert -20 radians to degrees which isn't desired. 也根据文档: 返回 这意味着 This means 您需要将-20度转换为弧度,然后找到余弦.使用 You need to convert -20 degrees to radians, and then find the cosine. Use 这篇关于math.cos(x)没有返回正确的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!数学.度(x)
math.degrees(x)
math.cos(x)
math.cos(x)
x
弧度的余弦. math.cos
以弧度而不是度来查找传递的参数的余弦.这意味着您的代码当前将度数更改为-20弧度,然后将其余弦视为弧度...您可以看到为什么会出现问题.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. math.radians
:math.radians
:math.cos(math.radians(-20))