• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 ///////////////////////////////////////////////////////////////
2 //  Copyright 2013 John Maddock. Distributed under the Boost
3 //  Software License, Version 1.0. (See accompanying file
4 //  LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
5 
6 //[logged_adaptor
7 
8 #include <boost/multiprecision/mpfi.hpp>
9 #include <boost/multiprecision/logged_adaptor.hpp>
10 #include <iostream>
11 #include <iomanip>
12 //
13 // Begin by overloading log_postfix_event so we can capture each arithmetic event as it happens:
14 //
15 namespace boost{ namespace multiprecision{
16 
17 template <unsigned D>
log_postfix_event(const mpfi_float_backend<D> & val,const char * event_description)18 inline void log_postfix_event(const mpfi_float_backend<D>& val, const char* event_description)
19 {
20    // Print out the (relative) diameter of the interval:
21    using namespace boost::multiprecision;
22    number<mpfr_float_backend<D> > diam;
23    mpfi_diam(diam.backend().data(), val.data());
24    std::cout << "Diameter was " << diam << " after operation: " << event_description << std::endl;
25 }
26 template <unsigned D, class T>
log_postfix_event(const mpfi_float_backend<D> &,const T &,const char * event_description)27 inline void log_postfix_event(const mpfi_float_backend<D>&, const T&, const char* event_description)
28 {
29    // This version is never called in this example.
30 }
31 
32 }}
33 
34 
main()35 int main()
36 {
37    using namespace boost::multiprecision;
38    typedef number<logged_adaptor<mpfi_float_backend<17> > > logged_type;
39    //
40    // Test case deliberately introduces cancellation error, relative size of interval
41    // gradually gets larger after each operation:
42    //
43    logged_type a = 1;
44    a /= 10;
45 
46    for(unsigned i = 0; i < 13; ++i)
47    {
48       logged_type b = a * 9;
49       b /= 10;
50       a -= b;
51    }
52    std::cout << "Final value was: " << a << std::endl;
53    return 0;
54 }
55 
56 //]
57 
58 /*
59 //[logged_adaptor_output
60 
61 Diameter was nan after operation: Default construct
62 Diameter was 0 after operation: Assignment from arithmetic type
63 Diameter was 4.33681e-18 after operation: /=
64 Diameter was nan after operation: Default construct
65 Diameter was 7.70988e-18 after operation: *
66 Diameter was 9.63735e-18 after operation: /=
67 Diameter was 1.30104e-16 after operation: -=
68 Diameter was nan after operation: Default construct
69 Diameter was 1.30104e-16 after operation: *
70 Diameter was 1.38537e-16 after operation: /=
71 Diameter was 2.54788e-15 after operation: -=
72 Diameter was nan after operation: Default construct
73 Diameter was 2.54788e-15 after operation: *
74 Diameter was 2.54863e-15 after operation: /=
75 Diameter was 4.84164e-14 after operation: -=
76 Diameter was nan after operation: Default construct
77 Diameter was 4.84164e-14 after operation: *
78 Diameter was 4.84221e-14 after operation: /=
79 Diameter was 9.19962e-13 after operation: -=
80 Diameter was nan after operation: Default construct
81 Diameter was 9.19962e-13 after operation: *
82 Diameter was 9.19966e-13 after operation: /=
83 Diameter was 1.74793e-11 after operation: -=
84 Diameter was nan after operation: Default construct
85 Diameter was 1.74793e-11 after operation: *
86 Diameter was 1.74793e-11 after operation: /=
87 Diameter was 3.32107e-10 after operation: -=
88 Diameter was nan after operation: Default construct
89 Diameter was 3.32107e-10 after operation: *
90 Diameter was 3.32107e-10 after operation: /=
91 Diameter was 6.31003e-09 after operation: -=
92 Diameter was nan after operation: Default construct
93 Diameter was 6.31003e-09 after operation: *
94 Diameter was 6.31003e-09 after operation: /=
95 Diameter was 1.19891e-07 after operation: -=
96 Diameter was nan after operation: Default construct
97 Diameter was 1.19891e-07 after operation: *
98 Diameter was 1.19891e-07 after operation: /=
99 Diameter was 2.27792e-06 after operation: -=
100 Diameter was nan after operation: Default construct
101 Diameter was 2.27792e-06 after operation: *
102 Diameter was 2.27792e-06 after operation: /=
103 Diameter was 4.32805e-05 after operation: -=
104 Diameter was nan after operation: Default construct
105 Diameter was 4.32805e-05 after operation: *
106 Diameter was 4.32805e-05 after operation: /=
107 Diameter was 0.00082233 after operation: -=
108 Diameter was nan after operation: Default construct
109 Diameter was 0.00082233 after operation: *
110 Diameter was 0.00082233 after operation: /=
111 Diameter was 0.0156243 after operation: -=
112 Diameter was nan after operation: Default construct
113 Diameter was 0.0156243 after operation: *
114 Diameter was 0.0156243 after operation: /=
115 Diameter was 0.296861 after operation: -=
116 Final value was: {8.51569e-15,1.14843e-14}
117 
118 //]
119 */
120