dimanche 22 mai 2016

OS X libc++ std::uniform_real_distribution bug

I'm seeing some strange behavior using the C++ 11's std::uniform_real_distribution compiling with Apple LLVM version 7.0.2 (clang-700.1.81). Calling to operator() renders results outside the distribution range. The minimal sample program below reproduces the trouble

// 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;
    }
}

Compiling and running online (presumably with g++) renders reasonable results

-1 1

-0.529254
-0.599452
0.513316
-0.604338

However, compiling and running locally renders unreasonable results.

-1 1

130349
37439.4
42270.5
45335.4

Have I overlooked something or have I encountered a bug in libc++? If the latter is the case, is anyone aware of a work around?




Aucun commentaire:

Enregistrer un commentaire