iam*_*man 5 c++ boost boost-accumulators
环境:VS 2013,Boost 1.58
我写了一些东西,它为Boost的累加器提供了一个更友好的界面,可用于在窗口上投影和,并计算窗口上的实际滚动平均值.在推动将VS 2013作为我们的主编译器的过程中,该类的一个单元测试开始失败.剥离层,我把它缩小到这个最小的例子:
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>
#include <boost/accumulators/statistics/rolling_mean.hpp>
namespace ba = boost::accumulators;
namespace bt = ba::tag;
typedef ba::accumulator_set < uint32_t, ba::stats <bt::rolling_mean > > MeanAccumulator;
int main() {
MeanAccumulator acc(bt::rolling_window::window_size = 5u);
for (uint32_t i : { 3, 2, 1, 0 }) {
acc(i);
std::cout << i << " actualMean: " << std::fixed << boost::accumulators::rolling_mean(acc) << "\n";
}
}
Run Code Online (Sandbox Code Playgroud)
在循环的最后一次传递中,我没有得到预期的平均值(1.5),而是得到一个疯狂的高值(1431655766.333333).
此代码在VS 2008中使用Boost 1.49正确执行(显然已替换C++ 11向量初始化),但在VS 2012和VS 2013中使用Boost 1.58失败.我无法解释这种失败,因此无法修复它.
其他有趣的观点:
它似乎是一个Boost错误,但是想要在报告错误或尝试构建Boost 1.59之前验证我没有做一些愚蠢的事情.提前致谢!
编辑:感谢您的回复,因为这似乎是一个Boost错误.相关的Boost票在这里..这是与带累加器的无符号整数相关的问题.具体来说,如果在窗口已满后添加到累加器的值严格小于窗口中的所有值,则rolling_mean调用将返回无效结果.
有一种解决方法,即不使用带累加器的无符号整数.这解决了我的问题,谢谢你的帮助!
显然有一个错误潜伏在那里,可能在编译器中,但更有可能在库中,因为我已经能够在GCC上重现这个:
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>
#include <boost/accumulators/statistics/rolling_mean.hpp>
namespace ba = boost::accumulators;
namespace bt = ba::tag;
typedef ba::accumulator_set < uint32_t, ba::stats <bt::rolling_mean > > MeanAccumulator;
int main() {
MeanAccumulator acc(bt::rolling_window::window_size = 5u);
for (uint32_t i : { 252, 189, 248, 154, 620, 885, 939, 196 }) {
acc(i);
std::cout << i << " actualMean: " << std::fixed << boost::accumulators::rolling_mean(acc) << "\n";
}
}
Run Code Online (Sandbox Code Playgroud)
打印
g++-5.2 -std=c++1y -O2 -Wall -pedantic main.cpp && ./a.out
252 actualMean: 252.000000
189 actualMean: 220.500000
248 actualMean: 229.666667
154 actualMean: 210.750000
620 actualMean: 292.600000
885 actualMean: 419.200000
939 actualMean: 569.200000
196 actualMean: 858994018.000000
Run Code Online (Sandbox Code Playgroud)
现在问题似乎与样本类型的选择有关unsigned:将其更改为signed会删除症状:Live On Coliru
简而言之:我会在boost邮件列表或Trac上报告这个:https://svn.boost.org/trac/boost/
| 归档时间: |
|
| 查看次数: |
1248 次 |
| 最近记录: |