3

我对 cmath/math.h 的标准 cos 函数有一个奇怪的问题。显然在某些情况下它返回一个错误的或根本未定义的值。

#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-008(使用双精度时,浮点数为 -4.XYZe-009)。

我在这里错过了一些非常基本和简单的东西吗......?

4

3 回答 3

25

cos 期望弧度,你给它度数。将您的输入值乘以 3.14159/180,您将得到正确答案。

于 2009-12-06T13:52:55.753 回答
2

cos(PI/2) = 0,而不是 1。

于 2010-06-25T15:55:27.673 回答
1

我不知道你是通过弧度还是度数......但是你的 foo 值接近 PI/2。所以你得到 cos(foo) = 0 和 sin(foo) = 1 (你期望什么?)

于 2009-12-06T16:31:20.533 回答