real_concept.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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/config.hpp>
  22. #include <boost/limits.hpp>
  23. #include <boost/math/special_functions/round.hpp>
  24. #include <boost/math/special_functions/trunc.hpp>
  25. #include <boost/math/special_functions/modf.hpp>
  26. #include <boost/math/tools/big_constant.hpp>
  27. #include <boost/math/tools/precision.hpp>
  28. #include <boost/math/policies/policy.hpp>
  29. #if defined(__SGI_STL_PORT)
  30. # include <boost/math/tools/real_cast.hpp>
  31. #endif
  32. #include <ostream>
  33. #include <istream>
  34. #include <boost/config/no_tr1/cmath.hpp>
  35. #include <math.h> // fmodl
  36. #if defined(__SGI_STL_PORT) || defined(_RWSTD_VER) || defined(__LIBCOMO__)
  37. # include <cstdio>
  38. #endif
  39. namespace boost{ namespace math{
  40. namespace concepts
  41. {
  42. #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  43. typedef double real_concept_base_type;
  44. #else
  45. typedef long double real_concept_base_type;
  46. #endif
  47. class real_concept
  48. {
  49. public:
  50. // Constructors:
  51. real_concept() : m_value(0){}
  52. real_concept(char c) : m_value(c){}
  53. #ifndef BOOST_NO_INTRINSIC_WCHAR_T
  54. real_concept(wchar_t c) : m_value(c){}
  55. #endif
  56. real_concept(unsigned char c) : m_value(c){}
  57. real_concept(signed char c) : m_value(c){}
  58. real_concept(unsigned short c) : m_value(c){}
  59. real_concept(short c) : m_value(c){}
  60. real_concept(unsigned int c) : m_value(c){}
  61. real_concept(int c) : m_value(c){}
  62. real_concept(unsigned long c) : m_value(c){}
  63. real_concept(long c) : m_value(c){}
  64. #if defined(__DECCXX) || defined(__SUNPRO_CC)
  65. real_concept(unsigned long long c) : m_value(static_cast<real_concept_base_type>(c)){}
  66. real_concept(long long c) : m_value(static_cast<real_concept_base_type>(c)){}
  67. #elif defined(BOOST_HAS_LONG_LONG)
  68. real_concept(boost::ulong_long_type c) : m_value(static_cast<real_concept_base_type>(c)){}
  69. real_concept(boost::long_long_type c) : m_value(static_cast<real_concept_base_type>(c)){}
  70. #elif defined(BOOST_HAS_MS_INT64)
  71. real_concept(unsigned __int64 c) : m_value(static_cast<real_concept_base_type>(c)){}
  72. real_concept(__int64 c) : m_value(static_cast<real_concept_base_type>(c)){}
  73. #endif
  74. real_concept(float c) : m_value(c){}
  75. real_concept(double c) : m_value(c){}
  76. real_concept(long double c) : m_value(c){}
  77. #ifdef BOOST_MATH_USE_FLOAT128
  78. real_concept(__float128 c) : m_value(c){}
  79. #endif
  80. // Assignment:
  81. real_concept& operator=(char c) { m_value = c; return *this; }
  82. real_concept& operator=(unsigned char c) { m_value = c; return *this; }
  83. real_concept& operator=(signed char c) { m_value = c; return *this; }
  84. #ifndef BOOST_NO_INTRINSIC_WCHAR_T
  85. real_concept& operator=(wchar_t c) { m_value = c; return *this; }
  86. #endif
  87. real_concept& operator=(short c) { m_value = c; return *this; }
  88. real_concept& operator=(unsigned short c) { m_value = c; return *this; }
  89. real_concept& operator=(int c) { m_value = c; return *this; }
  90. real_concept& operator=(unsigned int c) { m_value = c; return *this; }
  91. real_concept& operator=(long c) { m_value = c; return *this; }
  92. real_concept& operator=(unsigned long c) { m_value = c; return *this; }
  93. #ifdef BOOST_HAS_LONG_LONG
  94. real_concept& operator=(boost::long_long_type c) { m_value = static_cast<real_concept_base_type>(c); return *this; }
  95. real_concept& operator=(boost::ulong_long_type c) { m_value = static_cast<real_concept_base_type>(c); return *this; }
  96. #endif
  97. real_concept& operator=(float c) { m_value = c; return *this; }
  98. real_concept& operator=(double c) { m_value = c; return *this; }
  99. real_concept& operator=(long double c) { m_value = c; return *this; }
  100. // Access:
  101. real_concept_base_type value()const{ return m_value; }
  102. // Member arithmetic:
  103. real_concept& operator+=(const real_concept& other)
  104. { m_value += other.value(); return *this; }
  105. real_concept& operator-=(const real_concept& other)
  106. { m_value -= other.value(); return *this; }
  107. real_concept& operator*=(const real_concept& other)
  108. { m_value *= other.value(); return *this; }
  109. real_concept& operator/=(const real_concept& other)
  110. { m_value /= other.value(); return *this; }
  111. real_concept operator-()const
  112. { return -m_value; }
  113. real_concept const& operator+()const
  114. { return *this; }
  115. real_concept& operator++()
  116. { ++m_value; return *this; }
  117. real_concept& operator--()
  118. { --m_value; return *this; }
  119. private:
  120. real_concept_base_type m_value;
  121. };
  122. // Non-member arithmetic:
  123. inline real_concept operator+(const real_concept& a, const real_concept& b)
  124. {
  125. real_concept result(a);
  126. result += b;
  127. return result;
  128. }
  129. inline real_concept operator-(const real_concept& a, const real_concept& b)
  130. {
  131. real_concept result(a);
  132. result -= b;
  133. return result;
  134. }
  135. inline real_concept operator*(const real_concept& a, const real_concept& b)
  136. {
  137. real_concept result(a);
  138. result *= b;
  139. return result;
  140. }
  141. inline real_concept operator/(const real_concept& a, const real_concept& b)
  142. {
  143. real_concept result(a);
  144. result /= b;
  145. return result;
  146. }
  147. // Comparison:
  148. inline bool operator == (const real_concept& a, const real_concept& b)
  149. { return a.value() == b.value(); }
  150. inline bool operator != (const real_concept& a, const real_concept& b)
  151. { return a.value() != b.value();}
  152. inline bool operator < (const real_concept& a, const real_concept& b)
  153. { return a.value() < b.value(); }
  154. inline bool operator <= (const real_concept& a, const real_concept& b)
  155. { return a.value() <= b.value(); }
  156. inline bool operator > (const real_concept& a, const real_concept& b)
  157. { return a.value() > b.value(); }
  158. inline bool operator >= (const real_concept& a, const real_concept& b)
  159. { return a.value() >= b.value(); }
  160. // Non-member functions:
  161. inline real_concept acos(real_concept a)
  162. { return std::acos(a.value()); }
  163. inline real_concept cos(real_concept a)
  164. { return std::cos(a.value()); }
  165. inline real_concept asin(real_concept a)
  166. { return std::asin(a.value()); }
  167. inline real_concept atan(real_concept a)
  168. { return std::atan(a.value()); }
  169. inline real_concept atan2(real_concept a, real_concept b)
  170. { return std::atan2(a.value(), b.value()); }
  171. inline real_concept ceil(real_concept a)
  172. { return std::ceil(a.value()); }
  173. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  174. // I've seen std::fmod(long double) crash on some platforms
  175. // so use fmodl instead:
  176. #ifdef _WIN32_WCE
  177. //
  178. // Ugly workaround for macro fmodl:
  179. //
  180. inline long double call_fmodl(long double a, long double b)
  181. { return fmodl(a, b); }
  182. inline real_concept fmod(real_concept a, real_concept b)
  183. { return call_fmodl(a.value(), b.value()); }
  184. #else
  185. inline real_concept fmod(real_concept a, real_concept b)
  186. { return fmodl(a.value(), b.value()); }
  187. #endif
  188. #endif
  189. inline real_concept cosh(real_concept a)
  190. { return std::cosh(a.value()); }
  191. inline real_concept exp(real_concept a)
  192. { return std::exp(a.value()); }
  193. inline real_concept fabs(real_concept a)
  194. { return std::fabs(a.value()); }
  195. inline real_concept abs(real_concept a)
  196. { return std::abs(a.value()); }
  197. inline real_concept floor(real_concept a)
  198. { return std::floor(a.value()); }
  199. inline real_concept modf(real_concept a, real_concept* ipart)
  200. {
  201. real_concept_base_type ip;
  202. real_concept_base_type result = std::modf(a.value(), &ip);
  203. *ipart = ip;
  204. return result;
  205. }
  206. inline real_concept frexp(real_concept a, int* expon)
  207. { return std::frexp(a.value(), expon); }
  208. inline real_concept ldexp(real_concept a, int expon)
  209. { return std::ldexp(a.value(), expon); }
  210. inline real_concept log(real_concept a)
  211. { return std::log(a.value()); }
  212. inline real_concept log10(real_concept a)
  213. { return std::log10(a.value()); }
  214. inline real_concept tan(real_concept a)
  215. { return std::tan(a.value()); }
  216. inline real_concept pow(real_concept a, real_concept b)
  217. { return std::pow(a.value(), b.value()); }
  218. #if !defined(__SUNPRO_CC)
  219. inline real_concept pow(real_concept a, int b)
  220. { return std::pow(a.value(), b); }
  221. #else
  222. inline real_concept pow(real_concept a, int b)
  223. { return std::pow(a.value(), static_cast<real_concept_base_type>(b)); }
  224. #endif
  225. inline real_concept sin(real_concept a)
  226. { return std::sin(a.value()); }
  227. inline real_concept sinh(real_concept a)
  228. { return std::sinh(a.value()); }
  229. inline real_concept sqrt(real_concept a)
  230. { return std::sqrt(a.value()); }
  231. inline real_concept tanh(real_concept a)
  232. { return std::tanh(a.value()); }
  233. //
  234. // Conversion and truncation routines:
  235. //
  236. template <class Policy>
  237. inline int iround(const concepts::real_concept& v, const Policy& pol)
  238. { return boost::math::iround(v.value(), pol); }
  239. inline int iround(const concepts::real_concept& v)
  240. { return boost::math::iround(v.value(), policies::policy<>()); }
  241. template <class Policy>
  242. inline long lround(const concepts::real_concept& v, const Policy& pol)
  243. { return boost::math::lround(v.value(), pol); }
  244. inline long lround(const concepts::real_concept& v)
  245. { return boost::math::lround(v.value(), policies::policy<>()); }
  246. #ifdef BOOST_HAS_LONG_LONG
  247. template <class Policy>
  248. inline boost::long_long_type llround(const concepts::real_concept& v, const Policy& pol)
  249. { return boost::math::llround(v.value(), pol); }
  250. inline boost::long_long_type llround(const concepts::real_concept& v)
  251. { return boost::math::llround(v.value(), policies::policy<>()); }
  252. #endif
  253. template <class Policy>
  254. inline int itrunc(const concepts::real_concept& v, const Policy& pol)
  255. { return boost::math::itrunc(v.value(), pol); }
  256. inline int itrunc(const concepts::real_concept& v)
  257. { return boost::math::itrunc(v.value(), policies::policy<>()); }
  258. template <class Policy>
  259. inline long ltrunc(const concepts::real_concept& v, const Policy& pol)
  260. { return boost::math::ltrunc(v.value(), pol); }
  261. inline long ltrunc(const concepts::real_concept& v)
  262. { return boost::math::ltrunc(v.value(), policies::policy<>()); }
  263. #ifdef BOOST_HAS_LONG_LONG
  264. template <class Policy>
  265. inline boost::long_long_type lltrunc(const concepts::real_concept& v, const Policy& pol)
  266. { return boost::math::lltrunc(v.value(), pol); }
  267. inline boost::long_long_type lltrunc(const concepts::real_concept& v)
  268. { return boost::math::lltrunc(v.value(), policies::policy<>()); }
  269. #endif
  270. // Streaming:
  271. template <class charT, class traits>
  272. inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const real_concept& a)
  273. {
  274. return os << a.value();
  275. }
  276. template <class charT, class traits>
  277. inline std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& is, real_concept& a)
  278. {
  279. #if defined(BOOST_MSVC) && defined(__SGI_STL_PORT)
  280. //
  281. // STLPort 5.1.4 has a problem reading long doubles from strings,
  282. // see http://sourceforge.net/tracker/index.php?func=detail&aid=1811043&group_id=146814&atid=766244
  283. //
  284. double v;
  285. is >> v;
  286. a = v;
  287. return is;
  288. #elif defined(__SGI_STL_PORT) || defined(_RWSTD_VER) || defined(__LIBCOMO__)
  289. std::string s;
  290. real_concept_base_type d;
  291. is >> s;
  292. std::sscanf(s.c_str(), "%Lf", &d);
  293. a = d;
  294. return is;
  295. #else
  296. real_concept_base_type v;
  297. is >> v;
  298. a = v;
  299. return is;
  300. #endif
  301. }
  302. } // namespace concepts
  303. namespace tools
  304. {
  305. template <>
  306. inline concepts::real_concept make_big_value<concepts::real_concept>(long double val, const char* , mpl::false_ const&, mpl::false_ const&)
  307. {
  308. return val; // Can't use lexical_cast here, sometimes it fails....
  309. }
  310. template <>
  311. inline concepts::real_concept max_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
  312. {
  313. return max_value<concepts::real_concept_base_type>();
  314. }
  315. template <>
  316. inline concepts::real_concept min_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
  317. {
  318. return min_value<concepts::real_concept_base_type>();
  319. }
  320. template <>
  321. inline concepts::real_concept log_max_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
  322. {
  323. return log_max_value<concepts::real_concept_base_type>();
  324. }
  325. template <>
  326. inline concepts::real_concept log_min_value<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
  327. {
  328. return log_min_value<concepts::real_concept_base_type>();
  329. }
  330. template <>
  331. inline concepts::real_concept epsilon<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
  332. {
  333. #ifdef __SUNPRO_CC
  334. return std::numeric_limits<concepts::real_concept_base_type>::epsilon();
  335. #else
  336. return tools::epsilon<concepts::real_concept_base_type>();
  337. #endif
  338. }
  339. template <>
  340. inline int digits<concepts::real_concept>(BOOST_MATH_EXPLICIT_TEMPLATE_TYPE_SPEC(concepts::real_concept))
  341. {
  342. // Assume number of significand bits is same as real_concept_base_type,
  343. // unless std::numeric_limits<T>::is_specialized to provide digits.
  344. return tools::digits<concepts::real_concept_base_type>();
  345. // Note that if numeric_limits real concept is NOT specialized to provide digits10
  346. // (or max_digits10) then the default precision of 6 decimal digits will be used
  347. // by Boost test (giving misleading error messages like
  348. // "difference between {9.79796} and {9.79796} exceeds 5.42101e-19%"
  349. // and by Boost lexical cast and serialization causing loss of accuracy.
  350. }
  351. } // namespace tools
  352. #if defined(__SGI_STL_PORT) || defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS)
  353. //
  354. // We shouldn't really need these type casts any more, but there are some
  355. // STLport iostream bugs we work around by using them....
  356. //
  357. namespace tools
  358. {
  359. // real_cast converts from T to integer and narrower floating-point types.
  360. // Convert from T to integer types.
  361. template <>
  362. inline unsigned int real_cast<unsigned int, concepts::real_concept>(concepts::real_concept r)
  363. {
  364. return static_cast<unsigned int>(r.value());
  365. }
  366. template <>
  367. inline int real_cast<int, concepts::real_concept>(concepts::real_concept r)
  368. {
  369. return static_cast<int>(r.value());
  370. }
  371. template <>
  372. inline long real_cast<long, concepts::real_concept>(concepts::real_concept r)
  373. {
  374. return static_cast<long>(r.value());
  375. }
  376. // Converts from T to narrower floating-point types, float, double & long double.
  377. template <>
  378. inline float real_cast<float, concepts::real_concept>(concepts::real_concept r)
  379. {
  380. return static_cast<float>(r.value());
  381. }
  382. template <>
  383. inline double real_cast<double, concepts::real_concept>(concepts::real_concept r)
  384. {
  385. return static_cast<double>(r.value());
  386. }
  387. template <>
  388. inline long double real_cast<long double, concepts::real_concept>(concepts::real_concept r)
  389. {
  390. return r.value();
  391. }
  392. } // STLPort
  393. #endif
  394. #if BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
  395. //
  396. // For some strange reason ADL sometimes fails to find the
  397. // correct overloads, unless we bring these declarations into scope:
  398. //
  399. using concepts::itrunc;
  400. using concepts::iround;
  401. #endif
  402. } // namespace math
  403. } // namespace boost
  404. #endif // BOOST_MATH_REAL_CONCEPT_HPP