Commit 92d85953 by Jonathan Wakely Committed by Jonathan Wakely

PR libstdc++/80137 use std::nextafter instead of looping

	PR libstdc++/80137
	* include/bits/random.tcc (generate_canonical): Use std::nextafter
	or numeric_limits::epsilon() to reduce out-of-range values.
	* testsuite/26_numerics/random/uniform_real_distribution/operators/
	64351.cc: Verify complexity requirement is met.

From-SVN: r246542
parent 9d384e80
2017-03-28 Jonathan Wakely <jwakely@redhat.com> 2017-03-28 Jonathan Wakely <jwakely@redhat.com>
PR libstdc++/80137
* include/bits/random.tcc (generate_canonical): Use std::nextafter
or numeric_limits::epsilon() to reduce out-of-range values.
* testsuite/26_numerics/random/uniform_real_distribution/operators/
64351.cc: Verify complexity requirement is met.
* doc/xml/manual/abi.xml: Add xml:id anchor. * doc/xml/manual/abi.xml: Add xml:id anchor.
* doc/xml/manual/using.xml (manual.intro.using.macros): Document * doc/xml/manual/using.xml (manual.intro.using.macros): Document
_GLIBCXX_RELEASE. Link to new anchor for __GLIBCXX__ notes. _GLIBCXX_RELEASE. Link to new anchor for __GLIBCXX__ notes.
......
...@@ -3323,18 +3323,23 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -3323,18 +3323,23 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
const size_t __m = std::max<size_t>(1UL, const size_t __m = std::max<size_t>(1UL,
(__b + __log2r - 1UL) / __log2r); (__b + __log2r - 1UL) / __log2r);
_RealType __ret; _RealType __ret;
do _RealType __sum = _RealType(0);
_RealType __tmp = _RealType(1);
for (size_t __k = __m; __k != 0; --__k)
{ {
_RealType __sum = _RealType(0); __sum += _RealType(__urng() - __urng.min()) * __tmp;
_RealType __tmp = _RealType(1); __tmp *= __r;
for (size_t __k = __m; __k != 0; --__k) }
{ __ret = __sum / __tmp;
__sum += _RealType(__urng() - __urng.min()) * __tmp; if (__builtin_expect(__ret >= _RealType(1), 0))
__tmp *= __r; {
} #if _GLIBCXX_USE_C99_MATH_TR1
__ret = __sum / __tmp; __ret = std::nextafter(_RealType(1), _RealType(0));
#else
__ret = _RealType(1)
- std::numeric_limits<_RealType>::epsilon() / _RealType(2);
#endif
} }
while (__builtin_expect(__ret >= _RealType(1), 0));
return __ret; return __ret;
} }
......
...@@ -42,10 +42,18 @@ test02() ...@@ -42,10 +42,18 @@ test02()
std::mt19937 rng(8890); std::mt19937 rng(8890);
std::seed_seq sequence{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; std::seed_seq sequence{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
rng.seed(sequence); rng.seed(sequence);
rng.discard(12 * 629143 + 6); rng.discard(12 * 629143);
float n = std::mt19937 rng2{rng};
std::generate_canonical<float, std::numeric_limits<float>::digits>(rng); for (int i = 0; i < 20; ++i)
VERIFY( n != 1.f ); {
float n =
std::generate_canonical<float, std::numeric_limits<float>::digits>(rng);
VERIFY( n != 1.f );
// PR libstdc++/80137
rng2.discard(1);
VERIFY( rng == rng2 );
}
} }
int int
......
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