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