- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在研究重要性抽样,出于测试目的,我需要能够生成 uniform_real_distribution<float>
的所有可能值可能会在区间 [0,1] 内生成(是的,它也从右侧关闭)。我的想法是生成整数,然后我可以将其转换为 float 。从我所做的测试来看,[0,1] 中的统一单精度 float 和 [0,2^24] 中的整数之间似乎存在完美的双射(我对它不是 [0 ,2^24-1] 并且我仍在试图找出原因,我最好的猜测是 0 对 float 来说是特殊的,而 1 到 2^24 都会导致具有相同指数的 float )。我的问题是,以这种方式生成的 float 是否正是可以从 uniform_real_distribution<float>
生成的 float .您可以在下面找到我的整数 <-> float 测试:
void floatIntegerBitsBijectionTest()
{
uint32 two24 = 1 << 24;
bool bij24Bits = true;
float delta = float(1.0) / float(two24);
float prev = float(0) / float(two24);
for (uint32 i = 1; i <= two24; ++i)
{
float uintMap = float(i) / float(two24);
if (uintMap - prev != delta || uint32(uintMap*float(two24)) != i)
{
std::cout << "No bijection exists between uniform floats in [0,1] and integers in [0,2^24].\n";
bij24Bits = false;
break;
}
prev = uintMap;
}
if(bij24Bits) std::cout << "A bijection exists between uniform floats in [0,1] and integers in [0,2^24].\n";
std::cout << "\n";
uint32 two25 = 1 << 25;
bool bij25Bits = true;
delta = float(1.0) / float(two25);
prev = float(0) / float(two25);
for (uint32 i = 1; i <= two25; ++i)
{
float uintMap = float(i) / float(two25);
if (uintMap - prev != delta || uint32(uintMap*float(two25)) != i)
{
std::cout << "No bijection exists between uniform floats in [0,1] and integers in [0,2^25].\n";
if (i == ((1 << 24) + 1)) std::cout << "The first non-uniformly distributed float corresponds to the integer 2^24+1.\n";
bij25Bits = false;
break;
}
prev = uintMap;
}
if (bij25Bits) std::cout << "A bijection exists between uniform floats in [0,1] and integers in [0,2^25].\n";
std::cout << "\n";
bool bij25BitsS = true;
delta = 1.0f / float(two24);
prev = float(-two24) / float(two24);
for (int i = -two24+1; i <= two24; ++i)
{
float uintMap = float(i) / float(two24);
if (uintMap - prev != delta || int(uintMap*float(two24)) != i)
{
std::cout << i << " " << uintMap - prev << " " << delta << "\n";
std::cout << "No bijection exists between uniform floats in [-1,1] and integers in [-2^24,2^24].\n";
bij25BitsS = false;
break;
}
prev = uintMap;
}
if (bij25BitsS) std::cout << "A bijection exists between uniform floats in [-1,1] and integers in [-2^24,2^24].\n";
}
编辑:
有点相关:
http://xoroshiro.di.unimi.it/random_real.c
https://lemire.me/blog/2017/02/28/how-many-floating-point-numbers-are-in-the-interval-01/
编辑 2:
我终于弄明白了 uniform_real_distribution<float>
至少在与 mt19937
一起使用时engine 与它的默认模板参数一起使用时(我说的是 VS2017 附带的实现)。可悲的是,它只是在 [0,2^32-1] 中生成一个随机整数,将其转换为 float ,然后除以 2^32。不用说,这会产生非均匀分布的 float 。然而,我猜测这适用于大多数实际目的,除非人们正在接近生成数字之间增量的精度。
最佳答案
我假设 C++ 实现对 float
使用 IEEE-754 32 位基本二进制格式.在这种格式中,[1, 2] 中可表示的浮点值有规律地间隔,距离为 2−23。
定义x
与:
std::uniform_real_distribution<float> x(1, 2);
然后,假设 uniform_real_distribution
实现得很好并且使用了合适的引擎,x(engine) - 1
将为 [0, 223) 中的整数 n 生成等于 n/223 的值,具有统一分布。
我对 uniform_real_distribution
的规范有疑虑在 C++ 中。它是根据实算术定义的。它返回具有恒定概率密度的值的要求需要一组连续的数字,而浮点格式不提供。此外,我不确定实现将如何处理端点。
由于分布被强制离散,所以不妨使用 uniform_int_distribution
并将样本乘以 2−23(可作为 numeric_limits<float>::epsilon()
获得)。这样做的好处是可以根据需要阐明端点并轻松支持 [0, 1) 或 [0, 1] 的区间。
即使 C++ 标准不使用 IEEE-754,[1, 2] 中的可表示值也应该均匀分布,因为 C++ 标准中对浮点值的描述由 a 中的一些数字表示某个基数乘以基数的某个幂。对于零次幂,从 1 到 2 的值将根据格式中最低有效数字的值进行间隔。如上所述,该距离为 numeric_limits<float>::epsilon()
.
1 C++ 标准使用旧术语“尾数”,但首选术语是“尾数”。
关于c++ - uniform_real_distribution<float> 所有可能的值生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48968831/
降本手段一招鲜,增效方法吃遍天; 01 互联网行业里; 降本策略千奇百怪,手段却出奇一致;增效方法五花八门,手段更是花里胡哨; 对于企业来说;
有什么方法可以使用 angularjs 中的部分进行代码分组吗? 原因 --- 我的 Controller 包含太多代码。该 Controller 包含了多个方法和大量功能的代码,降低了代码的可读性。
不幸的是,我的数据库的数据模型必须改变,所以我正在寻找最轻松的方式来迁移我的数据。 此时情况如何: create table cargo{ id serial primary key, per
在 QTextEdit 对象中,假设我想知道字符在鼠标光标下的位置。 我会写... void MyQTextEditObject::mousePressEvent(QMouseEvent* mouse
是否可以在 C++ 中返回一个 return 语句或做一些具有类似功能的事情? 例如,如果代码中有几个函数将指针作为输入,并且每个函数都检查指针是否为 nullptr,这将很方便。如果它是一个 nul
我的 PC 上有一个控制台应用程序,它是 signalR 服务器。 我有一个 html 页面,它是互联网上的 signalR 客户端。但我尝试连接服务器,但我有一个错误的请求 400 错误。如果服务器
我想将应用程序作为后台进程运行。当点击应用程序图标时,它不会显示任何 View ,只会启动后台进程。 最佳答案 对于 iOS 这是不可能的,但是对于 android,react native 有 he
我知道有(昂贵的)框架可以让你在 VS C# 中编写 android 应用程序并将其编译为 android apk。 我也知道,可以在 VS 中编写 Java 应用程序(link)。 是否有可能,甚至
我在做: can :manage, :all if user.role == 'admin' can :approve, Anuncio do |anuncio| anuncio.try(:apr
我是一名优秀的程序员,十分优秀!