Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:59:11

0001 /* boost random/generate_canonical.hpp header file
0002  *
0003  * Copyright Steven Watanabe 2011
0004  * Distributed under the Boost Software License, Version 1.0. (See
0005  * accompanying file LICENSE_1_0.txt or copy at
0006  * http://www.boost.org/LICENSE_1_0.txt)
0007  *
0008  * See http://www.boost.org for most recent version including documentation.
0009  *
0010  * $Id$
0011  *
0012  */
0013 
0014 #ifndef BOOST_RANDOM_GENERATE_CANONICAL_HPP
0015 #define BOOST_RANDOM_GENERATE_CANONICAL_HPP
0016 
0017 #include <algorithm>
0018 #include <boost/assert.hpp>
0019 #include <boost/config/no_tr1/cmath.hpp>
0020 #include <boost/limits.hpp>
0021 #include <boost/type_traits/is_integral.hpp>
0022 #include <boost/random/detail/signed_unsigned_tools.hpp>
0023 #include <boost/random/detail/generator_bits.hpp>
0024 
0025 namespace boost {
0026 namespace random {
0027 
0028 namespace detail {
0029 
0030 template<class RealType, std::size_t bits, class URNG>
0031 RealType generate_canonical_impl(URNG& g, boost::true_type /*is_integral*/)
0032 {
0033     using std::pow;
0034     typedef typename URNG::result_type base_result;
0035     std::size_t digits = std::numeric_limits<RealType>::digits;
0036     RealType R = RealType((g.max)()) - RealType((g.min)()) + 1;
0037     RealType mult = R;
0038     RealType limit =
0039         pow(RealType(2),
0040             RealType((std::min)(static_cast<std::size_t>(bits), digits)));
0041     RealType S = RealType(detail::subtract<base_result>()(g(), (g.min)()));
0042     while(mult < limit) {
0043         RealType inc = RealType(detail::subtract<base_result>()(g(), (g.min)()));
0044         S += inc * mult;
0045         mult *= R;
0046     }
0047     return S / mult;
0048 }
0049 
0050 template<class RealType, std::size_t bits, class URNG>
0051 RealType generate_canonical_impl(URNG& g, boost::false_type /*is_integral*/)
0052 {
0053     using std::pow;
0054     using std::floor;
0055     BOOST_ASSERT((g.min)() == 0);
0056     BOOST_ASSERT((g.max)() == 1);
0057     std::size_t digits = std::numeric_limits<RealType>::digits;
0058     std::size_t engine_bits = detail::generator_bits<URNG>::value();
0059     std::size_t b = (std::min)(bits, digits);
0060     RealType R = pow(RealType(2), RealType(engine_bits));
0061     RealType mult = R;
0062     RealType limit = pow(RealType(2), RealType(b));
0063     RealType S = RealType(g() - (g.min)());
0064     while(mult < limit) {
0065         RealType inc(floor((RealType(g()) - RealType((g.min)())) * R));
0066         S += inc * mult;
0067         mult *= R;
0068     }
0069     return S / mult;
0070 }
0071 
0072 }
0073 
0074 /**
0075  * Returns a value uniformly distributed in the range [0, 1)
0076  * with at least @c bits random bits.
0077  */
0078 template<class RealType, std::size_t bits, class URNG>
0079 RealType generate_canonical(URNG& g)
0080 {
0081     RealType result = detail::generate_canonical_impl<RealType, bits>(
0082         g, boost::random::traits::is_integral<typename URNG::result_type>());
0083     BOOST_ASSERT(result >= 0);
0084     BOOST_ASSERT(result <= 1);
0085     if(result == 1) {
0086         result -= std::numeric_limits<RealType>::epsilon() / 2;
0087         BOOST_ASSERT(result != 1);
0088     }
0089     return result;
0090 }
0091 
0092 } // namespace random
0093 } // namespace boost
0094 
0095 #endif // BOOST_RANDOM_GENERATE_CANONICAL_HPP