]> git.donarmstrong.com Git - rsem.git/blob - boost/random/generate_canonical.hpp
Updated boost to v1.55.0
[rsem.git] / boost / random / generate_canonical.hpp
1 /* boost random/generate_canonical.hpp header file
2  *
3  * Copyright Steven Watanabe 2011
4  * Distributed under the Boost Software License, Version 1.0. (See
5  * accompanying file LICENSE_1_0.txt or copy at
6  * http://www.boost.org/LICENSE_1_0.txt)
7  *
8  * See http://www.boost.org for most recent version including documentation.
9  *
10  * $Id: generate_canonical.hpp 72951 2011-07-07 04:57:37Z steven_watanabe $
11  *
12  */
13
14 #ifndef BOOST_RANDOM_GENERATE_CANONICAL_HPP
15 #define BOOST_RANDOM_GENERATE_CANONICAL_HPP
16
17 #include <algorithm>
18 #include <boost/assert.hpp>
19 #include <boost/config/no_tr1/cmath.hpp>
20 #include <boost/limits.hpp>
21 #include <boost/type_traits/is_integral.hpp>
22 #include <boost/math/special_functions.hpp>
23 #include <boost/random/detail/signed_unsigned_tools.hpp>
24 #include <boost/random/detail/generator_bits.hpp>
25
26 namespace boost {
27 namespace random {
28
29 namespace detail {
30
31 template<class RealType, std::size_t bits, class URNG>
32 RealType generate_canonical_impl(URNG& g, boost::mpl::true_ /*is_integral*/)
33 {
34     using std::pow;
35     typedef typename URNG::result_type base_result;
36     std::size_t digits = std::numeric_limits<RealType>::digits;
37     RealType R = RealType((g.max)()) - RealType((g.min)()) + 1;
38     RealType mult = R;
39     RealType limit =
40         pow(RealType(2),
41             RealType((std::min)(static_cast<std::size_t>(bits), digits)));
42     RealType S = RealType(detail::subtract<base_result>()(g(), (g.min)()));
43     while(mult < limit) {
44         RealType inc = RealType(detail::subtract<base_result>()(g(), (g.min)()));
45         S += inc * mult;
46         mult *= R;
47     }
48     return S / mult;
49 }
50
51 template<class RealType, std::size_t bits, class URNG>
52 RealType generate_canonical_impl(URNG& g, boost::mpl::false_ /*is_integral*/)
53 {
54     using std::pow;
55     using std::floor;
56     BOOST_ASSERT((g.min)() == 0);
57     BOOST_ASSERT((g.max)() == 1);
58     typedef typename URNG::result_type base_result;
59     std::size_t digits = std::numeric_limits<RealType>::digits;
60     std::size_t engine_bits = detail::generator_bits<URNG>::value();
61     std::size_t b = (std::min)(bits, digits);
62     RealType R = pow(RealType(2), RealType(engine_bits));
63     RealType mult = R;
64     RealType limit = pow(RealType(2), RealType(b));
65     RealType S = RealType(g() - (g.min)());
66     while(mult < limit) {
67         RealType inc(floor((RealType(g()) - RealType((g.min)())) * R));
68         S += inc * mult;
69         mult *= R;
70     }
71     return S / mult;
72 }
73
74 }
75
76 /**
77  * Returns a value uniformly distributed in the range [0, 1)
78  * with at least @c bits random bits.
79  */
80 template<class RealType, std::size_t bits, class URNG>
81 RealType generate_canonical(URNG& g)
82 {
83     RealType result = detail::generate_canonical_impl<RealType, bits>(
84         g, boost::is_integral<typename URNG::result_type>());
85     BOOST_ASSERT(result >= 0);
86     BOOST_ASSERT(result <= 1);
87     if(result == 1) {
88         result -= std::numeric_limits<RealType>::epsilon() / 2;
89         BOOST_ASSERT(result != 1);
90     }
91     return result;
92 }
93
94 } // namespace random
95 } // namespace boost
96
97 #endif // BOOST_RANDOM_GENERATE_CANONICAL_HPP