math_info.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. !defined(_LIBCPP_VERSION)
  168. template <class T>
  169. char abs(T)
  170. {
  171. return ' ';
  172. }
  173. #endif
  174. }
  175. template <class T>
  176. void test_overloads(T, const char* name)
  177. {
  178. //
  179. // Probe known and suspected problems with the std lib Math functions.
  180. //
  181. std::cout <<
  182. "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
  183. "Math function overload information for type " << name << std::endl;
  184. //
  185. // Are the math functions overloaded for type T,
  186. // or do we just get double versions?
  187. //
  188. bool r = is_same_type(std::fabs(T(0)), T(0));
  189. r &= is_same_type(std::sqrt(T(0)), T(0));
  190. r &= is_same_type(std::sin(T(0)), T(0));
  191. if(r)
  192. std::cout << "The Math functions are overloaded for type " << name << std::endl;
  193. else
  194. std::cout << "CAUTION: The Math functions are NOT overloaded for type " << name << std::endl;
  195. //
  196. // Check that a few of the functions work OK, we do this because if these
  197. // are implemented as double precision internally then we can get
  198. // overflow or underflow when passing arguments of other types.
  199. //
  200. r = (std::fabs((std::numeric_limits<T>::max)()) == (std::numeric_limits<T>::max)());
  201. r &= (std::fabs(-(std::numeric_limits<T>::max)()) == (std::numeric_limits<T>::max)());
  202. r &= (std::fabs((std::numeric_limits<T>::min)()) == (std::numeric_limits<T>::min)());
  203. r &= (std::fabs(-(std::numeric_limits<T>::min)()) == (std::numeric_limits<T>::min)());
  204. if(r)
  205. std::cout << "std::fabs looks OK for type " << name << std::endl;
  206. else
  207. std::cout << "CAUTION: std::fabs is broken for type " << name << std::endl;
  208. //
  209. // abs not overloaded for real arguments with VC6 (and others?)
  210. //
  211. r = (std::abs((std::numeric_limits<T>::max)()) == (std::numeric_limits<T>::max)());
  212. r &= (std::abs(-(std::numeric_limits<T>::max)()) == (std::numeric_limits<T>::max)());
  213. r &= (std::abs((std::numeric_limits<T>::min)()) == (std::numeric_limits<T>::min)());
  214. r &= (std::abs(-(std::numeric_limits<T>::min)()) == (std::numeric_limits<T>::min)());
  215. if(r)
  216. std::cout << "std::abs looks OK for type " << name << std::endl;
  217. else
  218. std::cout << "CAUTION: std::abs is broken for type " << name << std::endl;
  219. //
  220. // std::sqrt on FreeBSD converts long double arguments to double leading to
  221. // overflow/underflow:
  222. //
  223. r = (std::sqrt((std::numeric_limits<T>::max)()) < (std::numeric_limits<T>::max)());
  224. if(r)
  225. std::cout << "std::sqrt looks OK for type " << name << std::endl;
  226. else
  227. std::cout << "CAUTION: std::sqrt is broken for type " << name << std::endl;
  228. //
  229. // Sanity check for atan2: verify that it returns arguments in the correct
  230. // range and not just atan(x/y).
  231. //
  232. static const T half_pi = static_cast<T>(1.57079632679489661923132169163975144L);
  233. T val = std::atan2(T(-1), T(-1));
  234. r = -half_pi > val;
  235. val = std::atan2(T(1), T(-1));
  236. r &= half_pi < val;
  237. val = std::atan2(T(1), T(1));
  238. r &= (val > 0) && (val < half_pi);
  239. val = std::atan2(T(-1), T(1));
  240. r &= (val < 0) && (val > -half_pi);
  241. if(r)
  242. std::cout << "std::atan2 looks OK for type " << name << std::endl;
  243. else
  244. std::cout << "CAUTION: std::atan2 is broken for type " << name << std::endl;
  245. }
  246. int main()
  247. {
  248. //
  249. // Start by printing the values of the macros from float.h
  250. //
  251. std::cout <<
  252. "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
  253. "Macros from <math.h>" << std::endl;
  254. #ifdef __BORLANDC__
  255. // Turn off hardware exceptions so we don't just abort
  256. // when calling numeric_limits members.
  257. _control87(MCW_EM,MCW_EM);
  258. #endif
  259. PRINT_EXPRESSION(HUGE_VAL);
  260. #ifdef HUGE_VALF
  261. PRINT_EXPRESSION(HUGE_VALF);
  262. #endif
  263. #ifdef HUGE_VALL
  264. PRINT_EXPRESSION(HUGE_VALL);
  265. #endif
  266. #ifdef INFINITY
  267. PRINT_EXPRESSION(INFINITY);
  268. #endif
  269. PRINT_MACRO(NAN);
  270. PRINT_MACRO(FP_INFINITE);
  271. PRINT_MACRO(FP_NAN);
  272. PRINT_MACRO(FP_NORMAL);
  273. PRINT_MACRO(FP_SUBNORMAL);
  274. PRINT_MACRO(FP_ZERO);
  275. PRINT_MACRO(FP_FAST_FMA);
  276. PRINT_MACRO(FP_FAST_FMAF);
  277. PRINT_MACRO(FP_FAST_FMAL);
  278. PRINT_MACRO(FP_ILOGB0);
  279. PRINT_MACRO(FP_ILOGBNAN);
  280. PRINT_MACRO(MATH_ERRNO);
  281. PRINT_MACRO(MATH_ERREXCEPT);
  282. PRINT_EXPRESSION(FLT_MIN_10_EXP);
  283. PRINT_EXPRESSION(FLT_DIG);
  284. PRINT_EXPRESSION(FLT_MIN_EXP);
  285. PRINT_EXPRESSION(FLT_EPSILON);
  286. PRINT_EXPRESSION(FLT_RADIX);
  287. PRINT_EXPRESSION(FLT_MANT_DIG);
  288. PRINT_EXPRESSION(FLT_ROUNDS);
  289. PRINT_EXPRESSION(FLT_MAX);
  290. PRINT_EXPRESSION(FLT_MAX_10_EXP);
  291. PRINT_EXPRESSION(FLT_MAX_EXP);
  292. PRINT_EXPRESSION(FLT_MIN);
  293. PRINT_EXPRESSION(DBL_DIG);
  294. PRINT_EXPRESSION(DBL_MIN_EXP);
  295. PRINT_EXPRESSION(DBL_EPSILON);
  296. PRINT_EXPRESSION(DBL_MANT_DIG);
  297. PRINT_EXPRESSION(DBL_MAX);
  298. PRINT_EXPRESSION(DBL_MIN);
  299. PRINT_EXPRESSION(DBL_MAX_10_EXP);
  300. PRINT_EXPRESSION(DBL_MAX_EXP);
  301. PRINT_EXPRESSION(DBL_MIN_10_EXP);
  302. PRINT_EXPRESSION(LDBL_MAX_10_EXP);
  303. PRINT_EXPRESSION(LDBL_MAX_EXP);
  304. PRINT_EXPRESSION(LDBL_MIN);
  305. PRINT_EXPRESSION(LDBL_MIN_10_EXP);
  306. PRINT_EXPRESSION(LDBL_DIG);
  307. PRINT_EXPRESSION(LDBL_MIN_EXP);
  308. PRINT_EXPRESSION(LDBL_EPSILON);
  309. PRINT_EXPRESSION(LDBL_MANT_DIG);
  310. PRINT_EXPRESSION(LDBL_MAX);
  311. std::cout << std::endl;
  312. //
  313. // print out numeric_limits info:
  314. //
  315. print_limits(float(0), "float");
  316. print_limits(double(0), "double");
  317. print_limits((long double)(0), "long double");
  318. //
  319. // print out function overload information:
  320. //
  321. test_overloads(float(0), "float");
  322. test_overloads(double(0), "double");
  323. test_overloads((long double)(0), "long double");
  324. return 0;
  325. }