Having some problems porting some code between C++0x and TR1. The C++0x version
works as expected, but the TR1 uniform_real distribution seems to be returning
elements outside the given range.

And example:

#include <string>
#include <iostream>

#ifdef __GXX_EXPERIMENTAL_CXX0X__
# include <functional>
# include <random>
#else
# include <tr1/functional>
# include <tr1/random>
#endif

#ifdef __GXX_EXPERIMENTAL_CXX0X__
    typedef std::uniform_real_distribution<double>      distribution_type;
    typedef std::mt19937                                engine_type;
#else
    typedef std::tr1::uniform_real<double>              distribution_type;
    typedef std::tr1::mt19937                           engine_type;
#endif

double
generate()
{
  const distribution_type distribution(0, 1);
  static engine_type engine;

#ifdef __GXX_EXPERIMENTAL_CXX0X__
  static auto generator = std::bind(distribution, engine);
#else
  typedef std::tr1::_Bind<distribution_type(engine_type)> __result_type;
  static __result_type generator = std::tr1::bind(distribution, engine);
#endif

  double random = generator();
  if (random < distribution.min() || random > distribution.max())
    {
      std::string __s("throw_allocator::throw_conditionally");
      __s += "\n";
      __s += "random number generated is: ";
      char buf[40];
      __builtin_sprintf(buf, "%f", random);
      __s += buf;
      std::__throw_out_of_range(__s.c_str());
    }
  else
    std::cout << random << std::endl;

  return random;
}

int main()
{
  for (int i(0); i < 50; ++i)
    std::cout << generate() << std::endl;
  return 0;
}


Gives me this for C++03:

%./a.out 
terminate called after throwing an instance of 'std::out_of_range'
  what():  throw_allocator::throw_conditionally
random number generated is: 3499211612.000000
Abort

Gives me this for C++0x (-std=gnu++0x)
...
0.0512164
0.0512164
0.0364413
0.0364413
0.408731
0.408731
0.457989
0.457989
0.487569
0.487569
0.793975
0.793975
0.920875
0.920875
0.807531
0.807531
...


-- 
           Summary: random unform real distro: tr1 vs c++0x
           Product: gcc
           Version: 4.4.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: bkoz at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40263

Reply via email to