boost::random和boost:uniform_real适用于doubles,而不适用于float
boost::random and boost:uniform_real works with doubles not with floats?
如果已经讨论过了,请原谅。我有一个模板函数,它根据模板参数使用boost::uniform_int和boost::uniform_real,并且应该返回相同的类型:
template <typename N> N getRandom(int min, int max)
{
timeval t;
gettimeofday(&t,NULL);
boost::mt19937 seed((int)t.tv_sec);
boost::uniform_int<> dist(min, max);
boost::variate_generator<boost::mt19937&, boost::uniform_int<> > random(seed, dist);
return random();
}
//! partial specialization for real numbers
template <typename N> N getRandom(N min, N max)
{
timeval t;
gettimeofday(&t,NULL);
boost::mt19937 seed( (int)t.tv_sec );
boost::uniform_real<> dist(min,max);
boost::variate_generator<boost::mt19937&, boost::uniform_real<> > random(seed,dist);
return random();
}
现在我已经用int、float和doubles测试了这个函数。它对int很好,对double也很好,但对float不起作用。这就好像它要么将浮点转换为int,要么存在某种类型转换问题。我之所以这么说是因为当我这样做的时候:
float y = getRandom<float>(0.0,5.0);
我总是得到一个int。然而,正如我所说,它适用于双打。是我做错了什么还是遗漏了什么?非常感谢。
参数0.0,5.0
是双精度的,而不是浮点值。使其浮动:
float y = getRandom<float>(0.0f,5.0f);
您甚至可以避免编写具有类型特征和MPL:的样板代码
template <typename N>
N getRandom(N min, N max)
{
typedef typename boost::mpl::if_<
boost::is_floating_point<N>, // if we have a floating point type
boost::uniform_real<>, // use this, or
boost::uniform_int<> // else use this one
>::type distro_type;
timeval t;
gettimeofday(&t,NULL);
boost::mt19937 seed( (int)t.tv_sec );
distro_type dist(min,max);
boost::variate_generator<boost::mt19937&, distro_type > random(seed,dist);
return random();
};
不是真正解决问题本身,而是一个解决方案:
为什么不使用特征类来获得正确的分布类型呢?
template<class T>
struct distribution
{ // general case, assuming T is of integral type
typedef boost::uniform_int<> type;
};
template<>
struct distribution<float>
{ // float case
typedef boost::uniform_real<> type;
};
template<>
struct distribution<double>
{ // double case
typedef boost::uniform_real<> type;
};
有了这套,你就可以有一个通用功能:
template <typename N> N getRandom(N min, N max)
{
typedef typename distribution<N>::type distro_type;
timeval t;
gettimeofday(&t,NULL);
boost::mt19937 seed( (int)t.tv_sec );
distro_type dist(min,max);
boost::variate_generator<boost::mt19937&, distro_type > random(seed,dist);
return random();
};
相关文章:
- FLTK 2.0构建和演示,适用于VS2019的2011年左右的代码库
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- 适用于 WebView2 旧版本的示例应用程序
- 在 NVIDIA GEFORCE GTX 1050 上下载适用于 Windows 10 的 openCL 1.2
- __attribute__(优化(0))) 是否适用于"recursively"?
- 为什么 std::erase(std::erase_if) 不是适用于<algorithm>任何容器的模板?
- 使用一个参数的模板函数时出错(适用于 2)
- 使用 适用于 Android 和 iOS 的 tf-lite C++ API
- 为什么这适用于 G++ 而不是 CLANG?
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- 适用于 Linux 的 c++ 上的代理脚本
- 为什么我的 SFINAE 表达式不再适用于 GCC 8.2?
- 使输出流式处理运算符适用于 boost::variant<std::vector<int>、int、double 的正确方法是什么>
- 有没有适用于Windows.lib文件的GNU二进制文件描述符(BFD)
- 无法获得 boost::spirit parser&lexer 适用于 std::string 或 int 或 double 以外的令牌类型
- 将 C++11 正则表达式与 gcc 4.8.2 一起使用时会出现奇怪的结果(但适用于 Boost 正则表达式)
- boost::variant是否适用于std::string
- boost::random和boost:uniform_real适用于doubles,而不适用于float
- 带有私有默认构造函数的Boost::serialization: object适用于vector,但不适用于map