【发布时间】:2014-12-25 00:16:14
【问题描述】:
我的代码
double to_radians(double theta)
{
return (M_PI * theta) / 180.0;
}
int main()
{
std::vector<std::pair<double, double>> points;
for (double theta = 0.0; theta <= 360.0; theta += 30.0)
{
points.push_back(std::make_pair(std::cos(to_radians(theta)), std::sin(to_radians(theta))));
}
for (auto point : points)
std::cout << point.first << " " << point.second << "\n";
}
我期望的输出
1 0 0.866025 0.5 0.5 0.866025 0 1 -0.5 0.866025 -0.866025 0.5 -1 0 -0.866025 -0.5 -0.5 -0.866025 0 -1 0.5 -0.866025 0.866025 -0.5 1 0我得到的输出:
1 0 0.866025 0.5 0.5 0.866025 6.12303e-17 1 -0.5 0.866025 -0.866025 0.5 -1 1.22461e-16 -0.866025 -0.5 -0.5 -0.866025 -1.83691e-16 -1 0.5 -0.866025 0.866025 -0.5 1 -2.44921e-16如您所见,我得到的是这些奇怪的值而不是零。有人可以解释为什么会这样吗?
【问题讨论】:
标签: c++ floating-point