1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
/*
* Copyright Nick Thompson, 2020
* Use, modification and distribution are subject to the
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#include "math_unit_test.hpp"
#include <boost/math/tools/simple_continued_fraction.hpp>
#include <boost/math/constants/constants.hpp>
#include <boost/core/demangle.hpp>
#ifdef BOOST_HAS_FLOAT128
#include <boost/multiprecision/float128.hpp>
using boost::multiprecision::float128;
#endif
#include <boost/multiprecision/cpp_bin_float.hpp>
using boost::math::tools::simple_continued_fraction;
using boost::multiprecision::cpp_bin_float_100;
using boost::math::constants::pi;
template<class Real>
void test_integral()
{
for (int64_t i = -20; i < 20; ++i) {
Real ii = i;
auto cfrac = simple_continued_fraction<Real>(ii);
auto const & a = cfrac.partial_denominators();
CHECK_EQUAL(size_t(1), a.size());
CHECK_EQUAL(i, a.front());
}
}
template<class Real>
void test_halves()
{
for (int64_t i = -20; i < 20; ++i) {
Real x = i + Real(1)/Real(2);
auto cfrac = simple_continued_fraction<Real>(x);
auto const & a = cfrac.partial_denominators();
CHECK_EQUAL(size_t(2), a.size());
CHECK_EQUAL(i, a.front());
CHECK_EQUAL(int64_t(2), a.back());
}
// We'll also test quarters; why not?
for (int64_t i = -20; i < 20; ++i) {
Real x = i + Real(1)/Real(4);
auto cfrac = simple_continued_fraction<Real>(x);
auto const & a = cfrac.partial_denominators();
CHECK_EQUAL(size_t(2), a.size());
CHECK_EQUAL(i, a.front());
CHECK_EQUAL(int64_t(4), a.back());
}
for (int64_t i = -20; i < 20; ++i) {
Real x = i + Real(1)/Real(8);
auto cfrac = simple_continued_fraction<Real>(x);
auto const & a = cfrac.partial_denominators();
CHECK_EQUAL(size_t(2), a.size());
CHECK_EQUAL(i, a.front());
CHECK_EQUAL(int64_t(8), a.back());
}
for (int64_t i = -20; i < 20; ++i) {
Real x = i + Real(3)/Real(4);
auto cfrac = simple_continued_fraction<Real>(x);
auto const & a = cfrac.partial_denominators();
CHECK_EQUAL(size_t(3), a.size());
CHECK_EQUAL(i, a.front());
CHECK_EQUAL(int64_t(1), a[1]);
CHECK_EQUAL(int64_t(3), a.back());
}
for (int64_t i = -20; i < 20; ++i) {
Real x = i + Real(7)/Real(8);
auto cfrac = simple_continued_fraction<Real>(x);
auto const & a = cfrac.partial_denominators();
CHECK_EQUAL(size_t(3), a.size());
CHECK_EQUAL(i, a.front());
CHECK_EQUAL(int64_t(1), a[1]);
CHECK_EQUAL(int64_t(7), a.back());
}
}
template<typename Real>
void test_simple()
{
std::cout << "Testing rational numbers on type " << boost::core::demangle(typeid(Real).name()) << "\n";
{
Real x = Real(649)/200;
// ContinuedFraction[649/200] = [3; 4, 12, 4]
auto cfrac = simple_continued_fraction(x);
auto const & a = cfrac.partial_denominators();
CHECK_EQUAL(size_t(4), a.size());
CHECK_EQUAL(int64_t(3), a[0]);
CHECK_EQUAL(int64_t(4), a[1]);
CHECK_EQUAL(int64_t(12), a[2]);
CHECK_EQUAL(int64_t(4), a[3]);
}
{
Real x = Real(415)/Real(93);
// [4; 2, 6, 7]:
auto cfrac = simple_continued_fraction(x);
auto const & a = cfrac.partial_denominators();
CHECK_EQUAL(size_t(4), a.size());
CHECK_EQUAL(int64_t(4), a[0]);
CHECK_EQUAL(int64_t(2), a[1]);
CHECK_EQUAL(int64_t(6), a[2]);
CHECK_EQUAL(int64_t(7), a[3]);
}
}
template<typename Real>
void test_khinchin()
{
// These are simply sanity checks; the convergence is too slow otherwise:
auto cfrac = simple_continued_fraction(pi<Real>());
auto K0 = cfrac.khinchin_geometric_mean();
CHECK_MOLLIFIED_CLOSE(Real(2.6854520010), K0, 0.1);
auto Km1 = cfrac.khinchin_harmonic_mean();
CHECK_MOLLIFIED_CLOSE(Real(1.74540566240), Km1, 0.1);
using std::sqrt;
auto rt_cfrac = simple_continued_fraction(sqrt(static_cast<Real>(2)));
K0 = rt_cfrac.khinchin_geometric_mean();
CHECK_ULP_CLOSE(Real(2), K0, 10);
Km1 = rt_cfrac.khinchin_harmonic_mean();
CHECK_ULP_CLOSE(Real(2), Km1, 10);
}
int main()
{
test_integral<float>();
test_integral<double>();
test_integral<long double>();
test_integral<cpp_bin_float_100>();
test_halves<float>();
test_halves<double>();
test_halves<long double>();
test_halves<cpp_bin_float_100>();
test_simple<float>();
test_simple<double>();
test_simple<long double>();
test_simple<cpp_bin_float_100>();
test_khinchin<cpp_bin_float_100>();
#ifdef BOOST_HAS_FLOAT128
test_integral<float128>();
test_halves<float128>();
test_simple<float128>();
test_khinchin<float128>();
#endif
return boost::math::test::report_errors();
}
|