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/math/tools/precision.hpp>
14 #include <boost/math/tools/complex.hpp>
15 #include <type_traits>
19 namespace boost{ namespace math{ namespace tools{
25 struct is_pair : public std::false_type{};
27 template <typename T, typename U>
28 struct is_pair<std::pair<T,U>> : public std::true_type{};
30 template <typename Gen>
31 struct fraction_traits_simple
33 using result_type = typename Gen::result_type;
34 using value_type = typename Gen::result_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)
46 template <typename Gen>
47 struct fraction_traits_pair
49 using value_type = typename Gen::result_type;
50 using result_type = typename value_type::first_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)
62 template <typename Gen>
63 struct fraction_traits
64 : public std::conditional<
65 is_pair<typename Gen::result_type>::value,
66 fraction_traits_pair<Gen>,
67 fraction_traits_simple<Gen>>::type
71 template <typename T, bool = is_complex_type<T>::value>
74 // For float, double, and long double, 1/min_value<T>() is finite.
75 // But for mpfr_float and cpp_bin_float, 1/min_value<T>() is inf.
76 // Multiply the min by 16 so that the reciprocal doesn't overflow.
78 return 16*tools::min_value<T>();
82 struct tiny_value<T, true>
84 using value_type = typename T::value_type;
86 return 16*tools::min_value<value_type>();
93 // continued_fraction_b
104 // Note that the first a0 returned by generator Gen is discarded.
106 template <typename Gen, typename U>
107 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b(Gen& g, const U& factor, std::uintmax_t& max_terms)
108 noexcept(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
110 BOOST_MATH_STD_USING // ADL of std names
112 using traits = detail::fraction_traits<Gen>;
113 using result_type = typename traits::result_type;
114 using value_type = typename traits::value_type;
115 using integer_type = typename integer_scalar_type<result_type>::type;
116 using scalar_type = typename scalar_type<result_type>::type;
118 integer_type const zero(0), one(1);
120 result_type tiny = detail::tiny_value<result_type>::get();
121 scalar_type terminator = abs(factor);
125 result_type f, C, D, delta;
132 std::uintmax_t counter(max_terms);
135 D = traits::b(v) + traits::a(v) * D;
136 if(D == result_type(0))
138 C = traits::b(v) + traits::a(v) / C;
144 }while((abs(delta - one) > terminator) && --counter);
146 max_terms = max_terms - counter;
151 template <typename Gen, typename U>
152 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b(Gen& g, const U& factor)
153 noexcept(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
155 std::uintmax_t max_terms = (std::numeric_limits<std::uintmax_t>::max)();
156 return continued_fraction_b(g, factor, max_terms);
159 template <typename Gen>
160 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b(Gen& g, int bits)
161 noexcept(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
163 BOOST_MATH_STD_USING // ADL of std names
165 using traits = detail::fraction_traits<Gen>;
166 using result_type = typename traits::result_type;
168 result_type factor = ldexp(1.0f, 1 - bits); // 1 / pow(result_type(2), bits);
169 std::uintmax_t max_terms = (std::numeric_limits<std::uintmax_t>::max)();
170 return continued_fraction_b(g, factor, max_terms);
173 template <typename Gen>
174 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b(Gen& g, int bits, std::uintmax_t& max_terms)
175 noexcept(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
177 BOOST_MATH_STD_USING // ADL of std names
179 using traits = detail::fraction_traits<Gen>;
180 using result_type = typename traits::result_type;
182 result_type factor = ldexp(1.0f, 1 - bits); // 1 / pow(result_type(2), bits);
183 return continued_fraction_b(g, factor, max_terms);
187 // continued_fraction_a
198 // Note that the first a1 and b1 returned by generator Gen are both used.
200 template <typename Gen, typename U>
201 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a(Gen& g, const U& factor, std::uintmax_t& max_terms)
202 noexcept(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
204 BOOST_MATH_STD_USING // ADL of std names
206 using traits = detail::fraction_traits<Gen>;
207 using result_type = typename traits::result_type;
208 using value_type = typename traits::value_type;
209 using integer_type = typename integer_scalar_type<result_type>::type;
210 using scalar_type = typename scalar_type<result_type>::type;
212 integer_type const zero(0), one(1);
214 result_type tiny = detail::tiny_value<result_type>::get();
215 scalar_type terminator = abs(factor);
219 result_type f, C, D, delta, a0;
227 std::uintmax_t counter(max_terms);
231 D = traits::b(v) + traits::a(v) * D;
234 C = traits::b(v) + traits::a(v) / C;
240 }while((abs(delta - one) > terminator) && --counter);
242 max_terms = max_terms - counter;
247 template <typename Gen, typename U>
248 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a(Gen& g, const U& factor)
249 noexcept(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
251 std::uintmax_t max_iter = (std::numeric_limits<std::uintmax_t>::max)();
252 return continued_fraction_a(g, factor, max_iter);
255 template <typename Gen>
256 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a(Gen& g, int bits)
257 noexcept(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
259 BOOST_MATH_STD_USING // ADL of std names
261 typedef detail::fraction_traits<Gen> traits;
262 typedef typename traits::result_type result_type;
264 result_type factor = ldexp(1.0f, 1-bits); // 1 / pow(result_type(2), bits);
265 std::uintmax_t max_iter = (std::numeric_limits<std::uintmax_t>::max)();
267 return continued_fraction_a(g, factor, max_iter);
270 template <typename Gen>
271 inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a(Gen& g, int bits, std::uintmax_t& max_terms)
272 noexcept(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) && noexcept(std::declval<Gen>()()))
274 BOOST_MATH_STD_USING // ADL of std names
276 using traits = detail::fraction_traits<Gen>;
277 using result_type = typename traits::result_type;
279 result_type factor = ldexp(1.0f, 1-bits); // 1 / pow(result_type(2), bits);
280 return continued_fraction_a(g, factor, max_terms);
287 #endif // BOOST_MATH_TOOLS_FRACTION_INCLUDED