File: logged_adaptor_cpp_output_test.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (56 lines) | stat: -rw-r--r-- 2,016 bytes parent folder | download | duplicates (8)
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
///////////////////////////////////////////////////////////////
//  Copyright 2023 John Maddock. Distributed under the Boost
//  Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt

#include <iostream>
#include <iomanip>
#include <boost/multiprecision/fwd.hpp>
//
// Begin by overloading log_postfix_event so we can capture each arithmetic event as it happens,
// unfortunately this must occur BEFORE we include the full header, so just include the forward
// declarations and define our overloads for now.  Note that in some cases we may need to just
// declare the overloads here, and define them once the types become concrete:
//
namespace boost::multiprecision {
      void log_postfix_event(const cpp_bin_float<113, backends::digit_base_2, void, std::int16_t, -16382, 16383>& val, const char* event_description);
}
//
// Now we can include the actual multiprecision headers and make the types concrete:
//
#include <boost/multiprecision/cpp_bin_float.hpp>
#include <boost/multiprecision/logged_adaptor.hpp>

namespace boost::multiprecision {

      bool event_has_been_called = false;

      inline void log_postfix_event(const cpp_bin_float<113, backends::digit_base_2, void, std::int16_t, -16382, 16383>& val, const char* event_description)
      {
         // Print out the (relative) diameter of the interval:
         using namespace boost::multiprecision;
         event_has_been_called = true;
      }
}

int main()
{
   using namespace boost::multiprecision;
   using logged_type = logged_adaptor_t<cpp_bin_float_quad>;
   //
   // Test case deliberately introduces cancellation error, relative size of interval
   // gradually gets larger after each operation:
   //
   logged_type a = 1;
   a /= 10;

   for (unsigned i = 0; i < 13; ++i)
   {
      logged_type b = a * 9;
      b /= 10;
      a -= b;
   }
   std::cout << "Final value was: " << a << std::endl;
   return boost::multiprecision::event_has_been_called ? 0 : -1;
}