Commit 42031254 by Paolo Carlini Committed by Paolo Carlini

random (class binomial_distribution<>): Add.

2006-08-18  Paolo Carlini  <pcarlini@suse.de>

	* include/tr1/random (class binomial_distribution<>): Add.
	* include/tr1/random.tcc (binomial_distribution<>::operator(),
	operator<<(std::basic_ostream<>&, const binomial_distribution<>&),
	operator>>(std::basic_istream<>&, binomial_distribution<>&,
	binomial_distribution<>::_M_waiting(), binomial_distribution<>::
	_M_initialize()): Define.
	* testsuite/tr1/5_numerical_facilities/random/binomial_distribution/
	requirements/typedefs.cc: New.

	* include/tr1/random (geometric_distribution<>::
	geometric_distribution(const _RealType&)): Fix DEBUG_ASSERT
	limits.

	* include/tr1/random (poisson_distribution): Add normal_distribution
	member, adjust consistently; minor tweaks and rearrangements of the
	arithmetic.
	(operator>>(std::basic_istream<>&, poisson_distribution<>&)): Move
	out of line.
	* include/tr1/random.tcc: Adjust.

	* include/tr1/random.tcc (normal_distribution<>::operator()): Minor
	tweaks.

From-SVN: r116245
parent 7867a3f7
2006-08-18 Paolo Carlini <pcarlini@suse.de>
* include/tr1/random (class binomial_distribution<>): Add.
* include/tr1/random.tcc (binomial_distribution<>::operator(),
operator<<(std::basic_ostream<>&, const binomial_distribution<>&),
operator>>(std::basic_istream<>&, binomial_distribution<>&,
binomial_distribution<>::_M_waiting(), binomial_distribution<>::
_M_initialize()): Define.
* testsuite/tr1/5_numerical_facilities/random/binomial_distribution/
requirements/typedefs.cc: New.
* include/tr1/random (geometric_distribution<>::
geometric_distribution(const _RealType&)): Fix DEBUG_ASSERT
limits.
* include/tr1/random (poisson_distribution): Add normal_distribution
member, adjust consistently; minor tweaks and rearrangements of the
arithmetic.
(operator>>(std::basic_istream<>&, poisson_distribution<>&)): Move
out of line.
* include/tr1/random.tcc: Adjust.
* include/tr1/random.tcc (normal_distribution<>::operator()): Minor
tweaks.
2006-08-18 Paolo Carlini <pcarlini@suse.de>
PR libstdc++/28765
* include/ext/rc_string_base.h (_M_clear): New.
* include/ext/sso_string_base.h (_M_clear): Likewise.
......
......@@ -1588,9 +1588,9 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
// constructors and member function
explicit
geometric_distribution(const _RealType& __p = _RealType(0.5))
: _M_p(__p), _M_log_p(std::log(_M_p))
: _M_p(__p), _M_log_p(std::log(__p))
{
_GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
_GLIBCXX_DEBUG_ASSERT((_M_p > 0.0) && (_M_p < 1.0));
}
/**
......@@ -1649,6 +1649,9 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
};
template<typename _RealType>
class normal_distribution;
/**
* @brief A discrete Poisson random number distribution.
*
......@@ -1665,6 +1668,12 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const poisson_distribution<_IntType, _RealType>& __x);
template<typename _IntType, typename _RealType,
typename _CharT, typename _Traits>
std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
poisson_distribution<_IntType, _RealType>& __x);
template<typename _IntType, typename _RealType>
class poisson_distribution
{
......@@ -1676,7 +1685,7 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
// constructors and member function
explicit
poisson_distribution(const _RealType& __mean = _RealType(1))
: _M_mean(__mean)
: _M_mean(__mean), _M_nd()
{
_GLIBCXX_DEBUG_ASSERT(_M_mean > 0.0);
_M_initialize();
......@@ -1690,7 +1699,8 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
{ return _M_mean; }
void
reset() { }
reset()
{ _M_nd.reset(); }
template<class _UniformRandomNumberGenerator>
result_type
......@@ -1721,27 +1731,143 @@ _GLIBCXX_BEGIN_NAMESPACE(tr1)
*
* @returns The input stream with @p __x extracted or in an error state.
*/
template<typename _CharT, typename _Traits>
template<typename _IntType1, typename _RealType1,
typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
poisson_distribution& __x)
{
__is >> __x._M_mean;
__x._M_initialize();
return __is;
}
poisson_distribution<_IntType1, _RealType1>& __x);
private:
void
_M_initialize();
// NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
normal_distribution<_RealType> _M_nd;
_RealType _M_mean;
// _M_lm_thr hosts either log(mean) or the threshold of the simple
// method.
_RealType _M_lm_thr;
#if _GLIBCXX_USE_C99_MATH_TR1
_RealType _M_lfm, _M_sm, _M_d, _M_scx4, _M_2cx, _M_c2b, _M_cb;
_RealType _M_lfm, _M_sm, _M_d, _M_scx, _M_1cx, _M_c2b, _M_cb;
#endif
};
/**
* @brief A discrete binomial random number distribution.
*
* The formula for the binomial probability mass function is
* @f$ p(i) = \binom{n}{i} p^i (1 - p)^{t - i} @f$ where @f$ t @f$
* and @f$ p @f$ are the parameters of the distribution.
*/
template<typename _IntType = int, typename _RealType = double>
class binomial_distribution;
template<typename _IntType, typename _RealType,
typename _CharT, typename _Traits>
std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const binomial_distribution<_IntType, _RealType>& __x);
template<typename _IntType, typename _RealType,
typename _CharT, typename _Traits>
std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
binomial_distribution<_IntType, _RealType>& __x);
template<typename _IntType, typename _RealType>
class binomial_distribution
{
public:
// types
typedef _RealType input_type;
typedef _IntType result_type;
// constructors and member function
explicit
binomial_distribution(_IntType __t = 1,
const _RealType& __p = _RealType(0.5))
: _M_t(__t), _M_p(__p), _M_nd()
{
_GLIBCXX_DEBUG_ASSERT((_M_t >= 0) && (_M_p >= 0.0) && (_M_p <= 1.0));
_M_initialize();
}
/**
* Gets the distribution @p t parameter.
*/
_IntType
t() const
{ return _M_t; }
/**
* Gets the distribution @p p parameter.
*/
_RealType
p() const
{ return _M_p; }
void
reset()
{ _M_nd.reset(); }
template<class _UniformRandomNumberGenerator>
result_type
operator()(_UniformRandomNumberGenerator& __urng);
/**
* Inserts a %binomial_distribution random number distribution
* @p __x into the output stream @p __os.
*
* @param __os An output stream.
* @param __x A %binomial_distribution random number distribution.
*
* @returns The output stream with the state of @p __x inserted or in
* an error state.
*/
template<typename _IntType1, typename _RealType1,
typename _CharT, typename _Traits>
friend std::basic_ostream<_CharT, _Traits>&
operator<<(std::basic_ostream<_CharT, _Traits>& __os,
const binomial_distribution<_IntType1, _RealType1>& __x);
/**
* Extracts a %binomial_distribution random number distribution
* @p __x from the input stream @p __is.
*
* @param __is An input stream.
* @param __x A %binomial_distribution random number generator engine.
*
* @returns The input stream with @p __x extracted or in an error state.
*/
template<typename _IntType1, typename _RealType1,
typename _CharT, typename _Traits>
friend std::basic_istream<_CharT, _Traits>&
operator>>(std::basic_istream<_CharT, _Traits>& __is,
binomial_distribution<_IntType1, _RealType1>& __x);
private:
void
_M_initialize();
template<class _UniformRandomNumberGenerator>
result_type
_M_waiting(_UniformRandomNumberGenerator& __urng, _IntType __t);
// NB: Unused when _GLIBCXX_USE_C99_MATH_TR1 is undefined.
normal_distribution<_RealType> _M_nd;
_IntType _M_t;
_RealType _M_p;
_RealType _M_q;
#if _GLIBCXX_USE_C99_MATH_TR1
_RealType _M_d1, _M_d2, _M_s1, _M_s2, _M_c,
_M_a1, _M_a123, _M_s, _M_lf, _M_lp1p;
#endif
bool _M_easy;
};
/* @} */ // group tr1_random_distributions_discrete
......
// { dg-do compile }
//
// 2006-08-18 Paolo Carlini <pcarlini@suse.de>
//
// Copyright (C) 2006 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 2, 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 COPYING. If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.
// 5.1.7.5 Class template binomial_distribution [tr.rand.dist.bin]
// 5.1.1 [7] Table 17
#include <tr1/random>
void
test01()
{
using namespace std::tr1;
typedef binomial_distribution<int, double> test_type;
typedef test_type::input_type input_type;
typedef test_type::result_type result_type;
}
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