logged_adaptor.cpp 4.2 KB

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