- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
情况是这样的,我真的不知道把大数加到最后计算所有平均值时到底发生了什么。
如果有具体错误需要编辑,请指正。
我已经调试过了,我只是在数据中找到,我在以下循环中的正常数据,但似乎变量“somme”给了我一些随机数并给出了一些完全错误的东西。 “moyenne”也是如此
别的,所有数据都是,或者0或者正数。Somme 有时会给出一个负数!
#define Nb 230400
std::vector<std::array<double,480>> data(480);
double somme=0;
double moyenne=0;
for (int i=0;i<480;i++)
{
for (int j=0;j<480;j++)
somme=somme+data[i][j];
}
moyenne=somme/Nb;
最佳答案
首先,使用您提供的代码,您无法获得负面结果(至少使用 IEEE float PC 和通常的 Unix 机器);如果你溢出,你会得到特殊值 Inf
(但如果数据是在您指定的范围内)。结果可能是错误的,由于舍入误差,但它们的下限仍为 0。
您还没有具体说明您是如何确定结果的负数,也不是你如何确保输入数据在范围内,所以我只能推测;但以下是不同的可能性:
for ( int i = 0; i < 480; ++ i ) { for ( int j = 0; j < 480; ++ j ) { assert( data[i][j] >= 0.0 && data[i][j] < 200000.0 ); somme += data[i][j]; } }
For the rest, your algorithm isn't particularly accurate. Somequick tests (filling your data structure with random values inthe range [0...2e5)
) show less than 15 digits accuracy in thefinal result. (Of course, this may be acceptable. Mostphysical data that you acquire won't have more than 3 or4 digits accuracy anyway, and you may not be displaying morethan 6. In which case...)
The accuracy issue is actually curious, and shows just howtricky these things can be. I used three functions for mytests:
// Basically what you did...
double
av1( std::vector<std::array<double, cols>> const& data )
{
double somme = 0.0;
for ( int i = 0; i != data.size(); ++ i ) {
for ( int j = 0; j != cols; ++j ) {
somme += data[i][j];
}
}
return somme / (data.size() * cols);
}
// The natural way of writing it in C++11...
double
av2( std::vector<std::array<double, cols>> const& data )
{
return std::accumulate(
data.begin(),
data.end(),
0.0,
[]( double a, std::array<double, cols> const& b ) {
return a + std::accumulate( b.begin(), b.end(), 0.0 );
} ) / (data.size() * cols);
}
// Using the Kahan summation algorithm...
double
av3( std::vector<std::array<double, cols>> const& data )
{
double somme = 0.0;
double c = 0.0;
for ( int i = 0; i != data.size(); ++ i ) {
for ( int j = 0; j != cols; ++j ) {
double y = data[i][j] - c;
double t = somme + y;
c = (t - somme) - y;
somme = t;
}
}
return somme / (data.size() * cols);
}
(在所有测试中,cols == 480
和 data.size() == 480
。)
代码是使用 VC11 编译的,带有选项/O2。这有趣的是 av2
更系统地比您的代码准确,通常精确到第 17 位数字(2 或内部表示中的 3 位),其中av1
在第 15 位数字(8 或内部表示中的 9 位)。这样做的原因是您的代码系统地收集到 xmm1
中,跨越所有480*480
值,其中 av2
分别收集每一行;这导致添加量减少,但差异很大震级。 (随着 av1
接近数据末尾,somme
接近 2.3e10
,它比任何一个都大得多数据元素。)使用类似的东西:
double
moyenne( std::vector<std::array<double, cols>> const& data )
{
double outerSum = 0.0;
for ( int i = 0; i != data.size(); ++ i ) {
double innerSum = 0.0;
for ( int j = 0; j != cols; ++ j ) {
innerSum += data[i][j];
}
outerSum += innerSum;
}
return outerSum / (data.size() * cols);
}
应该给出等同于 av2
的结果。 (但是如果你需要准确性,你真的应该使用 Kahan 求和算法。)
(我很想补充一点,如果其中任何一个让您感到惊讶,您无论如何都不应该使用 float 。)
关于c++ - 求和巨大的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17543807/
我通过在共享首选项中使用 GSON 将其转换为 json 来存储我的复杂对象。但是在检索它时,无法获得预期的字符串。 代码 这里 holderListCustomizationMap 是对象的复杂映射
因此,我正在尝试对大于可用RAM的gz压缩文件执行某种面向行的操作,因此排除了将其首先读取为字符串的情况。问题是,如何在rust(缺少gunzip file.gz|./my-rust-program)
我试图更好地理解为什么具有潜在大精度的大数字处理不一致,特别是在 JavaScript 及其本地化工具(例如 ECMA-402/Intl)中。我假设这与 float 的使用有关,但我想了解限制在哪里和
我们有一个 5GB 的 csv 文件,这是我们业务的主列表。 有多个类别,每个类别包含数千条记录。我们的目标是将每个类别导出为其自己的 csv 文件。 我们如何运行查询并导出数据? 运行 OSX。有没
基于上一个问题 ( see here ),我试图通过 xmlEventParse 读取许多大型 xml 文件,同时保存节点变化数据。使用此示例 xml:https://www.nlm.nih.gov/
我正在开发一个系统,它加载一个巨大的 CSV 文件(超过 100 万行)并保存到数据库中。每行也有超过一千个字段。 CSV 文件被视为一个批处理,每一行都被视为其子对象。在添加对象的过程中,每个对象都
借助node-google模块 我编写了一个简单的 Node 模块来为我的网络应用程序启用“文本网络搜索”功能,并在我的一个 View 中显示结果。 由于在来自同一 IP 的少量查询后 Google
我有相当大的 4D 阵列 [20x20x40x15000],我使用 h5py 将其作为 HDF5 文件保存到磁盘.现在的问题是我想计算整个数组的平均值,即使用: numpy.average(HDF5_
我在遗留代码库中连接巨大的 CString 时遇到问题。 CStrings 可以包含 base64 编码的文件,因此可能很大。在某些时候,这些 CString 会像这样连接起来: result +=
我正在尝试让我的服务器提供来自另一台服务器的巨大文件。但是,为了保护我的凭据免受该远程服务器的攻击,我不能简单地将请求者重定向到文件 url;另一方面,虽然使用 StreamingHttpRespon
感谢对此的任何见解,我有 2 个问题: 1) 弄清楚为什么我的本地数据库 oplog 庞大且不断增长 2) 安全删除(或重置)我的 local.oplog 以释放 18 GB 的浪费空间 场景:我一直
我的预期任务:获取大量数据(1 GB 及更多大小)json 字符串,操作(进行一些格式化、解析 json、重组 json 数据)并写入新格式化的 json 字符串作为响应。处理这种情况的更好方法是什么
我做了一个小的 Angular 4 应用程序,但我不知道如何应用 tree shaking 和 aot 编译。我运行的命令如下: ng build --prod --aot 但我得到的结果仍然很大,供
我是一名优秀的程序员,十分优秀!