简体   繁体   English

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

[英]How does Boost.Units come up with this imprecise result of conversion?

Consider the following code:考虑以下代码:

#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";
}

I get the following output:我得到以下输出:

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

Apparently, Boost.Units gets very low precision M_PI defined somewhere manually, because it's just truncated after 12th decimal place.显然,Boost.Units 在某处手动定义了非常低的精度 M_PI,因为它只是在小数点后 12后被截断。 But as I grep ped my /usr/include/ , I only found this imprecise definition in /usr/include/python2.7/Imaging.h , which looks quite unrelated.但正如我grep PED我的/usr/include/ ,我才发现,在这个定义不确切/usr/include/python2.7/Imaging.h ,它看起来相当无关。 All others have more decimal figures specified.所有其他人都指定了更多的十进制数字。

So, my question is: how does Boost come up with this result?所以,我的问题是:Boost 是如何得出这个结果的?

From boost/units/base_units/angle/degree.hpp :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);

Dividing 6.28318530718 by 2 returns the value you are seeing. 6.28318530718 除以 2 返回您看到的值。 This certainly doesn't seem quite right, but alas, this seems to be the case.这当然看起来不太对劲,但唉,这似乎是事实。

Update : I found a bug report for this problem: https://svn.boost.org/trac/boost/ticket/6893更新:我发现了这个问题的错误报告: https : //svn.boost.org/trac/boost/ticket/6893

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM