Elements  6.2
A C++ base framework for the Euclid Software.
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 
36 using std::cout;
37 using std::endl;
38 using std::map;
39 using std::string;
40 
41 namespace Elements {
42 namespace Examples {
43 
44 constexpr int PRINT_PRECISION = 32;
45 
46 template <typename T>
47 void printTitle() {
48  cout << "================================================================================" << endl;
49  cout << "Float:" << endl;
50  cout << "--------------------------------------------------------------------------------" << endl;
51 }
52 
53 template <>
55  cout << "================================================================================" << endl;
56  cout << "Double:" << endl;
57  cout << "--------------------------------------------------------------------------------" << endl;
58 }
59 
60 template <typename T>
61 constexpr T Zero() {
62  return 0.0f;
63 }
64 
65 template <>
66 constexpr double Zero<double>() {
67  return 0.0;
68 }
69 
70 template <typename T>
71 constexpr T One() {
72  return 1.0f;
73 }
74 
75 template <>
76 constexpr double One<double>() {
77  return 1.0;
78 }
79 
80 template <typename T>
81 constexpr T Two() {
82  return 2.0f;
83 }
84 
85 template <>
86 constexpr double Two<double>() {
87  return 2.0;
88 }
89 
90 template <typename T>
91 constexpr T Seven() {
92  return 7.0f;
93 }
94 
95 template <>
96 constexpr double Seven<double>() {
97  return 7.0;
98 }
99 
100 template <typename T>
101 constexpr T Ten() {
102  return 10.0f;
103 }
104 
105 template <>
106 constexpr double Ten<double>() {
107  return 10.0;
108 }
109 
110 template <typename T>
112 
113  using boost::math::float_next;
115  using std::cos;
116  using std::nextafter;
117  using std::nexttoward;
118  using std::numeric_limits;
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 
162 class FloatPrecision : public Program {
163 
164 public:
166 
168 
169  printFloatPrecision<float>();
170 
171  printFloatPrecision<double>();
172 
173  return ExitCode::OK;
174  }
175 };
176 
177 } // namespace Examples
178 } // namespace Elements
179 
Elements::Examples::Zero
constexpr T Zero()
Definition: FloatPrecision.cpp:61
Elements::Examples::FloatPrecision
Definition: FloatPrecision.cpp:162
std::setprecision
T setprecision(T... args)
Elements::ExitCode::OK
@ OK
Everything is OK.
std::string
STL class.
Elements::Examples::Two< double >
constexpr double Two< double >()
Definition: FloatPrecision.cpp:86
Elements::Examples::Seven
constexpr T Seven()
Definition: FloatPrecision.cpp:91
std::cos
T cos(T... args)
Elements::Examples::FloatPrecision::mainMethod
ExitCode mainMethod(ELEMENTS_UNUSED map< string, VariableValue > &args) override
Definition: FloatPrecision.cpp:165
Elements::Examples::One
constexpr T One()
Definition: FloatPrecision.cpp:71
Elements::Examples::Zero< double >
constexpr double Zero< double >()
Definition: FloatPrecision.cpp:66
Elements::Kernel::Units::pi
constexpr double pi
Definition: MathConstants.h:34
Elements::ExitCode
ExitCode
Strongly typed exit numbers.
Definition: Exit.h:97
Elements::Examples::Program
Simple example of an Elements program.
Definition: Program.cpp:79
std::nextafter
T nextafter(T... args)
std::cout
Elements::Examples::Seven< double >
constexpr double Seven< double >()
Definition: FloatPrecision.cpp:96
Elements::Examples::printTitle< double >
void printTitle< double >()
Definition: FloatPrecision.cpp:54
std::map
STL class.
Elements::Examples::printTitle
void printTitle()
Definition: FloatPrecision.cpp:47
ProgramHeaders.h
Elements::Examples::Ten
constexpr T Ten()
Definition: FloatPrecision.cpp:101
std::endl
T endl(T... args)
Elements::Examples::Ten< double >
constexpr double Ten< double >()
Definition: FloatPrecision.cpp:106
Elements::Examples::Two
constexpr T Two()
Definition: FloatPrecision.cpp:81
std::fixed
T fixed(T... args)
MAIN_FOR
#define MAIN_FOR(ELEMENTS_PROGRAM_NAME)
Definition: Main.h:113
Elements::Examples::One< double >
constexpr double One< double >()
Definition: FloatPrecision.cpp:76
Elements::Examples::printFloatPrecision
void printFloatPrecision()
Definition: FloatPrecision.cpp:111
Unused.h
Macro to silence unused variables warnings from the compiler.
Elements::Examples::PRINT_PRECISION
constexpr int PRINT_PRECISION
Definition: FloatPrecision.cpp:44
std::numeric_limits
ELEMENTS_UNUSED
#define ELEMENTS_UNUSED
Definition: Unused.h:39
Elements
Definition: callBackExample.h:35
std::pow
T pow(T... args)