test_value.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright Paul A. Bristow 2017.
  2. // Copyright John Maddock 2017.
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. // test_value.hpp
  8. #ifndef TEST_VALUE_HPP
  9. #define TEST_VALUE_HPP
  10. // BOOST_MATH_TEST_VALUE is used to create a test value of suitable type from a decimal digit string.
  11. // Two parameters, both a floating-point literal double like 1.23 (not long double so no suffix L)
  12. // and a decimal digit string const char* like "1.23" must be provided.
  13. // The decimal value represented must be the same of course, with at least enough precision for long double.
  14. // Note there are two gotchas to this approach:
  15. // * You need all values to be real floating-point values
  16. // * and *MUST* include a decimal point (to avoid confusion with an integer literal).
  17. // * It's slow to compile compared to a simple literal.
  18. // Speed is not an issue for a few test values,
  19. // but it's not generally usable in large tables
  20. // where you really need everything to be statically initialized.
  21. // Macro BOOST_MATH_INSTRUMENT_CREATE_TEST_VALUE provides a global diagnostic value for create_type.
  22. #include <boost/cstdfloat.hpp> // For float_64_t, float128_t. Must be first include!
  23. #include <boost/lexical_cast.hpp>
  24. #include <boost/type_traits/is_constructible.hpp>
  25. #include <boost/type_traits/is_convertible.hpp>
  26. #ifdef BOOST_MATH_INSTRUMENT_CREATE_TEST_VALUE
  27. // global int create_type(0); must be defined before including this file.
  28. #endif
  29. #ifdef BOOST_HAS_FLOAT128
  30. typedef __float128 largest_float;
  31. #define BOOST_MATH_TEST_LARGEST_FLOAT_SUFFIX(x) x##Q
  32. #define BOOST_MATH_TEST_LARGEST_FLOAT_DIGITS std::numeric_limits<boost::multiprecision::float128>::digits
  33. #else
  34. typedef long double largest_float;
  35. #define BOOST_MATH_TEST_LARGEST_FLOAT_SUFFIX(x) x##L
  36. #define BOOST_MATH_TEST_LARGEST_FLOAT_DIGITS std::numeric_limits<long double>::digits
  37. #endif
  38. template <class T, class T2>
  39. inline T create_test_value(largest_float val, const char*, const boost::mpl::true_&, const T2&)
  40. { // Construct from long double or quad parameter val (ignoring string/const char* str).
  41. // (This is case for MPL parameters = true_ and T2 == false_,
  42. // and MPL parameters = true_ and T2 == true_ cpp_bin_float)
  43. // All built-in/fundamental floating-point types,
  44. // and other User-Defined Types that can be constructed without loss of precision
  45. // from long double suffix L (or quad suffix Q),
  46. //
  47. // Choose this method, even if can be constructed from a string,
  48. // because it will be faster, and more likely to be the closest representation.
  49. // (This is case for MPL parameters = mpl::true_ and T2 == mpl::true_).
  50. #ifdef BOOST_MATH_INSTRUMENT_CREATE_TEST_VALUE
  51. create_type = 1;
  52. #endif
  53. return static_cast<T>(val);
  54. }
  55. template <class T>
  56. inline T create_test_value(largest_float, const char* str, const boost::mpl::false_&, const boost::mpl::true_&)
  57. { // Construct from decimal digit string const char* @c str (ignoring long double parameter).
  58. // For example, extended precision or other User-Defined types which ARE constructible from a string
  59. // (but not from double, or long double without loss of precision).
  60. // (This is case for MPL parameters = mpl::false_ and T2 == mpl::true_).
  61. #ifdef BOOST_MATH_INSTRUMENT_CREATE_TEST_VALUE
  62. create_type = 2;
  63. #endif
  64. return T(str);
  65. }
  66. template <class T>
  67. inline T create_test_value(largest_float, const char* str, const boost::mpl::false_&, const boost::mpl::false_&)
  68. { // Create test value using from lexical cast of decimal digit string const char* str.
  69. // For example, extended precision or other User-Defined types which are NOT constructible from a string
  70. // (NOR constructible from a long double).
  71. // (This is case T1 = mpl::false and T2 == mpl::false).
  72. #ifdef BOOST_MATH_INSTRUMENT_CREATE_TEST_VALUE
  73. create_type = 3;
  74. #endif
  75. return boost::lexical_cast<T>(str);
  76. }
  77. // T real type, x a decimal digits representation of a floating-point, for example: 12.34.
  78. // It must include a decimal point (or it would be interpreted as an integer).
  79. // x is converted to a long double by appending the letter L (to suit long double fundamental type), 12.34L.
  80. // x is also passed as a const char* or string representation "12.34"
  81. // (to suit most other types that cannot be constructed from long double without possible loss).
  82. // BOOST_MATH_TEST_LARGEST_FLOAT_SUFFIX(x) makes a long double or quad version, with
  83. // suffix a letter L (or Q) to suit long double (or quad) fundamental type, 12.34L or 12.34Q.
  84. // #x makes a decimal digit string version to suit multiprecision and fixed_point constructors, "12.34".
  85. // (Constructing from double or long double (or quad) could lose precision for multiprecision or fixed-point).
  86. // The matching create_test_value function above is chosen depending on the T1 and T2 mpl bool truths.
  87. // The string version from #x is used if the precision of T is greater than long double.
  88. // Example: long double test_value = BOOST_MATH_TEST_VALUE(double, 1.23456789);
  89. #define BOOST_MATH_TEST_VALUE(T, x) create_test_value<T>(\
  90. BOOST_MATH_TEST_LARGEST_FLOAT_SUFFIX(x),\
  91. #x,\
  92. boost::mpl::bool_<\
  93. std::numeric_limits<T>::is_specialized &&\
  94. (std::numeric_limits<T>::radix == 2)\
  95. && (std::numeric_limits<T>::digits <= BOOST_MATH_TEST_LARGEST_FLOAT_DIGITS)\
  96. && boost::is_convertible<largest_float, T>::value>(),\
  97. boost::mpl::bool_<\
  98. boost::is_constructible<T, const char*>::value>()\
  99. )
  100. #endif // TEST_VALUE_HPP