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