- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我知道用有限的位数不可能以任意精度表示所有数字,并且不建议对 float 进行简单的比较。但我希望,如果我将许多数字加在一起,我添加它们的 ** 顺序 ** 并不重要。
为了测试这个预测,我创建了一个随机数 vector 并计算它们的总和,然后对 vector 进行排序并再次计算总和。很多时候,这两个总和不匹配!这是我的代码(包括在下面)的问题、一般浮点运算的缺点,还是可以通过切换编译器等解决的问题?
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <random>
#include <vector>
double check_sum_depends_on_order(int seed)
{
// fill a vector with random numbers
std::vector<long double> v;
std::uniform_real_distribution<long double> unif(-1.,1.);
std::mt19937 rng(seed);
for (size_t i = 0; i < 1000; ++i)
{
v.push_back(unif(rng));
}
// copy this vector and then shuffle it
std::vector<long double> v2 = v;
std::sort(v2.begin(), v2.end());
// tot is running total for vector v, unsorted
// tot2 is running total for vector v2, sorted
long double tot = 0.0, tot2 = 0.0;
for (size_t i = 0; i < v.size(); ++i)
{
tot += v[i];
tot2 += v2[i];
}
// display result
// you can comment this if you do not want verbose output
printf("v tot\t= %.64Lf\n", tot);
printf("v2 tot\t= %.64Lf\n", tot2);
printf("Do the sums match (0/1)? %d\n\n", tot==tot2);
// return 1.0 if the sums match, and 0.0 if they do not match
return double(tot==tot2);
}
int main()
{
// number of trials
size_t N = 1000;
// running total of number of matches
double match = 0.;
for (size_t i = 0; i < N; ++i)
{
// seed for random number generation
int seed = time(NULL)*i;
match += check_sum_depends_on_order(seed);
}
printf("%f percent of random samples have matching sums after sorting.", match/double(N)*100.);
return 0;
}
最佳答案
假设您有一个精度为三位的十进制浮点类型。不太现实,但它提供了一个更简单的示例。
假设您有三个变量,a
、b
和c
。假设a
是1000
,b
和c
都是14
。
a + b
将是 1014,四舍五入为 1010。(a + b) + c
将是 1024,四舍五入为 1020。
b + c
将是 28。a + (b + c)
将是 1028,四舍五入为 1030。
关于c++ - 浮点运算 : why would order of addition matter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48957828/
作为一个小项目(没有什么关键任务),我决定尝试用 C# 编写 GOST 28147-89 的实现。但是,在阅读 RFC 5830 时(定义 GOST 28147-89 的信息),我注意到了这一点。 (
我在 Android JNI 上使用 Neon 实现了一个算法。当我想将向量相加时,我注意到有两种类型的方法,但我看不出其中的区别。 // pairwise addition int8x8_t vpa
我想知道为什么 STL 没有重载它们的算法函数,这样我就可以通过简单地提供一个容器而不是采用更冗长的方式来传递开始 + 结束迭代器来调用它们。我当然理解为什么我们还想使用迭代器对来处理容器/数组的子序
我想知道为什么 STL 不会重载它们的算法函数,这样我就可以通过简单地提供一个容器而不是采用更冗长的方式来传递 begin + end 迭代器来调用它们。我当然理解为什么我们还想使用迭代器对来处理容器
假设我有两个模块 - Module1、Module2。 每个模块在 Visual Studio 中都有自己的项目。 如何使用“附加模块依赖项”或“附加 BMI 目录”设置将模块 2 导入模块 1? 问
我有一个问题 df = data.frame(col1 = c("A", "B", "C", "A", "A", "B"), col2 = c(0.2, 0.2, 0.6, 1, 0.8, 0.2),
在sencha touch中,我有一个如下声明的轮播: ajaxNav = new Ext.Panel({ scroll: 'vertical', cls: 'card1 dem
我有一个开始日期,如下所示: var beginDate = "29/04/2015"; var beginHour = "13:32"; 我有一些持续时间变量: var hourDuration =
我一直在以一种相当粗糙的函数式方式进行一些动态系统模拟,目前正试图弄清楚 cpp 对象可以为我的代码带来什么。更具体地说,我在考虑以下结构: 我想通过一个抽象类来指定动力系统,比如“DynSys”,用
我想知道是否有办法强制将图像加载为 8U。我在 OpenCV 文档中看到您可以指定是将图像加载为单 channel 还是三 channel ,但没有提及颜色深度。有什么建议吗? 感谢您的帮助!!! :
题目地址:https://leetcode.com/problems/additive-number/description/ 题目描述: Additive number is a string
我已经使用 react-leaflet 有一段时间了,几周后我删除了 node_modules 中的文件并重新安装了它们,我根本没有接触 react-leaflet 版本,但是当我尝试运行时该项目,它
令我惊讶的是,我发现在 TCanvas 上重复渲染文本在某种程度上是“附加的”。我意识到设置 Canvas.Brush.Style:=bsClear 是问题的原因,但我确实需要透明且重复地渲染文本(即
我有一个 pdf 文件,其中包含“UniCNS-UCS2-H”字体,我尝试了 pdfbox 和 pdfrenderer,它们都抛出异常:“UniCNS-UCS2-H”的未知编码 这个字体包含在一个字体
我有一个大多数用户只会使用一次的 Facebook 应用程序。在进入 Facebook 工作流程之前,用户表明他们是否希望自己的墙被写入。基于此,我要么请求 publish_stream 许可,要么不
我正在尝试使用 CodeIgnitor 在 php、mysql 中制作标记表。我已经使用XAMPP并创建了一个数据库。用于存储每条记录。正在从数据库中检索数据,插入的新记录已成功编辑 但问题在于检索总
是否可以为 AppStore 应用审核提供额外的文件? 我们正在开发一个与 SAP 服务器通信的应用程序。为了审查该应用程序,我们设置了一个可供 Apple 访问的测试服务器。但是该服务器上的证书是自
enter image description here 我正在尝试设置我的函数并执行一些重载操作,以便我可以 +、-、==、* 两个矩阵。我在第一次操作重载时遇到了一个问题:加法。 我的程序一直有效
我正在尝试制作一个可以执行各种算术函数的基本计算器,从加法开始!现在我已经弄清楚了它的基本逻辑,但我不确定如何获取两个输入并将其打印出来! #include int main() { cha
这个问题在这里已经有了答案: Showing a Windows form on a secondary monitor? (9 个回答) 关闭 9 年前。 我有一个应用程序,其中有一个我想在第二个
我是一名优秀的程序员,十分优秀!