C++使用std::accumulate计算不正确的标准差
C++ Incorrect standard deviation calculation using std::accumulate
我使用以下代码来计算标准偏差:
std::vector<float> k = {4,6,2};
float mean = 4;
float sum = std::accumulate(k.begin(), k.end(), 0, [&mean](float x, float y) {
return (y - mean) * (y - mean);
});
float variance = sum / k.size();
float stdev = sqrt(variance);
std::accumulate
应返回4
时返回:
(4-4)^2 + (6-4)^2 + (2-4)^2 = 8
此外,打印(y - mean) * (y - mean)
给出:
0
4
4
那么,为什么它不返回0 + 4 + 4
呢?
您不使用x参数。尝试以下操作:
float sum = std::accumulate(k.begin(), k.end(), 0.0F, [&mean](float x, float y) {
return x + (y - mean) * (y - mean);
});
UPDATE:初始化值为浮动
相关文章:
- 使用2个键的cpp-stl::优先级队列排序不正确
- 正弦级数方程计算不正确
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 密码登录程序将永远循环并显示不正确的结果
- 在C++中返回不正确的楼层函数值
- 形状对象的旋转和缩放不正确C++
- C++ 读取文件读取文件不正确
- 关于类的 Python 文档 - 对C++的引用不正确
- 特征 LLT 模块给出不正确的结果?
- glibcxx STL 在实现 std::valarray::sum() 时是否不正确?
- 如果语句逻辑不正确
- 来自逆 vp 矩阵和相机位置的光线方向不正确
- 不正确的操作数类型 MSVC
- 数组填充了不正确的值
- 我是否不正确地集中了这些字符数组?
- 为什么除非我使用 cout,否则我的值不正确?
- 对于代码中指定的边界之外的值,对角差算法的输入不正确
- 标准::地图使用.这是怎么回事?核心转储?我做得不正确?
- 在 C++11 标准中哪里说 char* p = "abc" ;格式不正确?
- C++使用std::accumulate计算不正确的标准差