File: logged_adaptor.cpp

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (126 lines) | stat: -rw-r--r-- 4,693 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
///////////////////////////////////////////////////////////////
//  Copyright 2013 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

//[logged_adaptor

#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 {
namespace multiprecision {

template <unsigned D>
inline void log_postfix_event(const mpfi_float_backend<D>& val, const char* event_description)
{
   // Print out the (relative) diameter of the interval:
   using namespace boost::multiprecision;
   number<mpfr_float_backend<D> > diam;
   mpfi_diam(diam.backend().data(), val.data());
   std::cout << "Diameter was " << diam << " after operation: " << event_description << std::endl;
}
template <unsigned D, class T>
inline void log_postfix_event(const mpfi_float_backend<D>&, const T&, const char* event_description)
{
   // This version is never called in this example.
}

}
} // namespace boost::multiprecision
//
// Now we can include the actual multiprecision headers and make the types concrete:
//
#include <boost/multiprecision/mpfi.hpp>
#include <boost/multiprecision/logged_adaptor.hpp>

int main()
{
   using namespace boost::multiprecision;
   using logged_type = logged_adaptor_t<mpfi_float_50>;
   //
   // 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 0;
}

//]

/*
//[logged_adaptor_output

Diameter was -0 after operation: Default construct
Diameter was 0 after operation: Assignment from arithmetic type
Diameter was 3.34096e-51 after operation: /=
Diameter was -0 after operation: Default construct
Diameter was 5.93948e-51 after operation: *
Diameter was 7.42435e-51 after operation: /=
Diameter was 1.00229e-49 after operation: -=
Diameter was -0 after operation: Default construct
Diameter was 1.00229e-49 after operation: *
Diameter was 1.02085e-49 after operation: /=
Diameter was 1.92105e-48 after operation: -=
Diameter was -0 after operation: Default construct
Diameter was 1.92105e-48 after operation: *
Diameter was 1.92279e-48 after operation: /=
Diameter was 3.65156e-47 after operation: -=
Diameter was -0 after operation: Default construct
Diameter was 3.65156e-47 after operation: *
Diameter was 3.65163e-47 after operation: /=
Diameter was 6.93803e-46 after operation: -=
Diameter was -0 after operation: Default construct
Diameter was 6.93803e-46 after operation: *
Diameter was 6.93803e-46 after operation: /=
Diameter was 1.31823e-44 after operation: -=
Diameter was -0 after operation: Default construct
Diameter was 1.31823e-44 after operation: *
Diameter was 1.31823e-44 after operation: /=
Diameter was 2.50463e-43 after operation: -=
Diameter was -0 after operation: Default construct
Diameter was 2.50463e-43 after operation: *
Diameter was 2.50463e-43 after operation: /=
Diameter was 4.7588e-42 after operation: -=
Diameter was -0 after operation: Default construct
Diameter was 4.7588e-42 after operation: *
Diameter was 4.7588e-42 after operation: /=
Diameter was 9.04171e-41 after operation: -=
Diameter was -0 after operation: Default construct
Diameter was 9.04171e-41 after operation: *
Diameter was 9.04171e-41 after operation: /=
Diameter was 1.71793e-39 after operation: -=
Diameter was -0 after operation: Default construct
Diameter was 1.71793e-39 after operation: *
Diameter was 1.71793e-39 after operation: /=
Diameter was 3.26406e-38 after operation: -=
Diameter was -0 after operation: Default construct
Diameter was 3.26406e-38 after operation: *
Diameter was 3.26406e-38 after operation: /=
Diameter was 6.20171e-37 after operation: -=
Diameter was -0 after operation: Default construct
Diameter was 6.20171e-37 after operation: *
Diameter was 6.20171e-37 after operation: /=
Diameter was 1.17832e-35 after operation: -=
Diameter was -0 after operation: Default construct
Diameter was 1.17832e-35 after operation: *
Diameter was 1.17832e-35 after operation: /=
Diameter was 2.23882e-34 after operation: -=
Final value was: {1e-14,1e-14}
//]
*/