1 // (C) Copyright John Maddock 2005-2006.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef BOOST_MATH_TOOLS_FRACTION_INCLUDED
7 #define BOOST_MATH_TOOLS_FRACTION_INCLUDED
13 #include <boost/config/no_tr1/cmath.hpp>
14 #include <boost/cstdint.hpp>
15 #include <boost/type_traits/integral_constant.hpp>
16 #include <boost/mpl/if.hpp>
17 #include <boost/math/tools/precision.hpp>
19 namespace boost{ namespace math{ namespace tools{
25 struct is_pair : public boost::false_type{};
27 template <class T, class U>
28 struct is_pair<std::pair<T,U> > : public boost::true_type{};
31 struct fraction_traits_simple
33 typedef typename Gen::result_type result_type;
34 typedef typename Gen::result_type value_type;
36 static result_type a(const value_type&) BOOST_MATH_NOEXCEPT(value_type)
40 static result_type b(const value_type& v) BOOST_MATH_NOEXCEPT(value_type)
47 struct fraction_traits_pair
49 typedef typename Gen::result_type value_type;
50 typedef typename value_type::first_type result_type;
52 static result_type a(const value_type& v) BOOST_MATH_NOEXCEPT(value_type)
56 static result_type b(const value_type& v) BOOST_MATH_NOEXCEPT(value_type)
63 struct fraction_traits
64 : public boost::mpl::if_c<
65 is_pair<typename Gen::result_type>::value,
66 fraction_traits_pair<Gen>,
67 fraction_traits_simple<Gen> >::type
74 // continued_fraction_b
85 // Note that the first a0 returned by generator Gen is disarded.
87 template <class Gen, class U>
88 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b(Gen& g, const U& factor, boost::uintmax_t& max_terms)
89 BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
91 BOOST_MATH_STD_USING // ADL of std names
93 typedef detail::fraction_traits<Gen> traits;
94 typedef typename traits::result_type result_type;
95 typedef typename traits::value_type value_type;
97 result_type tiny = tools::min_value<result_type>();
101 result_type f, C, D, delta;
108 boost::uintmax_t counter(max_terms);
112 D = traits::b(v) + traits::a(v) * D;
115 C = traits::b(v) + traits::a(v) / C;
121 }while((fabs(delta - 1) > factor) && --counter);
123 max_terms = max_terms - counter;
128 template <class Gen, class U>
129 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b(Gen& g, const U& factor)
130 BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
132 boost::uintmax_t max_terms = (std::numeric_limits<boost::uintmax_t>::max)();
133 return continued_fraction_b(g, factor, max_terms);
137 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b(Gen& g, int bits)
138 BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
140 BOOST_MATH_STD_USING // ADL of std names
142 typedef detail::fraction_traits<Gen> traits;
143 typedef typename traits::result_type result_type;
145 result_type factor = ldexp(1.0f, 1 - bits); // 1 / pow(result_type(2), bits);
146 boost::uintmax_t max_terms = (std::numeric_limits<boost::uintmax_t>::max)();
147 return continued_fraction_b(g, factor, max_terms);
151 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b(Gen& g, int bits, boost::uintmax_t& max_terms)
152 BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
154 BOOST_MATH_STD_USING // ADL of std names
156 typedef detail::fraction_traits<Gen> traits;
157 typedef typename traits::result_type result_type;
159 result_type factor = ldexp(1.0f, 1 - bits); // 1 / pow(result_type(2), bits);
160 return continued_fraction_b(g, factor, max_terms);
164 // continued_fraction_a
175 // Note that the first a1 and b1 returned by generator Gen are both used.
177 template <class Gen, class U>
178 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a(Gen& g, const U& factor, boost::uintmax_t& max_terms)
179 BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
181 BOOST_MATH_STD_USING // ADL of std names
183 typedef detail::fraction_traits<Gen> traits;
184 typedef typename traits::result_type result_type;
185 typedef typename traits::value_type value_type;
187 result_type tiny = tools::min_value<result_type>();
191 result_type f, C, D, delta, a0;
199 boost::uintmax_t counter(max_terms);
203 D = traits::b(v) + traits::a(v) * D;
206 C = traits::b(v) + traits::a(v) / C;
212 }while((fabs(delta - 1) > factor) && --counter);
214 max_terms = max_terms - counter;
219 template <class Gen, class U>
220 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a(Gen& g, const U& factor)
221 BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
223 boost::uintmax_t max_iter = (std::numeric_limits<boost::uintmax_t>::max)();
224 return continued_fraction_a(g, factor, max_iter);
228 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a(Gen& g, int bits)
229 BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
231 BOOST_MATH_STD_USING // ADL of std names
233 typedef detail::fraction_traits<Gen> traits;
234 typedef typename traits::result_type result_type;
236 result_type factor = ldexp(1.0f, 1-bits); // 1 / pow(result_type(2), bits);
237 boost::uintmax_t max_iter = (std::numeric_limits<boost::uintmax_t>::max)();
239 return continued_fraction_a(g, factor, max_iter);
243 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a(Gen& g, int bits, boost::uintmax_t& max_terms)
244 BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
246 BOOST_MATH_STD_USING // ADL of std names
248 typedef detail::fraction_traits<Gen> traits;
249 typedef typename traits::result_type result_type;
251 result_type factor = ldexp(1.0f, 1-bits); // 1 / pow(result_type(2), bits);
252 return continued_fraction_a(g, factor, max_terms);
259 #endif // BOOST_MATH_TOOLS_FRACTION_INCLUDED