]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/test/test_convert_from_mpf_float.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / multiprecision / test / test_convert_from_mpf_float.cpp
1 ///////////////////////////////////////////////////////////////
2 // Copyright 2012 John Maddock. Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
5
6 #ifdef _MSC_VER
7 # define _SCL_SECURE_NO_WARNINGS
8 #endif
9
10 #if defined(HAS_GMP)
11
12 #include <boost/multiprecision/cpp_int.hpp>
13 #include <boost/random/mersenne_twister.hpp>
14 #include <boost/random/uniform_int.hpp>
15 #include "test.hpp"
16
17 #if defined(HAS_GMP)
18 #include <boost/multiprecision/gmp.hpp>
19 #endif
20 #if defined(HAS_MPFR)
21 #include <boost/multiprecision/mpfr.hpp>
22 #endif
23 #if defined(HAS_MPFI)
24 #include <boost/multiprecision/mpfi.hpp>
25 #endif
26 #ifdef HAS_TOMMATH
27 #include <boost/multiprecision/tommath.hpp>
28 #endif
29 #ifdef HAS_FLOAT128
30 #include <boost/multiprecision/float128.hpp>
31 #endif
32 #include <boost/multiprecision/cpp_bin_float.hpp>
33 #include <boost/multiprecision/cpp_dec_float.hpp>
34
35
36 using namespace boost::multiprecision;
37
38 #ifdef BOOST_MSVC
39 #pragma warning(disable:4127)
40 #endif
41
42
43 template <class T>
44 T generate_random()
45 {
46 typedef int e_type;
47 static boost::random::mt19937 gen;
48 T val = gen();
49 T prev_val = -1;
50 while(val != prev_val)
51 {
52 val *= (gen.max)();
53 prev_val = val;
54 val += gen();
55 }
56 e_type e;
57 val = frexp(val, &e);
58
59 static boost::random::uniform_int_distribution<e_type> ui(-20, 20);
60 return ldexp(val, ui(gen));
61 }
62
63 template <class From, class To>
64 void test_convert_neg_int(From from, const boost::mpl::true_&)
65 {
66 from = -from;
67 To t3(from);
68 To t4 = from.template convert_to<To>();
69 BOOST_CHECK_EQUAL(From(trunc(from)), From(t3));
70 BOOST_CHECK_EQUAL(From(trunc(from)), From(t4));
71 }
72 template <class From, class To>
73 void test_convert_neg_int(From const&, const boost::mpl::false_&)
74 {
75 }
76
77 template <class From, class To>
78 void test_convert_imp(boost::mpl::int_<number_kind_floating_point> const&, boost::mpl::int_<number_kind_integer> const&)
79 {
80 for(unsigned i = 0; i < 100; ++i)
81 {
82 From from = generate_random<From>();
83 To t1(from);
84 To t2 = from.template convert_to<To>();
85 BOOST_CHECK_EQUAL(From(trunc(from)), From(t1));
86 BOOST_CHECK_EQUAL(From(trunc(from)), From(t2));
87 test_convert_neg_int<From, To>(from, boost::mpl::bool_<std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed>());
88 }
89 }
90
91 template <class From, class To>
92 void test_convert_neg_rat(From from, const boost::mpl::true_&)
93 {
94 from = -from;
95 To t3(from);
96 To t4 = from.template convert_to<To>();
97 From tol = std::numeric_limits<From>::epsilon();
98 BOOST_CHECK_CLOSE_FRACTION(From(t3), from, tol);
99 BOOST_CHECK_CLOSE_FRACTION(From(t4), from, tol);
100 }
101 template <class From, class To>
102 void test_convert_rat_int(From const&, const boost::mpl::false_&)
103 {
104 }
105
106 template <class From, class To>
107 void test_convert_imp(boost::mpl::int_<number_kind_floating_point> const&, boost::mpl::int_<number_kind_rational> const&)
108 {
109 for(unsigned i = 0; i < 100; ++i)
110 {
111 From from = generate_random<From>();
112 To t1(from);
113 To t2 = from.template convert_to<To>();
114 From tol = std::numeric_limits<From>::epsilon();
115 BOOST_CHECK_CLOSE_FRACTION(From(t1), from, tol);
116 BOOST_CHECK_CLOSE_FRACTION(From(t2), from, tol);
117 test_convert_neg_rat<From, To>(from, boost::mpl::bool_<std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed>());
118 }
119 }
120
121 template <class From, class To>
122 void test_convert_neg_float(From from, const boost::mpl::true_&)
123 {
124 from = -from;
125 To t3(from);
126 To t4 = from.template convert_to<To>();
127 To answer(from.str());
128 To tol = (std::max)(std::numeric_limits<To>::epsilon(), To(std::numeric_limits<From>::epsilon())) * 2;
129 BOOST_CHECK_CLOSE_FRACTION(t3, answer, tol);
130 BOOST_CHECK_CLOSE_FRACTION(t4, answer, tol);
131 }
132 template <class From, class To>
133 void test_convert_neg_float(From const&, const boost::mpl::false_&)
134 {
135 }
136
137 template <class From, class To>
138 void test_convert_imp(boost::mpl::int_<number_kind_floating_point> const&, boost::mpl::int_<number_kind_floating_point> const&)
139 {
140 for(unsigned i = 0; i < 100; ++i)
141 {
142 From from = generate_random<From>();
143 To t1(from);
144 To t2 = from.template convert_to<To>();
145 To answer(from.str());
146 To tol = (std::max)(std::numeric_limits<To>::epsilon(), To(std::numeric_limits<From>::epsilon())) * 2;
147 BOOST_CHECK_CLOSE_FRACTION(t1, answer, tol);
148 BOOST_CHECK_CLOSE_FRACTION(t2, answer, tol);
149 test_convert_neg_float<From, To>(from, boost::mpl::bool_<std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed>());
150 }
151 }
152
153 template <class From, class To>
154 void test_convert()
155 {
156 test_convert_imp<From, To>(typename number_category<From>::type(), typename number_category<To>::type());
157 }
158
159
160 int main()
161 {
162 test_convert<mpf_float_50, cpp_int>();
163 test_convert<mpf_float_50, int128_t>();
164 test_convert<mpf_float_50, uint128_t>();
165
166
167 test_convert<mpf_float_50, cpp_rational>();
168
169 test_convert<mpf_float_50, cpp_dec_float_50>();
170
171 test_convert<mpf_float_50, mpz_int>();
172 test_convert<mpf_float_50, mpq_rational>();
173 #if defined(HAS_MPFR)
174 test_convert<mpf_float_50, mpfr_float_50>();
175 #endif
176 #if defined(HAS_MPFI)
177 test_convert<mpf_float_50, mpfi_float_50>();
178 #endif
179 #ifdef HAS_TOMMATH
180 test_convert<mpf_float_50, tom_int>();
181 test_convert<mpf_float_50, tom_rational>();
182 #endif
183 #ifdef HAS_FLOAT128
184 test_convert<mpf_float_50, float128>();
185 #endif
186 return boost::report_errors();
187 }
188
189 #else
190
191 int main() { return 0; }
192
193 #endif