1 | // (C) Copyright John Maddock 2005. |
---|
2 | // Use, modification and distribution are subject to the |
---|
3 | // Boost Software License, Version 1.0. (See accompanying file |
---|
4 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
5 | |
---|
6 | // See http://www.boost.org/libs/config/test for most recent version. |
---|
7 | |
---|
8 | // |
---|
9 | // This test prints out informative information about <math.h>, <float.h> |
---|
10 | // and <limits>. Note that this file does require a correctly configured |
---|
11 | // Boost setup, and so can't be folded into config_info which is designed |
---|
12 | // to function without Boost.Confg support. Each test is documented in |
---|
13 | // more detail below. |
---|
14 | // |
---|
15 | |
---|
16 | #include <boost/limits.hpp> |
---|
17 | #include <limits.h> |
---|
18 | #include <math.h> |
---|
19 | #include <cmath> |
---|
20 | #include <float.h> |
---|
21 | #include <iostream> |
---|
22 | #include <iomanip> |
---|
23 | #include <cstring> |
---|
24 | #include <boost/type_traits/alignment_of.hpp> |
---|
25 | |
---|
26 | #ifdef BOOST_NO_STDC_NAMESPACE |
---|
27 | namespace std{ using ::strcmp; using ::pow; using ::fabs; using ::sqrt; using ::sin; using ::atan2; } |
---|
28 | #endif |
---|
29 | |
---|
30 | static unsigned int indent = 4; |
---|
31 | static unsigned int width = 40; |
---|
32 | |
---|
33 | void print_macro(const char* name, const char* value) |
---|
34 | { |
---|
35 | // if name == value+1 then then macro is not defined, |
---|
36 | // in which case we don't print anything: |
---|
37 | if(0 != std::strcmp(name, value+1)) |
---|
38 | { |
---|
39 | for(unsigned i = 0; i < indent; ++i) std::cout.put(' '); |
---|
40 | std::cout << std::setw(width); |
---|
41 | std::cout.setf(std::istream::left, std::istream::adjustfield); |
---|
42 | std::cout << name; |
---|
43 | if(value[1]) |
---|
44 | { |
---|
45 | // macro has a value: |
---|
46 | std::cout << value << "\n"; |
---|
47 | } |
---|
48 | else |
---|
49 | { |
---|
50 | // macro is defined but has no value: |
---|
51 | std::cout << " [no value]\n"; |
---|
52 | } |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | #define PRINT_MACRO(X) print_macro(#X, BOOST_STRINGIZE(=X)) |
---|
57 | |
---|
58 | template <class T> |
---|
59 | void print_expression(const char* expression, T val) |
---|
60 | { |
---|
61 | for(unsigned i = 0; i < indent; ++i) std::cout.put(' '); |
---|
62 | std::cout << std::setw(width); |
---|
63 | std::cout.setf(std::istream::left, std::istream::adjustfield); |
---|
64 | std::cout << std::setprecision(std::numeric_limits<T>::digits10+2); |
---|
65 | std::cout << expression << "=" << val << std::endl; |
---|
66 | } |
---|
67 | |
---|
68 | #define PRINT_EXPRESSION(E) print_expression(#E, E); |
---|
69 | |
---|
70 | |
---|
71 | template <class T> |
---|
72 | void print_limits(T, const char* name) |
---|
73 | { |
---|
74 | // |
---|
75 | // Output general information on numeric_limits, as well as |
---|
76 | // probing known and supected problems. |
---|
77 | // |
---|
78 | std::cout << |
---|
79 | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" |
---|
80 | "std::numeric_limits information for type " << name << std::endl; |
---|
81 | std::cout << |
---|
82 | " is_specialized = " << std::numeric_limits<T>::is_specialized << std::endl; |
---|
83 | std::cout << |
---|
84 | " min" "() = " << std::setprecision(std::numeric_limits<T>::digits10 + 2) << (std::numeric_limits<T>::min)() << std::endl; |
---|
85 | std::cout << |
---|
86 | " max" "() = " << std::setprecision(std::numeric_limits<T>::digits10 + 2) << (std::numeric_limits<T>::max)() << std::endl; |
---|
87 | std::cout << |
---|
88 | " digits = " << std::numeric_limits<T>::digits << std::endl; |
---|
89 | std::cout << |
---|
90 | " digits10 = " << std::numeric_limits<T>::digits10 << std::endl; |
---|
91 | std::cout << |
---|
92 | " is_signed = " << std::numeric_limits<T>::is_signed << std::endl; |
---|
93 | std::cout << |
---|
94 | " is_integer = " << std::numeric_limits<T>::is_integer << std::endl; |
---|
95 | std::cout << |
---|
96 | " is_exact = " << std::numeric_limits<T>::is_exact << std::endl; |
---|
97 | std::cout << |
---|
98 | " radix = " << std::numeric_limits<T>::radix << std::endl; |
---|
99 | |
---|
100 | std::cout << |
---|
101 | " epsilon() = " << std::setprecision(std::numeric_limits<T>::digits10 + 2) << (std::numeric_limits<T>::epsilon)() << std::endl; |
---|
102 | std::cout << |
---|
103 | " round_error() = " << std::setprecision(std::numeric_limits<T>::digits10 + 2) << (std::numeric_limits<T>::round_error)() << std::endl; |
---|
104 | |
---|
105 | std::cout << |
---|
106 | " min_exponent = " << std::numeric_limits<T>::min_exponent << std::endl; |
---|
107 | std::cout << |
---|
108 | " min_exponent10 = " << std::numeric_limits<T>::min_exponent10 << std::endl; |
---|
109 | std::cout << |
---|
110 | " max_exponent = " << std::numeric_limits<T>::max_exponent << std::endl; |
---|
111 | std::cout << |
---|
112 | " max_exponent10 = " << std::numeric_limits<T>::max_exponent10 << std::endl; |
---|
113 | std::cout << |
---|
114 | " has_infinity = " << std::numeric_limits<T>::has_infinity << std::endl; |
---|
115 | std::cout << |
---|
116 | " has_quiet_NaN = " << std::numeric_limits<T>::has_quiet_NaN << std::endl; |
---|
117 | std::cout << |
---|
118 | " has_signaling_NaN = " << std::numeric_limits<T>::has_signaling_NaN << std::endl; |
---|
119 | std::cout << |
---|
120 | " has_denorm = " << std::numeric_limits<T>::has_denorm << std::endl; |
---|
121 | std::cout << |
---|
122 | " has_denorm_loss = " << std::numeric_limits<T>::has_denorm_loss << std::endl; |
---|
123 | |
---|
124 | std::cout << |
---|
125 | " infinity() = " << std::setprecision(std::numeric_limits<T>::digits10 + 2) << (std::numeric_limits<T>::infinity)() << std::endl; |
---|
126 | std::cout << |
---|
127 | " quiet_NaN() = " << std::setprecision(std::numeric_limits<T>::digits10 + 2) << (std::numeric_limits<T>::quiet_NaN)() << std::endl; |
---|
128 | std::cout << |
---|
129 | " signaling_NaN() = " << std::setprecision(std::numeric_limits<T>::digits10 + 2) << (std::numeric_limits<T>::signaling_NaN)() << std::endl; |
---|
130 | std::cout << |
---|
131 | " denorm_min() = " << std::setprecision(std::numeric_limits<T>::digits10 + 2) << (std::numeric_limits<T>::denorm_min)() << std::endl; |
---|
132 | |
---|
133 | |
---|
134 | std::cout << |
---|
135 | " is_iec559 = " << std::numeric_limits<T>::is_iec559 << std::endl; |
---|
136 | std::cout << |
---|
137 | " is_bounded = " << std::numeric_limits<T>::is_bounded << std::endl; |
---|
138 | std::cout << |
---|
139 | " is_modulo = " << std::numeric_limits<T>::is_modulo << std::endl; |
---|
140 | std::cout << |
---|
141 | " traps = " << std::numeric_limits<T>::traps << std::endl; |
---|
142 | std::cout << |
---|
143 | " tinyness_before = " << std::numeric_limits<T>::tinyness_before << std::endl; |
---|
144 | std::cout << |
---|
145 | " round_style = " << std::numeric_limits<T>::round_style << std::endl << std::endl; |
---|
146 | |
---|
147 | if(std::numeric_limits<T>::is_exact == 0) |
---|
148 | { |
---|
149 | bool r = std::numeric_limits<T>::epsilon() == std::pow(static_cast<T>(std::numeric_limits<T>::radix), 1-std::numeric_limits<T>::digits); |
---|
150 | if(r) |
---|
151 | std::cout << "Epsilon has sane value of std::pow(std::numeric_limits<T>::radix, 1-std::numeric_limits<T>::digits)." << std::endl; |
---|
152 | else |
---|
153 | std::cout << "CAUTION: epsilon does not have a sane value." << std::endl; |
---|
154 | std::cout << std::endl; |
---|
155 | } |
---|
156 | std::cout << |
---|
157 | " sizeof(" << name << ") = " << sizeof(T) << std::endl; |
---|
158 | std::cout << |
---|
159 | " alignment_of<" << name << "> = " << boost::alignment_of<T>::value << std::endl << std::endl; |
---|
160 | } |
---|
161 | /* |
---|
162 | template <class T> |
---|
163 | bool is_same_type(T, T) |
---|
164 | { |
---|
165 | return true; |
---|
166 | }*/ |
---|
167 | bool is_same_type(float, float) |
---|
168 | { return true; } |
---|
169 | bool is_same_type(double, double) |
---|
170 | { return true; } |
---|
171 | bool is_same_type(long double, long double) |
---|
172 | { return true; } |
---|
173 | template <class T, class U> |
---|
174 | bool is_same_type(T, U) |
---|
175 | { |
---|
176 | return false; |
---|
177 | } |
---|
178 | |
---|
179 | // |
---|
180 | // We need this to test whether abs has been overloaded for |
---|
181 | // the floating point types or not: |
---|
182 | // |
---|
183 | namespace std{ |
---|
184 | #if !BOOST_WORKAROUND(BOOST_MSVC, == 1300) |
---|
185 | template <class T> |
---|
186 | char abs(T) |
---|
187 | { |
---|
188 | return ' '; |
---|
189 | } |
---|
190 | #endif |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | template <class T> |
---|
195 | void test_overloads(T, const char* name) |
---|
196 | { |
---|
197 | // |
---|
198 | // Probe known and suspected problems with the std lib Math functions. |
---|
199 | // |
---|
200 | std::cout << |
---|
201 | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" |
---|
202 | "Math function overload information for type " << name << std::endl; |
---|
203 | |
---|
204 | // |
---|
205 | // Are the math functions overloaded for type T, |
---|
206 | // or do we just get double versions? |
---|
207 | // |
---|
208 | bool r = is_same_type(std::fabs(T(0)), T(0)); |
---|
209 | r &= is_same_type(std::sqrt(T(0)), T(0)); |
---|
210 | r &= is_same_type(std::sin(T(0)), T(0)); |
---|
211 | if(r) |
---|
212 | std::cout << "The Math functions are overloaded for type " << name << std::endl; |
---|
213 | else |
---|
214 | std::cout << "CAUTION: The Math functions are NOT overloaded for type " << name << std::endl; |
---|
215 | |
---|
216 | // |
---|
217 | // Check that a few of the functions work OK, we do this because if these |
---|
218 | // are implemented as double precision internally then we can get |
---|
219 | // overflow or underflow when passing arguments of other types. |
---|
220 | // |
---|
221 | r = (std::fabs((std::numeric_limits<T>::max)()) == (std::numeric_limits<T>::max)()); |
---|
222 | r &= (std::fabs(-(std::numeric_limits<T>::max)()) == (std::numeric_limits<T>::max)()); |
---|
223 | r &= (std::fabs((std::numeric_limits<T>::min)()) == (std::numeric_limits<T>::min)()); |
---|
224 | r &= (std::fabs(-(std::numeric_limits<T>::min)()) == (std::numeric_limits<T>::min)()); |
---|
225 | if(r) |
---|
226 | std::cout << "std::fabs looks OK for type " << name << std::endl; |
---|
227 | else |
---|
228 | std::cout << "CAUTION: std::fabs is broken for type " << name << std::endl; |
---|
229 | |
---|
230 | // |
---|
231 | // abs not overloaded for real arguments with VC6 (and others?) |
---|
232 | // |
---|
233 | r = (std::abs((std::numeric_limits<T>::max)()) == (std::numeric_limits<T>::max)()); |
---|
234 | r &= (std::abs(-(std::numeric_limits<T>::max)()) == (std::numeric_limits<T>::max)()); |
---|
235 | r &= (std::abs((std::numeric_limits<T>::min)()) == (std::numeric_limits<T>::min)()); |
---|
236 | r &= (std::abs(-(std::numeric_limits<T>::min)()) == (std::numeric_limits<T>::min)()); |
---|
237 | if(r) |
---|
238 | std::cout << "std::abs looks OK for type " << name << std::endl; |
---|
239 | else |
---|
240 | std::cout << "CAUTION: std::abs is broken for type " << name << std::endl; |
---|
241 | |
---|
242 | // |
---|
243 | // std::sqrt on FreeBSD converts long double arguments to double leading to |
---|
244 | // overflow/underflow: |
---|
245 | // |
---|
246 | r = (std::sqrt((std::numeric_limits<T>::max)()) < (std::numeric_limits<T>::max)()); |
---|
247 | if(r) |
---|
248 | std::cout << "std::sqrt looks OK for type " << name << std::endl; |
---|
249 | else |
---|
250 | std::cout << "CAUTION: std::sqrt is broken for type " << name << std::endl; |
---|
251 | |
---|
252 | // |
---|
253 | // Sanity check for atan2: verify that it returns arguments in the correct |
---|
254 | // range and not just atan(x/y). |
---|
255 | // |
---|
256 | static const T half_pi = static_cast<T>(1.57079632679489661923132169163975144L); |
---|
257 | |
---|
258 | T val = std::atan2(T(-1), T(-1)); |
---|
259 | r = -half_pi > val; |
---|
260 | val = std::atan2(T(1), T(-1)); |
---|
261 | r &= half_pi < val; |
---|
262 | val = std::atan2(T(1), T(1)); |
---|
263 | r &= (val > 0) && (val < half_pi); |
---|
264 | val = std::atan2(T(-1), T(1)); |
---|
265 | r &= (val < 0) && (val > -half_pi); |
---|
266 | if(r) |
---|
267 | std::cout << "std::atan2 looks OK for type " << name << std::endl; |
---|
268 | else |
---|
269 | std::cout << "CAUTION: std::atan2 is broken for type " << name << std::endl; |
---|
270 | } |
---|
271 | |
---|
272 | |
---|
273 | |
---|
274 | int main() |
---|
275 | { |
---|
276 | // |
---|
277 | // Start by printing the values of the macros from float.h |
---|
278 | // |
---|
279 | std::cout << |
---|
280 | "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" |
---|
281 | "Macros from <math.h>" << std::endl; |
---|
282 | |
---|
283 | PRINT_EXPRESSION(HUGE_VAL); |
---|
284 | #ifdef HUGE_VALF |
---|
285 | PRINT_EXPRESSION(HUGE_VALF); |
---|
286 | #endif |
---|
287 | #ifdef HUGE_VALL |
---|
288 | PRINT_EXPRESSION(HUGE_VALL); |
---|
289 | #endif |
---|
290 | #ifdef INFINITY |
---|
291 | PRINT_EXPRESSION(INFINITY); |
---|
292 | #endif |
---|
293 | |
---|
294 | PRINT_MACRO(NAN); |
---|
295 | PRINT_MACRO(FP_INFINITE); |
---|
296 | PRINT_MACRO(FP_NAN); |
---|
297 | PRINT_MACRO(FP_NORMAL); |
---|
298 | PRINT_MACRO(FP_SUBNORMAL); |
---|
299 | PRINT_MACRO(FP_ZERO); |
---|
300 | PRINT_MACRO(FP_FAST_FMA); |
---|
301 | PRINT_MACRO(FP_FAST_FMAF); |
---|
302 | PRINT_MACRO(FP_FAST_FMAL); |
---|
303 | PRINT_MACRO(FP_ILOGB0); |
---|
304 | PRINT_MACRO(FP_ILOGBNAN); |
---|
305 | PRINT_MACRO(MATH_ERRNO); |
---|
306 | PRINT_MACRO(MATH_ERREXCEPT); |
---|
307 | |
---|
308 | PRINT_EXPRESSION(FLT_MIN_10_EXP); |
---|
309 | PRINT_EXPRESSION(FLT_DIG); |
---|
310 | PRINT_EXPRESSION(FLT_MIN_EXP); |
---|
311 | PRINT_EXPRESSION(FLT_EPSILON); |
---|
312 | PRINT_EXPRESSION(FLT_RADIX); |
---|
313 | PRINT_EXPRESSION(FLT_MANT_DIG); |
---|
314 | PRINT_EXPRESSION(FLT_ROUNDS); |
---|
315 | PRINT_EXPRESSION(FLT_MAX); |
---|
316 | PRINT_EXPRESSION(FLT_MAX_10_EXP); |
---|
317 | PRINT_EXPRESSION(FLT_MAX_EXP); |
---|
318 | PRINT_EXPRESSION(FLT_MIN); |
---|
319 | PRINT_EXPRESSION(DBL_DIG); |
---|
320 | PRINT_EXPRESSION(DBL_MIN_EXP); |
---|
321 | PRINT_EXPRESSION(DBL_EPSILON); |
---|
322 | PRINT_EXPRESSION(DBL_MANT_DIG); |
---|
323 | PRINT_EXPRESSION(DBL_MAX); |
---|
324 | PRINT_EXPRESSION(DBL_MIN); |
---|
325 | PRINT_EXPRESSION(DBL_MAX_10_EXP); |
---|
326 | PRINT_EXPRESSION(DBL_MAX_EXP); |
---|
327 | PRINT_EXPRESSION(DBL_MIN_10_EXP); |
---|
328 | PRINT_EXPRESSION(LDBL_MAX_10_EXP); |
---|
329 | PRINT_EXPRESSION(LDBL_MAX_EXP); |
---|
330 | PRINT_EXPRESSION(LDBL_MIN); |
---|
331 | PRINT_EXPRESSION(LDBL_MIN_10_EXP); |
---|
332 | PRINT_EXPRESSION(LDBL_DIG); |
---|
333 | PRINT_EXPRESSION(LDBL_MIN_EXP); |
---|
334 | PRINT_EXPRESSION(LDBL_EPSILON); |
---|
335 | PRINT_EXPRESSION(LDBL_MANT_DIG); |
---|
336 | PRINT_EXPRESSION(LDBL_MAX); |
---|
337 | |
---|
338 | std::cout << std::endl; |
---|
339 | |
---|
340 | // |
---|
341 | // print out numeric_limits info: |
---|
342 | // |
---|
343 | print_limits(float(0), "float"); |
---|
344 | print_limits(double(0), "double"); |
---|
345 | print_limits((long double)(0), "long double"); |
---|
346 | |
---|
347 | // |
---|
348 | // print out function overload information: |
---|
349 | // |
---|
350 | test_overloads(float(0), "float"); |
---|
351 | test_overloads(double(0), "double"); |
---|
352 | test_overloads((long double)(0), "long double"); |
---|
353 | return 0; |
---|
354 | } |
---|
355 | |
---|
356 | |
---|
357 | |
---|
358 | |
---|