Boost.Units如何提出这种不精确的转换结果?

时间:2015-06-10 11:31:06

标签: c++ boost boost-units

请考虑以下代码:

#include <boost/units/io.hpp>
#include <boost/units/systems/si/plane_angle.hpp>
#include <boost/units/systems/angle/degrees.hpp>
#include <iostream>
#include <cmath>
#include <limits>

int main()
{
    using namespace boost::units;
    std::cout.precision(std::numeric_limits<double>::digits10);
    std::cout << "Everyone knows that 180 deg = " << std::acos(-1.) << " rad\n";
    std::cout << "Boost thinks that   180 deg = "
              << quantity<si::plane_angle,double>(180.*degree::degree) << "\n";
}

我得到以下输出:

Everyone knows that 180 deg = 3.14159265358979 rad
Boost thinks that   180 deg = 3.14159265359 rad

显然,Boost.Units会在某处手动定义非常低精度的M_PI,因为它只是在第12个十进制之后被截断。但是当我grep ped {/usr/include/时,我只在/usr/include/python2.7/Imaging.h中找到了这个不精确的定义,这看起来非常不相关。所有其他人都指定了更多的十进制数字。

所以,我的问题是:Boost如何得出这个结果?

1 个答案:

答案 0 :(得分:4)

来自boost/units/base_units/angle/degree.hpp

BOOST_UNITS_DEFINE_BASE_UNIT_WITH_CONVERSIONS(angle,degree,"degree","deg",6.28318530718/360.,boost::units::angle::radian_base_unit,-101);

将6.28318530718除以2会返回您看到的值。这当然看起来不太对,但唉,这似乎就是这种情况。

更新:我发现了此问题的错误报告:https://svn.boost.org/trac/boost/ticket/6893