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