lcast_precision.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright Alexander Nasonov & Paul A. Bristow 2006.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED
  7. #define BOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED
  8. #include <climits>
  9. #include <ios>
  10. #include <limits>
  11. #include <boost/config.hpp>
  12. #include <boost/integer_traits.hpp>
  13. #ifndef BOOST_NO_IS_ABSTRACT
  14. // Fix for SF:1358600 - lexical_cast & pure virtual functions & VC 8 STL
  15. #include <boost/type_traits/conditional.hpp>
  16. #include <boost/type_traits/is_abstract.hpp>
  17. #endif
  18. #if defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) || \
  19. (defined(BOOST_MSVC) && (BOOST_MSVC<1310))
  20. #define BOOST_LCAST_NO_COMPILE_TIME_PRECISION
  21. #endif
  22. #ifdef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
  23. #include <boost/assert.hpp>
  24. #else
  25. #include <boost/static_assert.hpp>
  26. #endif
  27. namespace boost { namespace detail {
  28. class lcast_abstract_stub {};
  29. #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
  30. // Calculate an argument to pass to std::ios_base::precision from
  31. // lexical_cast. See alternative implementation for broken standard
  32. // libraries in lcast_get_precision below. Keep them in sync, please.
  33. template<class T>
  34. struct lcast_precision
  35. {
  36. #ifdef BOOST_NO_IS_ABSTRACT
  37. typedef std::numeric_limits<T> limits; // No fix for SF:1358600.
  38. #else
  39. typedef BOOST_DEDUCED_TYPENAME boost::conditional<
  40. boost::is_abstract<T>::value
  41. , std::numeric_limits<lcast_abstract_stub>
  42. , std::numeric_limits<T>
  43. >::type limits;
  44. #endif
  45. BOOST_STATIC_CONSTANT(bool, use_default_precision =
  46. !limits::is_specialized || limits::is_exact
  47. );
  48. BOOST_STATIC_CONSTANT(bool, is_specialized_bin =
  49. !use_default_precision &&
  50. limits::radix == 2 && limits::digits > 0
  51. );
  52. BOOST_STATIC_CONSTANT(bool, is_specialized_dec =
  53. !use_default_precision &&
  54. limits::radix == 10 && limits::digits10 > 0
  55. );
  56. BOOST_STATIC_CONSTANT(std::streamsize, streamsize_max =
  57. boost::integer_traits<std::streamsize>::const_max
  58. );
  59. BOOST_STATIC_CONSTANT(unsigned int, precision_dec = limits::digits10 + 1U);
  60. BOOST_STATIC_ASSERT(!is_specialized_dec ||
  61. precision_dec <= streamsize_max + 0UL
  62. );
  63. BOOST_STATIC_CONSTANT(unsigned long, precision_bin =
  64. 2UL + limits::digits * 30103UL / 100000UL
  65. );
  66. BOOST_STATIC_ASSERT(!is_specialized_bin ||
  67. (limits::digits + 0UL < ULONG_MAX / 30103UL &&
  68. precision_bin > limits::digits10 + 0UL &&
  69. precision_bin <= streamsize_max + 0UL)
  70. );
  71. BOOST_STATIC_CONSTANT(std::streamsize, value =
  72. is_specialized_bin ? precision_bin
  73. : is_specialized_dec ? precision_dec : 6
  74. );
  75. };
  76. #endif
  77. template<class T>
  78. inline std::streamsize lcast_get_precision(T* = 0)
  79. {
  80. #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
  81. return lcast_precision<T>::value;
  82. #else // Follow lcast_precision algorithm at run-time:
  83. #ifdef BOOST_NO_IS_ABSTRACT
  84. typedef std::numeric_limits<T> limits; // No fix for SF:1358600.
  85. #else
  86. typedef BOOST_DEDUCED_TYPENAME boost::conditional<
  87. boost::is_abstract<T>::value
  88. , std::numeric_limits<lcast_abstract_stub>
  89. , std::numeric_limits<T>
  90. >::type limits;
  91. #endif
  92. bool const use_default_precision =
  93. !limits::is_specialized || limits::is_exact;
  94. if(!use_default_precision)
  95. { // Includes all built-in floating-point types, float, double ...
  96. // and UDT types for which digits (significand bits) is defined (not zero)
  97. bool const is_specialized_bin =
  98. limits::radix == 2 && limits::digits > 0;
  99. bool const is_specialized_dec =
  100. limits::radix == 10 && limits::digits10 > 0;
  101. std::streamsize const streamsize_max =
  102. (boost::integer_traits<std::streamsize>::max)();
  103. (void)streamsize_max;
  104. if(is_specialized_bin)
  105. { // Floating-point types with
  106. // limits::digits defined by the specialization.
  107. unsigned long const digits = limits::digits;
  108. unsigned long const precision = 2UL + digits * 30103UL / 100000UL;
  109. // unsigned long is selected because it is at least 32-bits
  110. // and thus ULONG_MAX / 30103UL is big enough for all types.
  111. BOOST_ASSERT(
  112. digits < ULONG_MAX / 30103UL &&
  113. precision > limits::digits10 + 0UL &&
  114. precision <= streamsize_max + 0UL
  115. );
  116. return precision;
  117. }
  118. else if(is_specialized_dec)
  119. { // Decimal Floating-point type, most likely a User Defined Type
  120. // rather than a real floating-point hardware type.
  121. unsigned int const precision = limits::digits10 + 1U;
  122. BOOST_ASSERT(precision <= streamsize_max + 0UL);
  123. return precision;
  124. }
  125. }
  126. // Integral type (for which precision has no effect)
  127. // or type T for which limits is NOT specialized,
  128. // so assume stream precision remains the default 6 decimal digits.
  129. // Warning: if your User-defined Floating-point type T is NOT specialized,
  130. // then you may lose accuracy by only using 6 decimal digits.
  131. // To avoid this, you need to specialize T with either
  132. // radix == 2 and digits == the number of significand bits,
  133. // OR
  134. // radix = 10 and digits10 == the number of decimal digits.
  135. return 6;
  136. #endif
  137. }
  138. template<class T>
  139. inline void lcast_set_precision(std::ios_base& stream, T*)
  140. {
  141. stream.precision(lcast_get_precision<T>());
  142. }
  143. template<class Source, class Target>
  144. inline void lcast_set_precision(std::ios_base& stream, Source*, Target*)
  145. {
  146. std::streamsize const s = lcast_get_precision(static_cast<Source*>(0));
  147. std::streamsize const t = lcast_get_precision(static_cast<Target*>(0));
  148. stream.precision(s > t ? s : t);
  149. }
  150. }}
  151. #endif // BOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED