Home Libraries People FAQ More

#### 連分數計算(Continued Fraction Evaluation)

##### 概要

```#include <boost/math/tools/fraction.hpp>
```

```namespace boost{ namespace math{ namespace tools{

template <class Gen>
typename detail::fraction_traits<Gen>::result_type
continued_fraction_b(Gen& g, int bits);

template <class Gen>
typename detail::fraction_traits<Gen>::result_type
continued_fraction_b(Gen& g, int bits, boost::uintmax_t& max_terms);

template <class Gen>
typename detail::fraction_traits<Gen>::result_type
continued_fraction_a(Gen& g, int bits);

template <class Gen>
typename detail::fraction_traits<Gen>::result_type
continued_fraction_a(Gen& g, int bits, boost::uintmax_t& max_terms);

}}} // namespaces
```

Gen::result_type

g()

##### 例子

golden ratio phi = 1.618033989... 可以使用最簡單的連分數來計算：

```template <class T>
struct golden_ratio_fraction
{
typedef T result_type;

result_type operator()
{
return 1;
}
};
```

```continued_fraction_a(
golden_ratio_fraction<double>(),
std::numeric_limits<double>::digits);
```

```template <class T>
struct tan_fraction
{
private:
T a, b;
public:
tan_fraction(T v)
: a(-v*v), b(-1)
{}

typedef std::pair<T,T> result_type;

std::pair<T,T> operator()()
{
b += 2;
return std::make_pair(a, b);
}
};
```

```template <class T>
T tan(T a)
{
tan_fraction<T> fract(a);
return a / continued_fraction_b(fract, std::numeric_limits<T>::digits);
}
```