C++11 密度计算中的错误std::分段常数分布?

C++11 密度计算中的错误std::分段常数分布?,c++11,random,standard-library,C++11,Random,Standard Library,似乎std::piecewise_constant_distribution错误地计算了密度,至少在GCC及其标准库中是这样 根据: 密度应计算为: 手动检查此项会发现错误 这可以在这里看到: linux上的/usr/include/c++/4.8/bits/random.tcc中提供了与此相关的源代码,构造函数调用的初始化函数\u M_initialize的提取表明此处存在错误: const double __sum = std::accumulate(_M_den.begin(),

似乎std::piecewise_constant_distribution错误地计算了密度,至少在GCC及其标准库中是这样

根据: 密度应计算为:

手动检查此项会发现错误

这可以在这里看到:

linux上的/usr/include/c++/4.8/bits/random.tcc中提供了与此相关的源代码,构造函数调用的初始化函数\u M_initialize的提取表明此处存在错误:

const double __sum = std::accumulate(_M_den.begin(),
                       _M_den.end(), 0.0);

      __detail::__normalize(_M_den.begin(), _M_den.end(), _M_den.begin(),
                __sum);  <----- WRONG

      // THIS is not the cummulative distribution (since the above normalization does not give the probability of the intervalls!)
      _M_cp.reserve(_M_den.size());
      std::partial_sum(_M_den.begin(), _M_den.end(),
               std::back_inserter(_M_cp));


      // Make sure the last cumulative probability is one.
      _M_cp[_M_cp.size() - 1] = 1.0;



      // Dividing here by the interval length is WRONG!!!

      for (size_t __k = 0; __k < _M_den.size(); ++__k)
           _M_den[__k] /= _M_int[__k + 1] - _M_int[__k];

cpluplus.com上的措辞模棱两可。给出了更清楚的解释,这正是C++标准中写的:


以下是规范的适用部分,直接来自N4296:

可以清楚地看到,总和仅适用于权重

很容易看出您的测试代码有问题。将间隔数减少为两个,第一个为长度1,第二个为长度2:

std::array<PREC,3> intervals {0, 1, 3};

那你问什么?你需要确认吗?如果不需要,那就提交一份bug报告。我需要确认,以及提交报告的链接否,你读错了。公式中的求和不适用于整个表达式w_k*b_k+1-b_k,但仅适用于w_k。错误报告存档于:
std::array<PREC,2> weights   {1, 2};
Probability : 0.200000000000000011102230246252
Probability : 0.400000000000000022204460492503