Elements  5.10
A C++ base framework for the Euclid Software.
FloatPrecisionExample.cpp
Go to the documentation of this file.
1 
21 #include <iostream> // for cout, endl
22 #include <iomanip> // for setprecision
23 #include <map> // for map
24 #include <string> // for string
25 #include <limits> // the templated version of the <cfloat> header
26  // it exposes numeric_limits
27 #include <cmath> // for nextafter
28 
29 #include <boost/program_options.hpp> // for program options from configuration file
30  // of command line arguments
31 #include <boost/format.hpp> // for format
32 #include <boost/math/constants/constants.hpp> // for pi
33 #include <boost/math/special_functions/next.hpp> // for float_next
34 
35 #include "ElementsKernel/ProgramHeaders.h" // for including all Program/related headers
36 #include "ElementsKernel/Unused.h" // for ELEMENTS_UNUSED
37 
38 using std::map;
39 using std::string;
40 using std::cout;
41 using std::endl;
42 
43 using boost::program_options::variable_value;
44 
45 
46 namespace Elements {
47 namespace Examples {
48 
49 constexpr int PRINT_PRECISION = 32;
50 
51 template<typename T>
52 void printTitle() {
53  cout << "================================================================================" << endl;
54  cout << "Float:" << endl;
55  cout << "--------------------------------------------------------------------------------" << endl;
56 }
57 
58 template<>
60  cout << "================================================================================" << endl;
61  cout << "Double:" << endl;
62  cout << "--------------------------------------------------------------------------------" << endl;
63 }
64 
65 template<typename T>
66 constexpr T Zero() {
67  return 0.0f;
68 }
69 
70 template<>
71 constexpr double Zero<double>() {
72  return 0.0;
73 }
74 
75 template<typename T>
76 constexpr T One() {
77  return 1.0f;
78 }
79 
80 template<>
81 constexpr double One<double>() {
82  return 1.0;
83 }
84 
85 template<typename T>
86 constexpr T Two() {
87  return 2.0f;
88 }
89 
90 template<>
91 constexpr double Two<double>() {
92  return 2.0;
93 }
94 
95 template<typename T>
96 constexpr T Seven() {
97  return 7.0f;
98 }
99 
100 template<>
101 constexpr double Seven<double>() {
102  return 7.0;
103 }
104 
105 template<typename T>
106 constexpr T Ten() {
107  return 10.0f;
108 }
109 
110 template<>
111 constexpr double Ten<double>() {
112  return 10.0;
113 }
114 
115 template<typename T>
117 
118  using std::numeric_limits;
119  using std::nextafter;
120  using std::nexttoward;
121  using boost::math::float_next;
123  using std::cos;
124  using std::pow;
125 
126  printTitle<T>();
127 
128  auto zero = Zero<T>();
129  auto zeroplus = float_next(zero);
130  auto nextafterzero = nextafter(zero, Two<T>());
131  auto nextzerotoward = nexttoward(zero, Two<T>());
132 
133  cout << "zero: " << zero << endl;
134  cout << "next to zero: " << zeroplus << endl;
135  cout << "next after zero: " << nextafterzero << endl;
136  cout << "next after zero toward two: " << nextzerotoward << endl;
137 
138  auto one = One<T>();
139  auto oneplus = float_next(one);
140  auto nextafterone = nextafter(one, Two<T>());
141  auto nextonetoward = nexttoward(one, Two<T>());
142  auto cospiover7 = cos(pi<T>()/Seven<T>());
143  auto default_test_tolerance = pow(Ten<T>(), -numeric_limits<T>::digits10);
144 
145  cout << "one: " << one << endl;
146  cout << "next to one: " << oneplus << endl;
147  cout << "next after one: " << nextafterone << endl;
148  cout << "next after one toward two: " << nextonetoward << endl;
149 
150  cout << "pi: " << pi<T>() << endl;
151  cout << "the Cosine of pi/7: " << cospiover7 << endl;
152  cout << "the default test tolerance: " << default_test_tolerance << endl;
153 
154  cout << "The mantissa digits: " << numeric_limits<T>::digits << endl;
155  cout << "The decimal digits: " << numeric_limits<T>::digits10 << endl;
156  cout << "The epsilon: " << numeric_limits<T>::epsilon() << endl;
157  cout << "The minimum exponent: " << numeric_limits<T>::min_exponent << endl;
158  cout << "The minimum decimal exponent: " << numeric_limits<T>::min_exponent10 << endl;
159  cout << "The maximum exponent: " << numeric_limits<float>::max_exponent << endl;
160  cout << "The maximum decimal exponent: " << numeric_limits<T>::max_exponent10 << endl;
161  cout << "The minimum: " << numeric_limits<T>::min() << endl;
162  cout << "The maximum: " << numeric_limits<T>::max() << endl;
163  cout << "The sizeof: " << sizeof(T) << endl;
164  cout << "The sizeof in bits: " << 8*sizeof(T) << endl;
165 
166 }
167 
169 
170 public:
171 
173 
174  cout << std::fixed
176 
177  printFloatPrecision<float>();
178 
179  printFloatPrecision<double>();
180 
181  return ExitCode::OK;
182 
183  }
184 
185 };
186 
187 } // namespace Examples
188 } // namespace Elements
189 
190 
constexpr double pi
Definition: MathConstants.h:33
ExitCode
Strongly typed exit numbers.
Definition: Exit.h:98
Macro to silence unused variables warnings from the compiler.
constexpr double Two< double >()
Everything is OK.
T endl(T... args)
constexpr double Ten< double >()
Abstract class for all Elements programs.
Definition: Program.h:51
STL class.
STL class.
#define MAIN_FOR(ELEMENTS_PROGRAM_NAME)
Definition: Main.h:117
constexpr double Zero< double >()
T cos(T... args)
T nextafter(T... args)
T fixed(T... args)
constexpr double Seven< double >()
T pow(T... args)
ExitCode mainMethod(ELEMENTS_UNUSED map< string, variable_value > &args) override
constexpr double One< double >()
#define ELEMENTS_UNUSED
Definition: Unused.h:39
T setprecision(T... args)