std_real_concept.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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(BOOST_MATH_FLOAT128_TYPE 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. inline boost::math::concepts::std_real_concept (nextafter)(boost::math::concepts::std_real_concept a, boost::math::concepts::std_real_concept b)
  212. { return (boost::math::nextafter)(a, b); }
  213. //
  214. // C++11 ism's
  215. // Note that these must not actually call the std:: versions as that precludes using this
  216. // header to test in C++03 mode, call the Boost versions instead:
  217. //
  218. inline boost::math::concepts::std_real_concept asinh(boost::math::concepts::std_real_concept a)
  219. { return boost::math::asinh(a.value(), boost::math::policies::make_policy(boost::math::policies::overflow_error<boost::math::policies::ignore_error>())); }
  220. inline boost::math::concepts::std_real_concept acosh(boost::math::concepts::std_real_concept a)
  221. { return boost::math::acosh(a.value(), boost::math::policies::make_policy(boost::math::policies::overflow_error<boost::math::policies::ignore_error>())); }
  222. inline boost::math::concepts::std_real_concept atanh(boost::math::concepts::std_real_concept a)
  223. { return boost::math::atanh(a.value(), boost::math::policies::make_policy(boost::math::policies::overflow_error<boost::math::policies::ignore_error>())); }
  224. inline bool (isfinite)(boost::math::concepts::std_real_concept a)
  225. {
  226. return (boost::math::isfinite)(a.value());
  227. }
  228. } // namespace std
  229. #include <boost/math/special_functions/round.hpp>
  230. #include <boost/math/special_functions/trunc.hpp>
  231. #include <boost/math/special_functions/modf.hpp>
  232. #include <boost/math/tools/precision.hpp>
  233. namespace boost{ namespace math{ namespace concepts{
  234. //
  235. // Conversion and truncation routines:
  236. //
  237. template <class Policy>
  238. inline int iround(const concepts::std_real_concept& v, const Policy& pol)
  239. {
  240. return boost::math::iround(v.value(), pol);
  241. }
  242. inline int iround(const concepts::std_real_concept& v)
  243. {
  244. return boost::math::iround(v.value(), policies::policy<>());
  245. }
  246. template <class Policy>
  247. inline long lround(const concepts::std_real_concept& v, const Policy& pol)
  248. {
  249. return boost::math::lround(v.value(), pol);
  250. }
  251. inline long lround(const concepts::std_real_concept& v)
  252. {
  253. return boost::math::lround(v.value(), policies::policy<>());
  254. }
  255. #ifdef BOOST_HAS_LONG_LONG
  256. template <class Policy>
  257. inline boost::long_long_type llround(const concepts::std_real_concept& v, const Policy& pol)
  258. {
  259. return boost::math::llround(v.value(), pol);
  260. }
  261. inline boost::long_long_type llround(const concepts::std_real_concept& v)
  262. {
  263. return boost::math::llround(v.value(), policies::policy<>());
  264. }
  265. #endif
  266. template <class Policy>
  267. inline int itrunc(const concepts::std_real_concept& v, const Policy& pol)
  268. {
  269. return boost::math::itrunc(v.value(), pol);
  270. }
  271. inline int itrunc(const concepts::std_real_concept& v)
  272. {
  273. return boost::math::itrunc(v.value(), policies::policy<>());
  274. }
  275. template <class Policy>
  276. inline long ltrunc(const concepts::std_real_concept& v, const Policy& pol)
  277. {
  278. return boost::math::ltrunc(v.value(), pol);
  279. }
  280. inline long ltrunc(const concepts::std_real_concept& v)
  281. {
  282. return boost::math::ltrunc(v.value(), policies::policy<>());
  283. }
  284. #ifdef BOOST_HAS_LONG_LONG
  285. template <class Policy>
  286. inline boost::long_long_type lltrunc(const concepts::std_real_concept& v, const Policy& pol)
  287. {
  288. return boost::math::lltrunc(v.value(), pol);
  289. }
  290. inline boost::long_long_type lltrunc(const concepts::std_real_concept& v)
  291. {
  292. return boost::math::lltrunc(v.value(), policies::policy<>());
  293. }
  294. #endif
  295. // Streaming:
  296. template <class charT, class traits>
  297. inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const std_real_concept& a)
  298. {
  299. return os << a.value();
  300. }
  301. template <class charT, class traits>
  302. inline std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& is, std_real_concept& a)
  303. {
  304. #if defined(__SGI_STL_PORT) || defined(_RWSTD_VER) || defined(__LIBCOMO__) || defined(_LIBCPP_VERSION)
  305. std::string s;
  306. std_real_concept_base_type d;
  307. is >> s;
  308. std::sscanf(s.c_str(), "%Lf", &d);
  309. a = d;
  310. return is;
  311. #else
  312. std_real_concept_base_type v;
  313. is >> v;
  314. a = v;
  315. return is;
  316. #endif
  317. }
  318. } // namespace concepts
  319. }}
  320. #include <boost/math/tools/precision.hpp>
  321. #include <boost/math/tools/big_constant.hpp>
  322. namespace boost{ namespace math{
  323. namespace tools
  324. {
  325. template <>
  326. inline concepts::std_real_concept make_big_value<concepts::std_real_concept>(boost::math::tools::largest_float val, const char*, mpl::false_ const&, mpl::false_ const&)
  327. {
  328. return val; // Can't use lexical_cast here, sometimes it fails....
  329. }
  330. template <>
  331. inline concepts::std_real_concept max_value<concepts::std_real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept))
  332. {
  333. return max_value<concepts::std_real_concept_base_type>();
  334. }
  335. template <>
  336. inline concepts::std_real_concept min_value<concepts::std_real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept))
  337. {
  338. return min_value<concepts::std_real_concept_base_type>();
  339. }
  340. template <>
  341. inline concepts::std_real_concept log_max_value<concepts::std_real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept))
  342. {
  343. return log_max_value<concepts::std_real_concept_base_type>();
  344. }
  345. template <>
  346. inline concepts::std_real_concept log_min_value<concepts::std_real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept))
  347. {
  348. return log_min_value<concepts::std_real_concept_base_type>();
  349. }
  350. template <>
  351. inline concepts::std_real_concept epsilon(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept))
  352. {
  353. return tools::epsilon<concepts::std_real_concept_base_type>();
  354. }
  355. template <>
  356. inline BOOST_MATH_CONSTEXPR int digits<concepts::std_real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::std_real_concept)) BOOST_NOEXCEPT
  357. { // Assume number of significand bits is same as std_real_concept_base_type,
  358. // unless std::numeric_limits<T>::is_specialized to provide digits.
  359. return digits<concepts::std_real_concept_base_type>();
  360. }
  361. template <>
  362. inline double real_cast<double, concepts::std_real_concept>(concepts::std_real_concept r)
  363. {
  364. return static_cast<double>(r.value());
  365. }
  366. } // namespace tools
  367. #if BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
  368. using concepts::itrunc;
  369. using concepts::ltrunc;
  370. using concepts::lltrunc;
  371. using concepts::iround;
  372. using concepts::lround;
  373. using concepts::llround;
  374. #endif
  375. } // namespace math
  376. } // namespace boost
  377. //
  378. // These must go at the end, as they include stuff that won't compile until
  379. // after std_real_concept has been defined:
  380. //
  381. #include <boost/math/special_functions/acosh.hpp>
  382. #include <boost/math/special_functions/asinh.hpp>
  383. #include <boost/math/special_functions/atanh.hpp>
  384. #endif // BOOST_MATH_STD_REAL_CONCEPT_HPP