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>
18 #include <boost/math/tools/complex.hpp>
20 namespace boost{ namespace math{ namespace tools{
26 struct is_pair : public boost::false_type{};
28 template <class T, class U>
29 struct is_pair<std::pair<T,U> > : public boost::true_type{};
32 struct fraction_traits_simple
34 typedef typename Gen::result_type result_type;
35 typedef typename Gen::result_type value_type;
37 static result_type a(const value_type&) BOOST_MATH_NOEXCEPT(value_type)
41 static result_type b(const value_type& v) BOOST_MATH_NOEXCEPT(value_type)
48 struct fraction_traits_pair
50 typedef typename Gen::result_type value_type;
51 typedef typename value_type::first_type result_type;
53 static result_type a(const value_type& v) BOOST_MATH_NOEXCEPT(value_type)
57 static result_type b(const value_type& v) BOOST_MATH_NOEXCEPT(value_type)
64 struct fraction_traits
65 : public boost::mpl::if_c<
66 is_pair<typename Gen::result_type>::value,
67 fraction_traits_pair<Gen>,
68 fraction_traits_simple<Gen> >::type
72 template <class T, bool = is_complex_type<T>::value>
76 return tools::min_value<T>();
80 struct tiny_value<T, true>
82 typedef typename T::value_type value_type;
84 return tools::min_value<value_type>();
91 // continued_fraction_b
102 // Note that the first a0 returned by generator Gen is disarded.
104 template <class Gen, class U>
105 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b(Gen& g, const U& factor, boost::uintmax_t& max_terms)
106 BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
108 BOOST_MATH_STD_USING // ADL of std names
110 typedef detail::fraction_traits<Gen> traits;
111 typedef typename traits::result_type result_type;
112 typedef typename traits::value_type value_type;
113 typedef typename integer_scalar_type<result_type>::type integer_type;
114 typedef typename scalar_type<result_type>::type scalar_type;
116 integer_type const zero(0), one(1);
118 result_type tiny = detail::tiny_value<result_type>::get();
119 scalar_type terminator = abs(factor);
123 result_type f, C, D, delta;
130 boost::uintmax_t counter(max_terms);
134 D = traits::b(v) + traits::a(v) * D;
135 if(D == result_type(0))
137 C = traits::b(v) + traits::a(v) / C;
143 }while((abs(delta - one) > terminator) && --counter);
145 max_terms = max_terms - counter;
150 template <class Gen, class U>
151 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b(Gen& g, const U& factor)
152 BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
154 boost::uintmax_t max_terms = (std::numeric_limits<boost::uintmax_t>::max)();
155 return continued_fraction_b(g, factor, max_terms);
159 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b(Gen& g, int bits)
160 BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
162 BOOST_MATH_STD_USING // ADL of std names
164 typedef detail::fraction_traits<Gen> traits;
165 typedef typename traits::result_type result_type;
167 result_type factor = ldexp(1.0f, 1 - bits); // 1 / pow(result_type(2), bits);
168 boost::uintmax_t max_terms = (std::numeric_limits<boost::uintmax_t>::max)();
169 return continued_fraction_b(g, factor, max_terms);
173 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b(Gen& g, int bits, boost::uintmax_t& max_terms)
174 BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
176 BOOST_MATH_STD_USING // ADL of std names
178 typedef detail::fraction_traits<Gen> traits;
179 typedef typename traits::result_type result_type;
181 result_type factor = ldexp(1.0f, 1 - bits); // 1 / pow(result_type(2), bits);
182 return continued_fraction_b(g, factor, max_terms);
186 // continued_fraction_a
197 // Note that the first a1 and b1 returned by generator Gen are both used.
199 template <class Gen, class U>
200 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a(Gen& g, const U& factor, boost::uintmax_t& max_terms)
201 BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
203 BOOST_MATH_STD_USING // ADL of std names
205 typedef detail::fraction_traits<Gen> traits;
206 typedef typename traits::result_type result_type;
207 typedef typename traits::value_type value_type;
208 typedef typename integer_scalar_type<result_type>::type integer_type;
209 typedef typename scalar_type<result_type>::type scalar_type;
211 integer_type const zero(0), one(1);
213 result_type tiny = detail::tiny_value<result_type>::get();
214 scalar_type terminator = abs(factor);
218 result_type f, C, D, delta, a0;
226 boost::uintmax_t counter(max_terms);
230 D = traits::b(v) + traits::a(v) * D;
233 C = traits::b(v) + traits::a(v) / C;
239 }while((abs(delta - one) > terminator) && --counter);
241 max_terms = max_terms - counter;
246 template <class Gen, class U>
247 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a(Gen& g, const U& factor)
248 BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
250 boost::uintmax_t max_iter = (std::numeric_limits<boost::uintmax_t>::max)();
251 return continued_fraction_a(g, factor, max_iter);
255 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a(Gen& g, int bits)
256 BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
258 BOOST_MATH_STD_USING // ADL of std names
260 typedef detail::fraction_traits<Gen> traits;
261 typedef typename traits::result_type result_type;
263 result_type factor = ldexp(1.0f, 1-bits); // 1 / pow(result_type(2), bits);
264 boost::uintmax_t max_iter = (std::numeric_limits<boost::uintmax_t>::max)();
266 return continued_fraction_a(g, factor, max_iter);
270 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a(Gen& g, int bits, boost::uintmax_t& max_terms)
271 BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
273 BOOST_MATH_STD_USING // ADL of std names
275 typedef detail::fraction_traits<Gen> traits;
276 typedef typename traits::result_type result_type;
278 result_type factor = ldexp(1.0f, 1-bits); // 1 / pow(result_type(2), bits);
279 return continued_fraction_a(g, factor, max_terms);
286 #endif // BOOST_MATH_TOOLS_FRACTION_INCLUDED