std_real_concept.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. // std_real_concept is an archetype for built-in Real types.
  6. // The main purpose in providing this type is to verify
  7. // that std lib functions are found via a using declaration
  8. // bringing those functions into the current scope, and not
  9. // just because they happen to be in global scope.
  10. //
  11. // If ::pow is found rather than std::pow say, then the code
  12. // will silently compile, but truncation of long doubles to
  13. // double will cause a significant loss of precision.
  14. // A template instantiated with std_real_concept will *only*
  15. // compile if it std::whatever is in scope.
  16. #include <boost/config.hpp>
  17. #include <boost/limits.hpp>
  18. #include <boost/math/policies/policy.hpp>
  19. #include <boost/math/special_functions/math_fwd.hpp>
  20. #include <ostream>
  21. #include <istream>
  22. #include <boost/config/no_tr1/cmath.hpp>
  23. #include <math.h> // fmodl
  24. #ifndef BOOST_MATH_STD_REAL_CONCEPT_HPP
  25. #define BOOST_MATH_STD_REAL_CONCEPT_HPP
  26. namespace boost{ namespace math{
  27. namespace concepts
  28. {
  29. #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  30. typedef double std_real_concept_base_type;
  31. #else
  32. typedef long double std_real_concept_base_type;
  33. #endif
  34. class std_real_concept
  35. {
  36. public:
  37. // Constructors:
  38. std_real_concept() : m_value(0){}
  39. std_real_concept(char c) : m_value(c){}
  40. #ifndef BOOST_NO_INTRINSIC_WCHAR_T
  41. std_real_concept(wchar_t c) : m_value(c){}
  42. #endif
  43. std_real_concept(unsigned char c) : m_value(c){}
  44. std_real_concept(signed char c) : m_value(c){}
  45. std_real_concept(unsigned short c) : m_value(c){}
  46. std_real_concept(short c) : m_value(c){}
  47. std_real_concept(unsigned int c) : m_value(c){}
  48. std_real_concept(int c) : m_value(c){}
  49. std_real_concept(unsigned long c) : m_value(c){}
  50. std_real_concept(long c) : m_value(c){}
  51. #if defined(__DECCXX) || defined(__SUNPRO_CC)
  52. std_real_concept(unsigned long long c) : m_value(static_cast<std_real_concept_base_type>(c)){}
  53. std_real_concept(long long c) : m_value(static_cast<std_real_concept_base_type>(c)){}
  54. #elif defined(BOOST_HAS_LONG_LONG)
  55. std_real_concept(boost::ulong_long_type c) : m_value(static_cast<std_real_concept_base_type>(c)){}
  56. std_real_concept(boost::long_long_type c) : m_value(static_cast<std_real_concept_base_type>(c)){}
  57. #endif
  58. std_real_concept(float c) : m_value(c){}
  59. std_real_concept(double c) : m_value(c){}
  60. std_real_concept(long double c) : m_value(c){}
  61. #ifdef BOOST_MATH_USE_FLOAT128
  62. std_real_concept(__float128 c) : m_value(c){}
  63. #endif
  64. // Assignment:
  65. std_real_concept& operator=(char c) { m_value = c; return *this; }
  66. std_real_concept& operator=(unsigned char c) { m_value = c; return *this; }
  67. std_real_concept& operator=(signed char c) { m_value = c; return *this; }
  68. #ifndef BOOST_NO_INTRINSIC_WCHAR_T
  69. std_real_concept& operator=(wchar_t c) { m_value = c; return *this; }
  70. #endif
  71. std_real_concept& operator=(short c) { m_value = c; return *this; }
  72. std_real_concept& operator=(unsigned short c) { m_value = c; return *this; }
  73. std_real_concept& operator=(int c) { m_value = c; return *this; }
  74. std_real_concept& operator=(unsigned int c) { m_value = c; return *this; }
  75. std_real_concept& operator=(long c) { m_value = c; return *this; }
  76. std_real_concept& operator=(unsigned long c) { m_value = c; return *this; }
  77. #if defined(__DECCXX) || defined(__SUNPRO_CC)
  78. std_real_concept& operator=(unsigned long long c) { m_value = static_cast<std_real_concept_base_type>(c); return *this; }
  79. std_real_concept& operator=(long long c) { m_value = static_cast<std_real_concept_base_type>(c); return *this; }
  80. #elif defined(BOOST_HAS_LONG_LONG)
  81. std_real_concept& operator=(boost::long_long_type c) { m_value = static_cast<std_real_concept_base_type>(c); return *this; }
  82. std_real_concept& operator=(boost::ulong_long_type c) { m_value = static_cast<std_real_concept_base_type>(c); return *this; }
  83. #endif
  84. std_real_concept& operator=(float c) { m_value = c; return *this; }
  85. std_real_concept& operator=(double c) { m_value = c; return *this; }
  86. std_real_concept& operator=(long double c) { m_value = c; return *this; }
  87. // Access:
  88. std_real_concept_base_type value()const{ return m_value; }
  89. // Member arithmetic:
  90. std_real_concept& operator+=(const std_real_concept& other)
  91. { m_value += other.value(); return *this; }
  92. std_real_concept& operator-=(const std_real_concept& other)
  93. { m_value -= other.value(); return *this; }
  94. std_real_concept& operator*=(const std_real_concept& other)
  95. { m_value *= other.value(); return *this; }
  96. std_real_concept& operator/=(const std_real_concept& other)
  97. { m_value /= other.value(); return *this; }
  98. std_real_concept operator-()const
  99. { return -m_value; }
  100. std_real_concept const& operator+()const
  101. { return *this; }
  102. private:
  103. std_real_concept_base_type m_value;
  104. };
  105. // Non-member arithmetic:
  106. inline std_real_concept operator+(const std_real_concept& a, const std_real_concept& b)
  107. {
  108. std_real_concept result(a);
  109. result += b;
  110. return result;
  111. }
  112. inline std_real_concept operator-(const std_real_concept& a, const std_real_concept& b)
  113. {
  114. std_real_concept result(a);
  115. result -= b;
  116. return result;
  117. }
  118. inline std_real_concept operator*(const std_real_concept& a, const std_real_concept& b)
  119. {
  120. std_real_concept result(a);
  121. result *= b;
  122. return result;
  123. }
  124. inline std_real_concept operator/(const std_real_concept& a, const std_real_concept& b)
  125. {
  126. std_real_concept result(a);
  127. result /= b;
  128. return result;
  129. }
  130. // Comparison:
  131. inline bool operator == (const std_real_concept& a, const std_real_concept& b)
  132. { return a.value() == b.value(); }
  133. inline bool operator != (const std_real_concept& a, const std_real_concept& b)
  134. { return a.value() != b.value();}
  135. inline bool operator < (const std_real_concept& a, const std_real_concept& b)
  136. { return a.value() < b.value(); }
  137. inline bool operator <= (const std_real_concept& a, const std_real_concept& b)
  138. { return a.value() <= b.value(); }
  139. inline bool operator > (const std_real_concept& a, const std_real_concept& b)
  140. { return a.value() > b.value(); }
  141. inline bool operator >= (const std_real_concept& a, const std_real_concept& b)
  142. { return a.value() >= b.value(); }
  143. } // namespace concepts
  144. } // namespace math
  145. } // namespace boost
  146. namespace std{
  147. // Non-member functions:
  148. inline boost::math::concepts::std_real_concept acos(boost::math::concepts::std_real_concept a)
  149. { return std::acos(a.value()); }
  150. inline boost::math::concepts::std_real_concept cos(boost::math::concepts::std_real_concept a)
  151. { return std::cos(a.value()); }
  152. inline boost::math::concepts::std_real_concept asin(boost::math::concepts::std_real_concept a)
  153. { return std::asin(a.value()); }
  154. inline boost::math::concepts::std_real_concept atan(boost::math::concepts::std_real_concept a)
  155. { return std::atan(a.value()); }
  156. inline boost::math::concepts::std_real_concept atan2(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept b)
  157. { return std::atan2(a.value(), b.value()); }
  158. inline boost::math::concepts::std_real_concept ceil(boost::math::concepts::std_real_concept a)
  159. { return std::ceil(a.value()); }
  160. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  161. inline boost::math::concepts::std_real_concept fmod(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept b)
  162. { return fmodl(a.value(), b.value()); }
  163. #else
  164. inline boost::math::concepts::std_real_concept fmod(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept b)
  165. { return std::fmod(a.value(), b.value()); }
  166. #endif
  167. inline boost::math::concepts::std_real_concept cosh(boost::math::concepts::std_real_concept a)
  168. { return std::cosh(a.value()); }
  169. inline boost::math::concepts::std_real_concept exp(boost::math::concepts::std_real_concept a)
  170. { return std::exp(a.value()); }
  171. inline boost::math::concepts::std_real_concept fabs(boost::math::concepts::std_real_concept a)
  172. { return std::fabs(a.value()); }
  173. inline boost::math::concepts::std_real_concept abs(boost::math::concepts::std_real_concept a)
  174. { return std::abs(a.value()); }
  175. inline boost::math::concepts::std_real_concept floor(boost::math::concepts::std_real_concept a)
  176. { return std::floor(a.value()); }
  177. inline boost::math::concepts::std_real_concept modf(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept* ipart)
  178. {
  179. boost::math::concepts::std_real_concept_base_type ip;
  180. boost::math::concepts::std_real_concept_base_type result = std::modf(a.value(), &ip);
  181. *ipart = ip;
  182. return result;
  183. }
  184. inline boost::math::concepts::std_real_concept frexp(boost::math::concepts::std_real_concept a, int* expon)
  185. { return std::frexp(a.value(), expon); }
  186. inline boost::math::concepts::std_real_concept ldexp(boost::math::concepts::std_real_concept a, int expon)
  187. { return std::ldexp(a.value(), expon); }
  188. inline boost::math::concepts::std_real_concept log(boost::math::concepts::std_real_concept a)
  189. { return std::log(a.value()); }
  190. inline boost::math::concepts::std_real_concept log10(boost::math::concepts::std_real_concept a)
  191. { return std::log10(a.value()); }
  192. inline boost::math::concepts::std_real_concept tan(boost::math::concepts::std_real_concept a)
  193. { return std::tan(a.value()); }
  194. inline boost::math::concepts::std_real_concept pow(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept b)
  195. { return std::pow(a.value(), b.value()); }
  196. #if !defined(__SUNPRO_CC)
  197. inline boost::math::concepts::std_real_concept pow(boost::math::concepts::std_real_concept a, int b)
  198. { return std::pow(a.value(), b); }
  199. #else
  200. inline boost::math::concepts::std_real_concept pow(boost::math::concepts::std_real_concept a, int b)
  201. { return std::pow(a.value(), static_cast<long double>(b)); }
  202. #endif
  203. inline boost::math::concepts::std_real_concept sin(boost::math::concepts::std_real_concept a)
  204. { return std::sin(a.value()); }
  205. inline boost::math::concepts::std_real_concept sinh(boost::math::concepts::std_real_concept a)
  206. { return std::sinh(a.value()); }
  207. inline boost::math::concepts::std_real_concept sqrt(boost::math::concepts::std_real_concept a)
  208. { return std::sqrt(a.value()); }
  209. inline boost::math::concepts::std_real_concept tanh(boost::math::concepts::std_real_concept a)
  210. { return std::tanh(a.value()); }
  211. } // namespace std
  212. #include <boost/math/special_functions/round.hpp>
  213. #include <boost/math/special_functions/trunc.hpp>
  214. #include <boost/math/special_functions/modf.hpp>
  215. #include <boost/math/tools/precision.hpp>
  216. namespace boost{ namespace math{ namespace concepts{
  217. //
  218. // Conversion and truncation routines:
  219. //
  220. template <class Policy>
  221. inline int iround(const concepts::std_real_concept& v, const Policy& pol)
  222. {
  223. return boost::math::iround(v.value(), pol);
  224. }
  225. inline int iround(const concepts::std_real_concept& v)
  226. {
  227. return boost::math::iround(v.value(), policies::policy<>());
  228. }
  229. template <class Policy>
  230. inline long lround(const concepts::std_real_concept& v, const Policy& pol)
  231. {
  232. return boost::math::lround(v.value(), pol);
  233. }
  234. inline long lround(const concepts::std_real_concept& v)
  235. {
  236. return boost::math::lround(v.value(), policies::policy<>());
  237. }
  238. #ifdef BOOST_HAS_LONG_LONG
  239. template <class Policy>
  240. inline boost::long_long_type llround(const concepts::std_real_concept& v, const Policy& pol)
  241. {
  242. return boost::math::llround(v.value(), pol);
  243. }
  244. inline boost::long_long_type llround(const concepts::std_real_concept& v)
  245. {
  246. return boost::math::llround(v.value(), policies::policy<>());
  247. }
  248. #endif
  249. template <class Policy>
  250. inline int itrunc(const concepts::std_real_concept& v, const Policy& pol)
  251. {
  252. return boost::math::itrunc(v.value(), pol);
  253. }
  254. inline int itrunc(const concepts::std_real_concept& v)
  255. {
  256. return boost::math::itrunc(v.value(), policies::policy<>());
  257. }
  258. template <class Policy>
  259. inline long ltrunc(const concepts::std_real_concept& v, const Policy& pol)
  260. {
  261. return boost::math::ltrunc(v.value(), pol);
  262. }
  263. inline long ltrunc(const concepts::std_real_concept& v)
  264. {
  265. return boost::math::ltrunc(v.value(), policies::policy<>());
  266. }
  267. #ifdef BOOST_HAS_LONG_LONG
  268. template <class Policy>
  269. inline boost::long_long_type lltrunc(const concepts::std_real_concept& v, const Policy& pol)
  270. {
  271. return boost::math::lltrunc(v.value(), pol);
  272. }
  273. inline boost::long_long_type lltrunc(const concepts::std_real_concept& v)
  274. {
  275. return boost::math::lltrunc(v.value(), policies::policy<>());
  276. }
  277. #endif
  278. // Streaming:
  279. template <class charT, class traits>
  280. inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const std_real_concept& a)
  281. {
  282. return os << a.value();
  283. }
  284. template <class charT, class traits>
  285. inline std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& is, std_real_concept& a)
  286. {
  287. std_real_concept_base_type v;
  288. is >> v;
  289. a = v;
  290. return is;
  291. }
  292. } // namespace concepts
  293. }}
  294. #include <boost/math/tools/precision.hpp>
  295. #include <boost/math/tools/big_constant.hpp>
  296. namespace boost{ namespace math{
  297. namespace tools
  298. {
  299. template <>
  300. inline concepts::std_real_concept make_big_value<concepts::std_real_concept>(long double val, const char* , mpl::false_ const&, mpl::false_ const&)
  301. {
  302. return val; // Can't use lexical_cast here, sometimes it fails....
  303. }
  304. template <>
  305. inline concepts::std_real_concept max_value<concepts::std_real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept))
  306. {
  307. return max_value<concepts::std_real_concept_base_type>();
  308. }
  309. template <>
  310. inline concepts::std_real_concept min_value<concepts::std_real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept))
  311. {
  312. return min_value<concepts::std_real_concept_base_type>();
  313. }
  314. template <>
  315. inline concepts::std_real_concept log_max_value<concepts::std_real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept))
  316. {
  317. return log_max_value<concepts::std_real_concept_base_type>();
  318. }
  319. template <>
  320. inline concepts::std_real_concept log_min_value<concepts::std_real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept))
  321. {
  322. return log_min_value<concepts::std_real_concept_base_type>();
  323. }
  324. template <>
  325. inline concepts::std_real_concept epsilon(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept))
  326. {
  327. return tools::epsilon<concepts::std_real_concept_base_type>();
  328. }
  329. template <>
  330. inline int digits<concepts::std_real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept))
  331. { // Assume number of significand bits is same as std_real_concept_base_type,
  332. // unless std::numeric_limits<T>::is_specialized to provide digits.
  333. return digits<concepts::std_real_concept_base_type>();
  334. }
  335. } // namespace tools
  336. #if BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
  337. using concepts::itrunc;
  338. using concepts::ltrunc;
  339. using concepts::lltrunc;
  340. using concepts::iround;
  341. using concepts::lround;
  342. using concepts::llround;
  343. #endif
  344. } // namespace math
  345. } // namespace boost
  346. #endif // BOOST_MATH_STD_REAL_CONCEPT_HPP