Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / multiprecision / test / issue_13301.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 //  Copyright 2016 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_0.txt)
5
6 #include <boost/multiprecision/cpp_bin_float.hpp>
7
8 int main()
9 {
10    typedef boost::multiprecision::number<boost::multiprecision::cpp_bin_float<8, boost::multiprecision::backends::digit_base_2> > quarter_float;
11
12    quarter_float qf(256);
13
14    unsigned int ui = qf.convert_to<unsigned int>();
15    if (ui != 256)
16       return 1;
17
18    boost::uintmax_t m(1), n;
19    m <<= std::numeric_limits<boost::uintmax_t>::digits - 1;
20    qf = m;
21    n  = qf.convert_to<boost::uintmax_t>();
22    if (m != n)
23       return 2;
24    qf *= 2;
25    n = qf.convert_to<boost::uintmax_t>();
26    m = (std::numeric_limits<boost::uintmax_t>::max)();
27    if (m != n)
28       return 3;
29
30    qf     = 256;
31    int si = qf.convert_to<int>();
32    if (si != 256)
33       return 4;
34    boost::intmax_t sm(1), sn;
35    sm <<= std::numeric_limits<boost::intmax_t>::digits - 1;
36    qf = sm;
37    sn = qf.convert_to<boost::intmax_t>();
38    if (sm != sn)
39       return 5;
40    qf *= 2;
41    sn = qf.convert_to<boost::intmax_t>();
42    sm = (std::numeric_limits<boost::intmax_t>::max)();
43    if (sm != sn)
44       return 6;
45
46    // Again with negative numbers:
47    qf = -256;
48    si = qf.convert_to<int>();
49    if (si != -256)
50       return 7;
51    sm = 1;
52    sm <<= std::numeric_limits<boost::intmax_t>::digits - 1;
53    sm = -sm;
54    qf = sm;
55    sn = qf.convert_to<boost::intmax_t>();
56    if (sm != sn)
57       return 8;
58    qf *= 2;
59    sn = qf.convert_to<boost::intmax_t>();
60    sm = (std::numeric_limits<boost::intmax_t>::min)();
61    if (sm != sn)
62       return 9;
63
64    // Now try conversion to cpp_int:
65    qf                               = 256;
66    boost::multiprecision::cpp_int i = qf.convert_to<boost::multiprecision::cpp_int>(), j;
67    if (i != 256)
68       return 10;
69    qf = ldexp(qf, 126);
70    i  = qf.convert_to<boost::multiprecision::cpp_int>();
71    j  = 256;
72    j <<= 126;
73    if (i != j)
74       return 11;
75
76    return 0;
77 }