【发布时间】:2016-05-23 03:17:04
【问题描述】:
我看到使用 C++ 11 的 std::uniform_real_distribution 与 Apple LLVM 版本 7.0.2 (clang-700.1.81) 编译时出现一些奇怪的行为。调用 operator() 会呈现分布范围之外的结果。下面的最小示例程序重现了这个问题
// Example program
#include <random>
#include <iostream>
#include <string>
template< int power >
constexpr uint64_t power_of_two(){
return 2 * power_of_two< power - 1 >();
}
template< >
constexpr uint64_t power_of_two< 0 >(){
return 1;
}
std::linear_congruential_engine
< uint64_t, 273673163155, 13, power_of_two< 48 >() >
rng;
std::uniform_real_distribution< double > angle_cosine(-1, 1);
int main()
{
std::cout << angle_cosine.a() << " " << angle_cosine.b() << '\n' << std::endl;
for (int i = 0; i < 4; ++i){
std::cout << angle_cosine(rng) << std::endl;
}
}
编译运行online(大概用g++)会呈现合理的结果
-1 1
-0.529254
-0.599452
0.513316
-0.604338
但是,在本地编译运行会呈现不合理的结果。
-1 1
130349
37439.4
42270.5
45335.4
我是否忽略了某些事情或遇到了 libc++ 中的错误?如果是后者,是否有人知道解决方法?
【问题讨论】:
-
对我来说似乎是一个错误。它与
a、c和m的值的选择有关。它适用于here 列出的大多数流行选择,但对于m=2^48的这两个设置则失败了