real_concept.hpp 17 KB

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