File: issue_13301.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (77 lines) | stat: -rw-r--r-- 2,073 bytes parent folder | download | duplicates (7)
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
///////////////////////////////////////////////////////////////////////////////
//  Copyright 2016 John Maddock. Distributed under 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 <boost/multiprecision/cpp_bin_float.hpp>

int main()
{
   typedef boost::multiprecision::number<boost::multiprecision::cpp_bin_float<8, boost::multiprecision::backends::digit_base_2> > quarter_float;

   quarter_float qf(256);

   unsigned int ui = qf.convert_to<unsigned int>();
   if (ui != 256)
      return 1;

   std::uintmax_t m(1), n;
   m <<= std::numeric_limits<std::uintmax_t>::digits - 1;
   qf = m;
   n  = qf.convert_to<std::uintmax_t>();
   if (m != n)
      return 2;
   qf *= 2;
   n = qf.convert_to<std::uintmax_t>();
   m = (std::numeric_limits<std::uintmax_t>::max)();
   if (m != n)
      return 3;

   qf     = 256;
   int si = qf.convert_to<int>();
   if (si != 256)
      return 4;
   std::intmax_t sm(1), sn;
   sm <<= std::numeric_limits<std::intmax_t>::digits - 1;
   qf = sm;
   sn = qf.convert_to<std::intmax_t>();
   if (sm != sn)
      return 5;
   qf *= 2;
   sn = qf.convert_to<std::intmax_t>();
   sm = (std::numeric_limits<std::intmax_t>::max)();
   if (sm != sn)
      return 6;

   // Again with negative numbers:
   qf = -256;
   si = qf.convert_to<int>();
   if (si != -256)
      return 7;
   sm = 1;
   sm <<= std::numeric_limits<std::intmax_t>::digits - 1;
   sm = -sm;
   qf = sm;
   sn = qf.convert_to<std::intmax_t>();
   if (sm != sn)
      return 8;
   qf *= 2;
   sn = qf.convert_to<std::intmax_t>();
   sm = (std::numeric_limits<std::intmax_t>::min)();
   if (sm != sn)
      return 9;

   // Now try conversion to cpp_int:
   qf                               = 256;
   boost::multiprecision::cpp_int i = qf.convert_to<boost::multiprecision::cpp_int>(), j;
   if (i != 256)
      return 10;
   qf = ldexp(qf, 126);
   i  = qf.convert_to<boost::multiprecision::cpp_int>();
   j  = 256;
   j <<= 126;
   if (i != j)
      return 11;

   return 0;
}