PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Libraries/Boost1.40/libs/config/test/math_info.cpp

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