cos返回错误的值? [英] cos returns wrong values?
本文介绍了cos返回错误的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个奇怪的问题与cmath / math.h的标准cos函数。显然在某些情况下,它返回一个错误或简单的未定义的值。
I have a strange problem with the standard cos function of cmath/math.h. Apparently under some circumstances it returns a wrong or simply undefined value.
#include <cmath>
#include <iostream>
int main()
{
double foo = 8.0 * 0.19634955; // 1.5707964
double bla = std::cos(foo); // should be 0.9996242168245
std::cout << bla << std::endl; // cos returns -7.32051e-008
return 0;
}
如果cos的输入值为1.5707964,则cos返回-7.32051e
If the input value for cos is 1.5707964 for example, cos returns -7.32051e-008 (when using doubles, with floats it's -4.XYZe-009).
我在这里缺少一些真正的基本和简单的东西吗?
Am I missing something really basic and simple here...?
推荐答案
cos期望弧度,你给它度。将你的输入值乘以3.14159 / 180,你会得到正确的答案。
cos expects radians, you are giving it degrees. Multiply your input value by 3.14159/180, and you will get the right answer.
这篇关于cos返回错误的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文