Commit 33df19a7 by Edward Smith-Rowland Committed by Jonathan Wakely

Ensure std::generate_canonical doesn't return 1.

2015-08-26  Edward Smith-Rowland  <3dw4rd@verizon.net>
	    Jonathan Wakely  <jwakely@redhat.com>

	PR libstdc++/64351
	PR libstdc++/63176
	* include/bits/random.tcc (generate_canonical): Loop until we get a
	result less than one.
	* testsuite/26_numerics/random/uniform_real_distribution/operators/
	64351.cc: New.

Co-Authored-By: Jonathan Wakely <jwakely@redhat.com>

From-SVN: r227233
parent 6bc41b26
2015-08-26 Edward Smith-Rowland <3dw4rd@verizon.net>
Jonathan Wakely <jwakely@redhat.com>
PR libstdc++/64351
PR libstdc++/63176
* include/bits/random.tcc (generate_canonical): Loop until we get a
result less than one.
* testsuite/26_numerics/random/uniform_real_distribution/operators/
64351.cc: New.
2015-08-26 Jonathan Wakely <jwakely@redhat.com>
* include/bits/shared_ptr.h (__enable_shared_from_this_helper): Use
......
......@@ -3472,15 +3472,22 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
const long double __r = static_cast<long double>(__urng.max())
- static_cast<long double>(__urng.min()) + 1.0L;
const size_t __log2r = std::log(__r) / std::log(2.0L);
size_t __k = std::max<size_t>(1UL, (__b + __log2r - 1UL) / __log2r);
_RealType __sum = _RealType(0);
_RealType __tmp = _RealType(1);
for (; __k != 0; --__k)
const size_t __m = std::max<size_t>(1UL,
(__b + __log2r - 1UL) / __log2r);
_RealType __ret;
do
{
__sum += _RealType(__urng() - __urng.min()) * __tmp;
__tmp *= __r;
_RealType __sum = _RealType(0);
_RealType __tmp = _RealType(1);
for (size_t __k = __m; __k != 0; --__k)
{
__sum += _RealType(__urng() - __urng.min()) * __tmp;
__tmp *= __r;
}
__ret = __sum / __tmp;
}
return __sum / __tmp;
while (__builtin_expect(__ret >= _RealType(1), 0));
return __ret;
}
_GLIBCXX_END_NAMESPACE_VERSION
......
// Copyright (C) 2015 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
// { dg-options "-std=gnu++11" }
// { dg-do run { target { ! simulator } } }
#include <random>
#include <testsuite_hooks.h>
// libstdc++/64351
void
test01()
{
std::mt19937 rng(8890);
std::uniform_real_distribution<float> dist;
rng.discard(30e6);
for (long i = 0; i < 10e6; ++i)
{
auto n = dist(rng);
VERIFY( n != 1.f );
}
}
// libstdc++/63176
void
test02()
{
std::mt19937 rng(8890);
std::seed_seq sequence{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
rng.seed(sequence);
rng.discard(12 * 629143 + 6);
float n =
std::generate_canonical<float, std::numeric_limits<float>::digits>(rng);
VERIFY( n != 1.f );
}
int
main()
{
test01();
test02();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment