#include <iostream>
#include <math.h>
#include "boost/math/constants/constants.hpp"
const double pi = boost::math::constants::pi<double>();
int main() {
double x = 2;
double y = 1;
double angle = 90; //in degrees
double rad { angle * pi / 180 }; //converting to rads
std::cout << "rad: " << rad << std::endl; //1.5708 - OK
double c = cos( rad );
std::cout << "cos(rad): " << c << std::endl; //6.12303e-017 - Huh? Should be ~ -3.2051033e-9
double s = sin( rad );
std::cout << "sin(rad): " << s << std::endl; //1 - OK
x = x * c - y * s;
y = x * s + y * c;
std::cout << "(" << x << "," << y << ")" << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以这很奇怪,我正在尝试进行轮换,但是这cos给了我错误的值。它应该接近于-3.2051033e-9但我最终得到6.12303e-017. 我已将我的角度转换为弧度并检查出来。sin( rad )也返回正确的值。是什么赋予了?
编辑:代码有错误但与问题无关。在y = x * s + y * c;x 上应该指的是在顶部声明的值,但由于前面的等式覆盖了该值y,所以最后的值是错误的。
感谢指数的澄清。我有点困惑。
cos(90)(度) 近似为零,如0.0。担心取回e-17vs的数字e-9没有意义,它们基本上都是零。
它的结果如何关闭实际上是取决于他们如何表示pi,90.0,180.0,和执行cos的功能math.h。