real_concept.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. // Copyright John Maddock 2006.
  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. // Test real concept.
  6. // real_concept is an archetype for User defined Real types.
  7. // This file defines the features, constructors, operators, functions...
  8. // that are essential to use mathematical and statistical functions.
  9. // The template typename "RealType" is used where this type
  10. // (as well as the normal built-in types, float, double & long double)
  11. // can be used.
  12. // That this is the minimum set is confirmed by use as a type
  13. // in tests of all functions & distributions, for example:
  14. // test_spots(0.F); & test_spots(0.); for float and double, but also
  15. // test_spots(boost::math::concepts::real_concept(0.));
  16. // NTL quad_float type is an example of a type meeting the requirements,
  17. // but note minor additions are needed - see ntl.diff and documentation
  18. // "Using With NTL - a High-Precision Floating-Point Library".
  19. #ifndef BOOST_MATH_REAL_CONCEPT_HPP
  20. #define BOOST_MATH_REAL_CONCEPT_HPP
  21. #include <boost/math/special_functions/round.hpp>
  22. #include <boost/math/special_functions/trunc.hpp>
  23. #include <boost/math/special_functions/modf.hpp>
  24. #include <boost/math/tools/big_constant.hpp>
  25. #include <boost/math/tools/precision.hpp>
  26. #include <boost/math/tools/config.hpp>
  27. #include <boost/math/policies/policy.hpp>
  28. #include <boost/math/special_functions/asinh.hpp>
  29. #include <boost/math/special_functions/atanh.hpp>
  30. #if defined(__SGI_STL_PORT)
  31. # include <boost/math/tools/real_cast.hpp>
  32. #endif
  33. #include <ostream>
  34. #include <istream>
  35. #include <limits>
  36. #include <cmath>
  37. #include <cstdint>
  38. #if defined(__SGI_STL_PORT) || defined(_RWSTD_VER) || defined(__LIBCOMO__)
  39. # include <cstdio>
  40. #endif
  41. #if defined __has_include
  42. # if __cplusplus > 202002L || _MSVC_LANG > 202002L
  43. # if __has_include (<stdfloat>)
  44. # include <stdfloat>
  45. # endif
  46. # endif
  47. #endif
  48. namespace boost{ namespace math{
  49. namespace concepts
  50. {
  51. #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  52. typedef double real_concept_base_type;
  53. #else
  54. typedef long double real_concept_base_type;
  55. #endif
  56. class real_concept
  57. {
  58. public:
  59. // Constructors:
  60. real_concept() : m_value(0){}
  61. real_concept(char c) : m_value(c){}
  62. real_concept(wchar_t c) : m_value(c){}
  63. real_concept(unsigned char c) : m_value(c){}
  64. real_concept(signed char c) : m_value(c){}
  65. real_concept(unsigned short c) : m_value(c){}
  66. real_concept(short c) : m_value(c){}
  67. real_concept(unsigned int c) : m_value(c){}
  68. real_concept(int c) : m_value(c){}
  69. real_concept(unsigned long c) : m_value(c){}
  70. real_concept(long c) : m_value(c){}
  71. real_concept(unsigned long long c) : m_value(static_cast<real_concept_base_type>(c)){}
  72. real_concept(long long c) : m_value(static_cast<real_concept_base_type>(c)){}
  73. real_concept(float c) : m_value(c){}
  74. real_concept(double c) : m_value(c){}
  75. real_concept(long double c) : m_value(c){}
  76. #ifdef BOOST_MATH_USE_FLOAT128
  77. real_concept(BOOST_MATH_FLOAT128_TYPE c) : m_value(c){}
  78. #endif
  79. #ifdef __STDCPP_FLOAT32_T__
  80. real_concept(std::float32_t c) : m_value(static_cast<real_concept_base_type>(c)){}
  81. #endif
  82. #ifdef __STDCPP_FLOAT64_T__
  83. real_concept(std::float64_t c) : m_value(static_cast<real_concept_base_type>(c)){}
  84. #endif
  85. // Assignment:
  86. real_concept& operator=(char c) { m_value = c; return *this; }
  87. real_concept& operator=(unsigned char c) { m_value = c; return *this; }
  88. real_concept& operator=(signed char c) { m_value = c; return *this; }
  89. real_concept& operator=(wchar_t c) { m_value = c; return *this; }
  90. real_concept& operator=(short c) { m_value = c; return *this; }
  91. real_concept& operator=(unsigned short c) { m_value = c; return *this; }
  92. real_concept& operator=(int c) { m_value = c; return *this; }
  93. real_concept& operator=(unsigned int c) { m_value = c; return *this; }
  94. real_concept& operator=(long c) { m_value = c; return *this; }
  95. real_concept& operator=(unsigned long c) { m_value = c; return *this; }
  96. real_concept& operator=(long long c) { m_value = static_cast<real_concept_base_type>(c); return *this; }
  97. real_concept& operator=(unsigned long long c) { m_value = static_cast<real_concept_base_type>(c); return *this; }
  98. real_concept& operator=(float c) { m_value = c; return *this; }
  99. real_concept& operator=(double c) { m_value = c; return *this; }
  100. real_concept& operator=(long double c) { m_value = c; return *this; }
  101. #ifdef __STDCPP_FLOAT32_T__
  102. real_concept& operator=(std::float32_t c) { m_value = c; return *this; }
  103. #endif
  104. #ifdef __STDCPP_FLOAT64_T__
  105. real_concept& operator=(std::float64_t c) { m_value = c; return *this; }
  106. #endif
  107. // Access:
  108. real_concept_base_type value()const{ return m_value; }
  109. // Member arithmetic:
  110. real_concept& operator+=(const real_concept& other)
  111. { m_value += other.value(); return *this; }
  112. real_concept& operator-=(const real_concept& other)
  113. { m_value -= other.value(); return *this; }
  114. real_concept& operator*=(const real_concept& other)
  115. { m_value *= other.value(); return *this; }
  116. real_concept& operator/=(const real_concept& other)
  117. { m_value /= other.value(); return *this; }
  118. real_concept operator-()const
  119. { return -m_value; }
  120. real_concept const& operator+()const
  121. { return *this; }
  122. real_concept& operator++()
  123. { ++m_value; return *this; }
  124. real_concept& operator--()
  125. { --m_value; return *this; }
  126. private:
  127. real_concept_base_type m_value;
  128. };
  129. // Non-member arithmetic:
  130. inline real_concept operator+(const real_concept& a, const real_concept& b)
  131. {
  132. real_concept result(a);
  133. result += b;
  134. return result;
  135. }
  136. inline real_concept operator-(const real_concept& a, const real_concept& b)
  137. {
  138. real_concept result(a);
  139. result -= b;
  140. return result;
  141. }
  142. inline real_concept operator*(const real_concept& a, const real_concept& b)
  143. {
  144. real_concept result(a);
  145. result *= b;
  146. return result;
  147. }
  148. inline real_concept operator/(const real_concept& a, const real_concept& b)
  149. {
  150. real_concept result(a);
  151. result /= b;
  152. return result;
  153. }
  154. // Comparison:
  155. inline bool operator == (const real_concept& a, const real_concept& b)
  156. { return a.value() == b.value(); }
  157. inline bool operator != (const real_concept& a, const real_concept& b)
  158. { return a.value() != b.value();}
  159. inline bool operator < (const real_concept& a, const real_concept& b)
  160. { return a.value() < b.value(); }
  161. inline bool operator <= (const real_concept& a, const real_concept& b)
  162. { return a.value() <= b.value(); }
  163. inline bool operator > (const real_concept& a, const real_concept& b)
  164. { return a.value() > b.value(); }
  165. inline bool operator >= (const real_concept& a, const real_concept& b)
  166. { return a.value() >= b.value(); }
  167. // Non-member functions:
  168. inline real_concept acos(real_concept a)
  169. { return std::acos(a.value()); }
  170. inline real_concept cos(real_concept a)
  171. { return std::cos(a.value()); }
  172. inline real_concept asin(real_concept a)
  173. { return std::asin(a.value()); }
  174. inline real_concept atan(real_concept a)
  175. { return std::atan(a.value()); }
  176. inline real_concept atan2(real_concept a, real_concept b)
  177. { return std::atan2(a.value(), b.value()); }
  178. inline real_concept ceil(real_concept a)
  179. { return std::ceil(a.value()); }
  180. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  181. // I've seen std::fmod(long double) crash on some platforms
  182. // so use fmodl instead:
  183. #ifdef _WIN32_WCE
  184. //
  185. // Ugly workaround for macro fmodl:
  186. //
  187. inline long double call_fmodl(long double a, long double b)
  188. { return fmodl(a, b); }
  189. inline real_concept fmod(real_concept a, real_concept b)
  190. { return call_fmodl(a.value(), b.value()); }
  191. #else
  192. inline real_concept fmod(real_concept a, real_concept b)
  193. { return fmodl(a.value(), b.value()); }
  194. #endif
  195. #endif
  196. inline real_concept cosh(real_concept a)
  197. { return std::cosh(a.value()); }
  198. inline real_concept exp(real_concept a)
  199. { return std::exp(a.value()); }
  200. inline real_concept fabs(real_concept a)
  201. { return std::fabs(a.value()); }
  202. inline real_concept abs(real_concept a)
  203. { return std::abs(a.value()); }
  204. inline real_concept floor(real_concept a)
  205. { return std::floor(a.value()); }
  206. inline real_concept modf(real_concept a, real_concept* ipart)
  207. {
  208. #ifdef __MINGW32__
  209. real_concept_base_type ip;
  210. real_concept_base_type result = boost::math::modf(a.value(), &ip);
  211. *ipart = ip;
  212. return result;
  213. #else
  214. real_concept_base_type ip;
  215. real_concept_base_type result = std::modf(a.value(), &ip);
  216. *ipart = ip;
  217. return result;
  218. #endif
  219. }
  220. inline real_concept frexp(real_concept a, int* expon)
  221. { return std::frexp(a.value(), expon); }
  222. inline real_concept ldexp(real_concept a, int expon)
  223. { return std::ldexp(a.value(), expon); }
  224. inline real_concept log(real_concept a)
  225. { return std::log(a.value()); }
  226. inline real_concept log10(real_concept a)
  227. { return std::log10(a.value()); }
  228. inline real_concept tan(real_concept a)
  229. { return std::tan(a.value()); }
  230. inline real_concept pow(real_concept a, real_concept b)
  231. { return std::pow(a.value(), b.value()); }
  232. #if !defined(__SUNPRO_CC)
  233. inline real_concept pow(real_concept a, int b)
  234. { return std::pow(a.value(), b); }
  235. #else
  236. inline real_concept pow(real_concept a, int b)
  237. { return std::pow(a.value(), static_cast<real_concept_base_type>(b)); }
  238. #endif
  239. inline real_concept sin(real_concept a)
  240. { return std::sin(a.value()); }
  241. inline real_concept sinh(real_concept a)
  242. { return std::sinh(a.value()); }
  243. inline real_concept sqrt(real_concept a)
  244. { return std::sqrt(a.value()); }
  245. inline real_concept tanh(real_concept a)
  246. { return std::tanh(a.value()); }
  247. //
  248. // C++11 ism's
  249. // Note that these must not actually call the std:: versions as that precludes using this
  250. // header to test in C++03 mode, call the Boost versions instead:
  251. //
  252. inline boost::math::concepts::real_concept asinh(boost::math::concepts::real_concept a)
  253. {
  254. return boost::math::asinh(a.value(), boost::math::policies::make_policy(boost::math::policies::overflow_error<boost::math::policies::ignore_error>()));
  255. }
  256. inline boost::math::concepts::real_concept acosh(boost::math::concepts::real_concept a)
  257. {
  258. return boost::math::acosh(a.value(), boost::math::policies::make_policy(boost::math::policies::overflow_error<boost::math::policies::ignore_error>()));
  259. }
  260. inline boost::math::concepts::real_concept atanh(boost::math::concepts::real_concept a)
  261. {
  262. return boost::math::atanh(a.value(), boost::math::policies::make_policy(boost::math::policies::overflow_error<boost::math::policies::ignore_error>()));
  263. }
  264. //
  265. // Conversion and truncation routines:
  266. //
  267. template <class Policy>
  268. inline int iround(const concepts::real_concept& v, const Policy& pol)
  269. { return boost::math::iround(v.value(), pol); }
  270. inline int iround(const concepts::real_concept& v)
  271. { return boost::math::iround(v.value(), policies::policy<>()); }
  272. template <class Policy>
  273. inline long lround(const concepts::real_concept& v, const Policy& pol)
  274. { return boost::math::lround(v.value(), pol); }
  275. inline long lround(const concepts::real_concept& v)
  276. { return boost::math::lround(v.value(), policies::policy<>()); }
  277. template <class Policy>
  278. inline long long llround(const concepts::real_concept& v, const Policy& pol)
  279. { return boost::math::llround(v.value(), pol); }
  280. inline long long llround(const concepts::real_concept& v)
  281. { return boost::math::llround(v.value(), policies::policy<>()); }
  282. template <class Policy>
  283. inline int itrunc(const concepts::real_concept& v, const Policy& pol)
  284. { return boost::math::itrunc(v.value(), pol); }
  285. inline int itrunc(const concepts::real_concept& v)
  286. { return boost::math::itrunc(v.value(), policies::policy<>()); }
  287. template <class Policy>
  288. inline long ltrunc(const concepts::real_concept& v, const Policy& pol)
  289. { return boost::math::ltrunc(v.value(), pol); }
  290. inline long ltrunc(const concepts::real_concept& v)
  291. { return boost::math::ltrunc(v.value(), policies::policy<>()); }
  292. template <class Policy>
  293. inline long long lltrunc(const concepts::real_concept& v, const Policy& pol)
  294. { return boost::math::lltrunc(v.value(), pol); }
  295. inline long long lltrunc(const concepts::real_concept& v)
  296. { return boost::math::lltrunc(v.value(), policies::policy<>()); }
  297. // Streaming:
  298. template <class charT, class traits>
  299. inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const real_concept& a)
  300. {
  301. return os << a.value();
  302. }
  303. template <class charT, class traits>
  304. inline std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& is, real_concept& a)
  305. {
  306. real_concept_base_type v;
  307. is >> v;
  308. a = v;
  309. return is;
  310. }
  311. } // namespace concepts
  312. namespace tools
  313. {
  314. template <>
  315. inline concepts::real_concept make_big_value<concepts::real_concept>(boost::math::tools::largest_float val, const char* , std::false_type const&, std::false_type const&)
  316. {
  317. return val; // Can't use lexical_cast here, sometimes it fails....
  318. }
  319. template <>
  320. inline concepts::real_concept max_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
  321. {
  322. return max_value<concepts::real_concept_base_type>();
  323. }
  324. template <>
  325. inline concepts::real_concept min_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
  326. {
  327. return min_value<concepts::real_concept_base_type>();
  328. }
  329. template <>
  330. inline concepts::real_concept log_max_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
  331. {
  332. return log_max_value<concepts::real_concept_base_type>();
  333. }
  334. template <>
  335. inline concepts::real_concept log_min_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
  336. {
  337. return log_min_value<concepts::real_concept_base_type>();
  338. }
  339. template <>
  340. inline concepts::real_concept epsilon<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
  341. {
  342. #ifdef __SUNPRO_CC
  343. return std::numeric_limits<concepts::real_concept_base_type>::epsilon();
  344. #else
  345. return tools::epsilon<concepts::real_concept_base_type>();
  346. #endif
  347. }
  348. template <>
  349. inline constexpr int digits<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept)) noexcept
  350. {
  351. // Assume number of significand bits is same as real_concept_base_type,
  352. // unless std::numeric_limits<T>::is_specialized to provide digits.
  353. return tools::digits<concepts::real_concept_base_type>();
  354. // Note that if numeric_limits real concept is NOT specialized to provide digits10
  355. // (or max_digits10) then the default precision of 6 decimal digits will be used
  356. // by Boost test (giving misleading error messages like
  357. // "difference between {9.79796} and {9.79796} exceeds 5.42101e-19%"
  358. // and by Boost lexical cast and serialization causing loss of accuracy.
  359. }
  360. } // namespace tools
  361. } // namespace math
  362. } // namespace boost
  363. #endif // BOOST_MATH_REAL_CONCEPT_HPP