Commit c2d9083d by Haakan Younes Committed by Paolo Carlini

re PR libstdc++/55047 (operator() in std::exponential_distribution may call log(0))

2012-10-24   Haakan Younes  <hyounes@google.com>
	     Paolo Carlini  <paolo.carlini@oracle.com>

	PR libstdc++/55047
	* include/bits/random.h (exponential_distribution<>::operator):
	Fix formula to std::log(result_type(1) - __aurng()).
	* include/bits/random.tcc: Likewise, everywhere.

Co-Authored-By: Paolo Carlini <paolo.carlini@oracle.com>

From-SVN: r192762
parent c6c08485
2012-10-24 Haakan Younes <hyounes@google.com>
Paolo Carlini <paolo.carlini@oracle.com>
PR libstdc++/55047
* include/bits/random.h (exponential_distribution<>::operator):
Fix formula to std::log(result_type(1) - __aurng()).
* include/bits/random.tcc: Likewise, everywhere.
2012-10-23 Yufeng Zhang <yufeng.zhang@arm.com> 2012-10-23 Yufeng Zhang <yufeng.zhang@arm.com>
* config/cpu/aarch64/cxxabi_tweaks.h: New file. * config/cpu/aarch64/cxxabi_tweaks.h: New file.
......
// random number generation -*- C++ -*- // random number generation -*- C++ -*-
// Copyright (C) 2009, 2010, 2011, 2012 Free Software Foundation, Inc. // Copyright (C) 2009-2012 Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // 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 // software; you can redistribute it and/or modify it under the
...@@ -4748,7 +4748,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -4748,7 +4748,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ {
__detail::_Adaptor<_UniformRandomNumberGenerator, result_type> __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
__aurng(__urng); __aurng(__urng);
return -std::log(__aurng()) / __p.lambda(); return -std::log(result_type(1) - __aurng()) / __p.lambda();
} }
template<typename _ForwardIterator, template<typename _ForwardIterator,
......
// random number generation (out of line) -*- C++ -*- // random number generation (out of line) -*- C++ -*-
// Copyright (C) 2009, 2010, 2011, 2012 Free Software Foundation, Inc. // Copyright (C) 2009-2012 Free Software Foundation, Inc.
// //
// This file is part of the GNU ISO C++ Library. This library is free // 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 // software; you can redistribute it and/or modify it under the
...@@ -1187,7 +1187,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -1187,7 +1187,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
double __cand; double __cand;
do do
__cand = std::floor(std::log(__aurng()) / __param._M_log_1_p); __cand = std::floor(std::log(1.0 - __aurng()) / __param._M_log_1_p);
while (__cand >= __thr); while (__cand >= __thr);
return result_type(__cand + __naf); return result_type(__cand + __naf);
...@@ -1217,7 +1217,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -1217,7 +1217,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ {
double __cand; double __cand;
do do
__cand = std::floor(std::log(__aurng()) / __param._M_log_1_p); __cand = std::floor(std::log(1.0 - __aurng())
/ __param._M_log_1_p);
while (__cand >= __thr); while (__cand >= __thr);
*__f++ = __cand + __naf; *__f++ = __cand + __naf;
...@@ -1464,7 +1465,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -1464,7 +1465,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
do do
{ {
const double __u = __c * __aurng(); const double __u = __c * __aurng();
const double __e = -std::log(__aurng()); const double __e = -std::log(1.0 - __aurng());
double __w = 0.0; double __w = 0.0;
...@@ -1496,7 +1497,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -1496,7 +1497,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__x = 1; __x = 1;
else else
{ {
const double __v = -std::log(__aurng()); const double __v = -std::log(1.0 - __aurng());
const double __y = __param._M_d const double __y = __param._M_d
+ __v * __2cx / __param._M_d; + __v * __2cx / __param._M_d;
__x = std::ceil(__y); __x = std::ceil(__y);
...@@ -1655,7 +1656,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -1655,7 +1656,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
do do
{ {
const double __e = -std::log(__aurng()); const double __e = -std::log(1.0 - __aurng());
__sum += __e / (__t - __x); __sum += __e / (__t - __x);
__x += 1; __x += 1;
} }
...@@ -1723,7 +1724,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -1723,7 +1724,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__reject = __y >= __param._M_d1; __reject = __y >= __param._M_d1;
if (!__reject) if (!__reject)
{ {
const double __e = -std::log(__aurng()); const double __e = -std::log(1.0 - __aurng());
__x = std::floor(__y); __x = std::floor(__y);
__v = -__e - __n * __n / 2 + __param._M_c; __v = -__e - __n * __n / 2 + __param._M_c;
} }
...@@ -1735,15 +1736,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -1735,15 +1736,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__reject = __y >= __param._M_d2; __reject = __y >= __param._M_d2;
if (!__reject) if (!__reject)
{ {
const double __e = -std::log(__aurng()); const double __e = -std::log(1.0 - __aurng());
__x = std::floor(-__y); __x = std::floor(-__y);
__v = -__e - __n * __n / 2; __v = -__e - __n * __n / 2;
} }
} }
else if (__u <= __a123) else if (__u <= __a123)
{ {
const double __e1 = -std::log(__aurng()); const double __e1 = -std::log(1.0 - __aurng());
const double __e2 = -std::log(__aurng()); const double __e2 = -std::log(1.0 - __aurng());
const double __y = __param._M_d1 const double __y = __param._M_d1
+ 2 * __s1s * __e1 / __param._M_d1; + 2 * __s1s * __e1 / __param._M_d1;
...@@ -1754,8 +1755,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -1754,8 +1755,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
} }
else else
{ {
const double __e1 = -std::log(__aurng()); const double __e1 = -std::log(1.0 - __aurng());
const double __e2 = -std::log(__aurng()); const double __e2 = -std::log(1.0 - __aurng());
const double __y = __param._M_d2 const double __y = __param._M_d2
+ 2 * __s2s * __e1 / __param._M_d2; + 2 * __s2s * __e1 / __param._M_d2;
...@@ -1869,7 +1870,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -1869,7 +1870,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__detail::_Adaptor<_UniformRandomNumberGenerator, result_type> __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
__aurng(__urng); __aurng(__urng);
while (__f != __t) while (__f != __t)
*__f++ = -std::log(__aurng()) / __p.lambda(); *__f++ = -std::log(result_type(1) - __aurng()) / __p.lambda();
} }
template<typename _RealType, typename _CharT, typename _Traits> template<typename _RealType, typename _CharT, typename _Traits>
...@@ -2628,7 +2629,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -2628,7 +2629,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ {
__detail::_Adaptor<_UniformRandomNumberGenerator, result_type> __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
__aurng(__urng); __aurng(__urng);
return __p.b() * std::pow(-std::log(__aurng()), return __p.b() * std::pow(-std::log(result_type(1) - __aurng()),
result_type(1) / __p.a()); result_type(1) / __p.a());
} }
...@@ -2644,10 +2645,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -2644,10 +2645,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
__detail::_Adaptor<_UniformRandomNumberGenerator, result_type> __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
__aurng(__urng); __aurng(__urng);
auto inv_a = result_type(1) / __p.a(); auto __inv_a = result_type(1) / __p.a();
while (__f != __t) while (__f != __t)
*__f++ = __p.b() * std::pow(-std::log(__aurng()), inv_a); *__f++ = __p.b() * std::pow(-std::log(result_type(1) - __aurng()),
__inv_a);
} }
template<typename _RealType, typename _CharT, typename _Traits> template<typename _RealType, typename _CharT, typename _Traits>
...@@ -2704,7 +2706,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -2704,7 +2706,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ {
__detail::_Adaptor<_UniformRandomNumberGenerator, result_type> __detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
__aurng(__urng); __aurng(__urng);
return __p.a() - __p.b() * std::log(-std::log(__aurng())); return __p.a() - __p.b() * std::log(-std::log(result_type(1)
- __aurng()));
} }
template<typename _RealType> template<typename _RealType>
...@@ -2721,7 +2724,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -2721,7 +2724,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__aurng(__urng); __aurng(__urng);
while (__f != __t) while (__f != __t)
*__f++ = __p.a() - __p.b() * std::log(-std::log(__aurng())); *__f++ = __p.a() - __p.b() * std::log(-std::log(result_type(1)
- __aurng()));
} }
template<typename _RealType, typename _CharT, typename _Traits> template<typename _RealType, typename _CharT, typename _Traits>
......
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