You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2922 lines
100 KiB

  1. /*
  2. Multi-precision floating point number class for C++.
  3. Based on MPFR library: http://mpfr.org
  4. Project homepage: http://www.holoborodko.com/pavel/mpfr
  5. Contact e-mail: pavel@holoborodko.com
  6. Copyright (c) 2008-2012 Pavel Holoborodko
  7. Contributors:
  8. Dmitriy Gubanov, Konstantin Holoborodko, Brian Gladman,
  9. Helmut Jarausch, Fokko Beekhof, Ulrich Mutze, Heinz van Saanen,
  10. Pere Constans, Peter van Hoof, Gael Guennebaud, Tsai Chia Cheng,
  11. Alexei Zubanov, Jauhien Piatlicki, Victor Berger, John Westwood.
  12. ****************************************************************************
  13. This library is free software; you can redistribute it and/or
  14. modify it under the terms of the GNU Lesser General Public
  15. License as published by the Free Software Foundation; either
  16. version 2.1 of the License, or (at your option) any later version.
  17. This library is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. Lesser General Public License for more details.
  21. You should have received a copy of the GNU Lesser General Public
  22. License along with this library; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. ****************************************************************************
  25. Redistribution and use in source and binary forms, with or without
  26. modification, are permitted provided that the following conditions
  27. are met:
  28. 1. Redistributions of source code must retain the above copyright
  29. notice, this list of conditions and the following disclaimer.
  30. 2. Redistributions in binary form must reproduce the above copyright
  31. notice, this list of conditions and the following disclaimer in the
  32. documentation and/or other materials provided with the distribution.
  33. 3. The name of the author may not be used to endorse or promote products
  34. derived from this software without specific prior written permission.
  35. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  36. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  38. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  39. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  40. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  41. OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  42. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  43. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  44. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  45. SUCH DAMAGE.
  46. */
  47. #ifndef __MPREAL_H__
  48. #define __MPREAL_H__
  49. #include <string>
  50. #include <iostream>
  51. #include <sstream>
  52. #include <stdexcept>
  53. #include <cfloat>
  54. #include <cmath>
  55. #include <limits>
  56. // Options
  57. #define MPREAL_HAVE_INT64_SUPPORT // Enable int64_t support if possible. Available only for MSVC 2010 & GCC.
  58. #define MPREAL_HAVE_MSVC_DEBUGVIEW // Enable Debugger Visualizer for "Debug" builds in MSVC.
  59. // Detect compiler using signatures from http://predef.sourceforge.net/
  60. #if defined(__GNUC__) && defined(__INTEL_COMPILER)
  61. #define IsInf(x) isinf(x) // Intel ICC compiler on Linux
  62. #elif defined(_MSC_VER) // Microsoft Visual C++
  63. #define IsInf(x) (!_finite(x))
  64. #else
  65. #define IsInf(x) std::isinf(x) // GNU C/C++ (and/or other compilers), just hope for C99 conformance
  66. #endif
  67. #if defined(MPREAL_HAVE_INT64_SUPPORT)
  68. #define MPFR_USE_INTMAX_T // Should be defined before mpfr.h
  69. #if defined(_MSC_VER) // MSVC + Windows
  70. #if (_MSC_VER >= 1600)
  71. #include <stdint.h> // <stdint.h> is available only in msvc2010!
  72. #else // MPFR relies on intmax_t which is available only in msvc2010
  73. #undef MPREAL_HAVE_INT64_SUPPORT // Besides, MPFR & MPIR have to be compiled with msvc2010
  74. #undef MPFR_USE_INTMAX_T // Since we cannot detect this, disable x64 by default
  75. // Someone should change this manually if needed.
  76. #endif
  77. #elif defined (__GNUC__) && defined(__linux__)
  78. #if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(__ia64) || defined(__itanium__) || defined(_M_IA64)
  79. #undef MPREAL_HAVE_INT64_SUPPORT // Remove all shaman dances for x64 builds since
  80. #undef MPFR_USE_INTMAX_T // GCC already supports x64 as of "long int" is 64-bit integer, nothing left to do
  81. #else
  82. #include <stdint.h> // use int64_t, uint64_t otherwise
  83. #endif
  84. #else
  85. #include <stdint.h> // rely on int64_t, uint64_t in all other cases, Mac OSX, etc.
  86. #endif
  87. #endif
  88. #if defined(MPREAL_HAVE_MSVC_DEBUGVIEW) && defined(_MSC_VER) && defined(_DEBUG)
  89. #define MPREAL_MSVC_DEBUGVIEW_CODE DebugView = toString();
  90. #define MPREAL_MSVC_DEBUGVIEW_DATA std::string DebugView;
  91. #else
  92. #define MPREAL_MSVC_DEBUGVIEW_CODE
  93. #define MPREAL_MSVC_DEBUGVIEW_DATA
  94. #endif
  95. #include <mpfr.h>
  96. #if (MPFR_VERSION < MPFR_VERSION_NUM(3,0,0))
  97. #include <cstdlib> // Needed for random()
  98. #endif
  99. // Less important options
  100. #define MPREAL_DOUBLE_BITS_OVERFLOW -1 // Triggers overflow exception during conversion to double if mpreal
  101. // cannot fit in MPREAL_DOUBLE_BITS_OVERFLOW bits
  102. // = -1 disables overflow checks (default)
  103. #if defined(__GNUC__)
  104. #define MPREAL_PERMISSIVE_EXPR __extension__
  105. #else
  106. #define MPREAL_PERMISSIVE_EXPR
  107. #endif
  108. namespace mpfr {
  109. class mpreal {
  110. private:
  111. mpfr_t mp;
  112. public:
  113. // Get default rounding mode & precision
  114. inline static mp_rnd_t get_default_rnd() { return (mp_rnd_t)(mpfr_get_default_rounding_mode()); }
  115. inline static mp_prec_t get_default_prec() { return mpfr_get_default_prec(); }
  116. // Constructors && type conversions
  117. mpreal();
  118. mpreal(const mpreal& u);
  119. mpreal(const mpfr_t u);
  120. mpreal(const mpf_t u);
  121. mpreal(const mpz_t u, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t mode = mpreal::get_default_rnd());
  122. mpreal(const mpq_t u, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t mode = mpreal::get_default_rnd());
  123. mpreal(const double u, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t mode = mpreal::get_default_rnd());
  124. mpreal(const long double u, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t mode = mpreal::get_default_rnd());
  125. mpreal(const unsigned long int u, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t mode = mpreal::get_default_rnd());
  126. mpreal(const unsigned int u, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t mode = mpreal::get_default_rnd());
  127. mpreal(const long int u, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t mode = mpreal::get_default_rnd());
  128. mpreal(const int u, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t mode = mpreal::get_default_rnd());
  129. #if defined (MPREAL_HAVE_INT64_SUPPORT)
  130. mpreal(const uint64_t u, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t mode = mpreal::get_default_rnd());
  131. mpreal(const int64_t u, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t mode = mpreal::get_default_rnd());
  132. #endif
  133. mpreal(const char* s, mp_prec_t prec = mpreal::get_default_prec(), int base = 10, mp_rnd_t mode = mpreal::get_default_rnd());
  134. mpreal(const std::string& s, mp_prec_t prec = mpreal::get_default_prec(), int base = 10, mp_rnd_t mode = mpreal::get_default_rnd());
  135. ~mpreal();
  136. // Operations
  137. // =
  138. // +, -, *, /, ++, --, <<, >>
  139. // *=, +=, -=, /=,
  140. // <, >, ==, <=, >=
  141. // =
  142. mpreal& operator=(const mpreal& v);
  143. mpreal& operator=(const mpf_t v);
  144. mpreal& operator=(const mpz_t v);
  145. mpreal& operator=(const mpq_t v);
  146. mpreal& operator=(const long double v);
  147. mpreal& operator=(const double v);
  148. mpreal& operator=(const unsigned long int v);
  149. mpreal& operator=(const unsigned int v);
  150. mpreal& operator=(const long int v);
  151. mpreal& operator=(const int v);
  152. mpreal& operator=(const char* s);
  153. mpreal& operator=(const std::string& s);
  154. // +
  155. mpreal& operator+=(const mpreal& v);
  156. mpreal& operator+=(const mpf_t v);
  157. mpreal& operator+=(const mpz_t v);
  158. mpreal& operator+=(const mpq_t v);
  159. mpreal& operator+=(const long double u);
  160. mpreal& operator+=(const double u);
  161. mpreal& operator+=(const unsigned long int u);
  162. mpreal& operator+=(const unsigned int u);
  163. mpreal& operator+=(const long int u);
  164. mpreal& operator+=(const int u);
  165. #if defined (MPREAL_HAVE_INT64_SUPPORT)
  166. mpreal& operator+=(const int64_t u);
  167. mpreal& operator+=(const uint64_t u);
  168. mpreal& operator-=(const int64_t u);
  169. mpreal& operator-=(const uint64_t u);
  170. mpreal& operator*=(const int64_t u);
  171. mpreal& operator*=(const uint64_t u);
  172. mpreal& operator/=(const int64_t u);
  173. mpreal& operator/=(const uint64_t u);
  174. #endif
  175. const mpreal operator+() const;
  176. mpreal& operator++ ();
  177. const mpreal operator++ (int);
  178. // -
  179. mpreal& operator-=(const mpreal& v);
  180. mpreal& operator-=(const mpz_t v);
  181. mpreal& operator-=(const mpq_t v);
  182. mpreal& operator-=(const long double u);
  183. mpreal& operator-=(const double u);
  184. mpreal& operator-=(const unsigned long int u);
  185. mpreal& operator-=(const unsigned int u);
  186. mpreal& operator-=(const long int u);
  187. mpreal& operator-=(const int u);
  188. const mpreal operator-() const;
  189. friend const mpreal operator-(const unsigned long int b, const mpreal& a);
  190. friend const mpreal operator-(const unsigned int b, const mpreal& a);
  191. friend const mpreal operator-(const long int b, const mpreal& a);
  192. friend const mpreal operator-(const int b, const mpreal& a);
  193. friend const mpreal operator-(const double b, const mpreal& a);
  194. mpreal& operator-- ();
  195. const mpreal operator-- (int);
  196. // *
  197. mpreal& operator*=(const mpreal& v);
  198. mpreal& operator*=(const mpz_t v);
  199. mpreal& operator*=(const mpq_t v);
  200. mpreal& operator*=(const long double v);
  201. mpreal& operator*=(const double v);
  202. mpreal& operator*=(const unsigned long int v);
  203. mpreal& operator*=(const unsigned int v);
  204. mpreal& operator*=(const long int v);
  205. mpreal& operator*=(const int v);
  206. // /
  207. mpreal& operator/=(const mpreal& v);
  208. mpreal& operator/=(const mpz_t v);
  209. mpreal& operator/=(const mpq_t v);
  210. mpreal& operator/=(const long double v);
  211. mpreal& operator/=(const double v);
  212. mpreal& operator/=(const unsigned long int v);
  213. mpreal& operator/=(const unsigned int v);
  214. mpreal& operator/=(const long int v);
  215. mpreal& operator/=(const int v);
  216. friend const mpreal operator/(const unsigned long int b, const mpreal& a);
  217. friend const mpreal operator/(const unsigned int b, const mpreal& a);
  218. friend const mpreal operator/(const long int b, const mpreal& a);
  219. friend const mpreal operator/(const int b, const mpreal& a);
  220. friend const mpreal operator/(const double b, const mpreal& a);
  221. //<<= Fast Multiplication by 2^u
  222. mpreal& operator<<=(const unsigned long int u);
  223. mpreal& operator<<=(const unsigned int u);
  224. mpreal& operator<<=(const long int u);
  225. mpreal& operator<<=(const int u);
  226. //>>= Fast Division by 2^u
  227. mpreal& operator>>=(const unsigned long int u);
  228. mpreal& operator>>=(const unsigned int u);
  229. mpreal& operator>>=(const long int u);
  230. mpreal& operator>>=(const int u);
  231. // Boolean Operators
  232. friend bool operator > (const mpreal& a, const mpreal& b);
  233. friend bool operator >= (const mpreal& a, const mpreal& b);
  234. friend bool operator < (const mpreal& a, const mpreal& b);
  235. friend bool operator <= (const mpreal& a, const mpreal& b);
  236. friend bool operator == (const mpreal& a, const mpreal& b);
  237. friend bool operator != (const mpreal& a, const mpreal& b);
  238. // Optimized specializations for boolean operators
  239. friend bool operator == (const mpreal& a, const unsigned long int b);
  240. friend bool operator == (const mpreal& a, const unsigned int b);
  241. friend bool operator == (const mpreal& a, const long int b);
  242. friend bool operator == (const mpreal& a, const int b);
  243. friend bool operator == (const mpreal& a, const long double b);
  244. friend bool operator == (const mpreal& a, const double b);
  245. // Type Conversion operators
  246. long toLong (mp_rnd_t mode = GMP_RNDZ) const;
  247. unsigned long toULong (mp_rnd_t mode = GMP_RNDZ) const;
  248. double toDouble (mp_rnd_t mode = GMP_RNDN) const;
  249. long double toLDouble (mp_rnd_t mode = GMP_RNDN) const;
  250. #if defined (MPREAL_HAVE_INT64_SUPPORT)
  251. int64_t toInt64 (mp_rnd_t mode = GMP_RNDZ) const;
  252. uint64_t toUInt64 (mp_rnd_t mode = GMP_RNDZ) const;
  253. #endif
  254. // Get raw pointers so that mpreal can be directly used in raw mpfr_* functions
  255. ::mpfr_ptr mpfr_ptr();
  256. ::mpfr_srcptr mpfr_ptr() const;
  257. ::mpfr_srcptr mpfr_srcptr() const;
  258. // Convert mpreal to string with n significant digits in base b
  259. // n = 0 -> convert with the maximum available digits
  260. std::string toString(int n = 0, int b = 10, mp_rnd_t mode = mpreal::get_default_rnd()) const;
  261. #if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
  262. std::string toString(const std::string& format) const;
  263. #endif
  264. // Math Functions
  265. friend const mpreal sqr (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  266. friend const mpreal sqrt(const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  267. friend const mpreal sqrt(const unsigned long int v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  268. friend const mpreal cbrt(const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  269. friend const mpreal root(const mpreal& v, unsigned long int k, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  270. friend const mpreal pow (const mpreal& a, const mpreal& b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  271. friend const mpreal pow (const mpreal& a, const mpz_t b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  272. friend const mpreal pow (const mpreal& a, const unsigned long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  273. friend const mpreal pow (const mpreal& a, const long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  274. friend const mpreal pow (const unsigned long int a, const mpreal& b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  275. friend const mpreal pow (const unsigned long int a, const unsigned long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  276. friend const mpreal fabs(const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  277. friend const mpreal abs(const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  278. friend const mpreal dim(const mpreal& a, const mpreal& b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  279. friend inline const mpreal mul_2ui(const mpreal& v, unsigned long int k, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  280. friend inline const mpreal mul_2si(const mpreal& v, long int k, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  281. friend inline const mpreal div_2ui(const mpreal& v, unsigned long int k, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  282. friend inline const mpreal div_2si(const mpreal& v, long int k, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  283. friend int cmpabs(const mpreal& a,const mpreal& b);
  284. friend const mpreal log (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  285. friend const mpreal log2 (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  286. friend const mpreal log10(const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  287. friend const mpreal exp (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  288. friend const mpreal exp2 (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  289. friend const mpreal exp10(const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  290. friend const mpreal log1p(const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  291. friend const mpreal expm1(const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  292. friend const mpreal cos(const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  293. friend const mpreal sin(const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  294. friend const mpreal tan(const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  295. friend const mpreal sec(const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  296. friend const mpreal csc(const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  297. friend const mpreal cot(const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  298. friend int sin_cos(mpreal& s, mpreal& c, const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  299. friend const mpreal acos (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  300. friend const mpreal asin (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  301. friend const mpreal atan (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  302. friend const mpreal atan2 (const mpreal& y, const mpreal& x, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  303. friend const mpreal acot (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  304. friend const mpreal asec (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  305. friend const mpreal acsc (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  306. friend const mpreal cosh (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  307. friend const mpreal sinh (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  308. friend const mpreal tanh (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  309. friend const mpreal sech (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  310. friend const mpreal csch (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  311. friend const mpreal coth (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  312. friend const mpreal acosh (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  313. friend const mpreal asinh (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  314. friend const mpreal atanh (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  315. friend const mpreal acoth (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  316. friend const mpreal asech (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  317. friend const mpreal acsch (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  318. friend const mpreal hypot (const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  319. friend const mpreal fac_ui (unsigned long int v, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  320. friend const mpreal eint (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  321. friend const mpreal gamma (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  322. friend const mpreal lngamma (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  323. friend const mpreal lgamma (const mpreal& v, int *signp = 0, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  324. friend const mpreal zeta (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  325. friend const mpreal erf (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  326. friend const mpreal erfc (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  327. friend const mpreal besselj0 (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  328. friend const mpreal besselj1 (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  329. friend const mpreal besseljn (long n, const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  330. friend const mpreal bessely0 (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  331. friend const mpreal bessely1 (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  332. friend const mpreal besselyn (long n, const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  333. friend const mpreal fma (const mpreal& v1, const mpreal& v2, const mpreal& v3, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  334. friend const mpreal fms (const mpreal& v1, const mpreal& v2, const mpreal& v3, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  335. friend const mpreal agm (const mpreal& v1, const mpreal& v2, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  336. friend const mpreal sum (const mpreal tab[], unsigned long int n, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  337. friend int sgn(const mpreal& v); // returns -1 or +1
  338. // MPFR 2.4.0 Specifics
  339. #if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
  340. friend int sinh_cosh (mpreal& s, mpreal& c, const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  341. friend const mpreal li2 (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  342. friend const mpreal fmod (const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  343. friend const mpreal rec_sqrt (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  344. // MATLAB's semantic equivalents
  345. friend const mpreal rem (const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode = mpreal::get_default_rnd()); // Remainder after division
  346. friend const mpreal mod (const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode = mpreal::get_default_rnd()); // Modulus after division
  347. #endif
  348. // MPFR 3.0.0 Specifics
  349. #if (MPFR_VERSION >= MPFR_VERSION_NUM(3,0,0))
  350. friend const mpreal digamma (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  351. friend const mpreal ai (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  352. friend const mpreal urandom (gmp_randstate_t& state, mp_rnd_t rnd_mode = mpreal::get_default_rnd()); // use gmp_randinit_default() to init state, gmp_randclear() to clear
  353. #endif
  354. // Uniformly distributed random number generation in [0,1] using
  355. // Mersenne-Twister algorithm by default.
  356. // Use parameter to setup seed, e.g.: random((unsigned)time(NULL))
  357. // Check urandom() for more precise control.
  358. friend const mpreal random(unsigned int seed = 0);
  359. // Exponent and mantissa manipulation
  360. friend const mpreal frexp(const mpreal& v, mp_exp_t* exp);
  361. friend const mpreal ldexp(const mpreal& v, mp_exp_t exp);
  362. // Splits mpreal value into fractional and integer parts.
  363. // Returns fractional part and stores integer part in n.
  364. friend const mpreal modf(const mpreal& v, mpreal& n);
  365. // Constants
  366. // don't forget to call mpfr_free_cache() for every thread where you are using const-functions
  367. friend const mpreal const_log2 (mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  368. friend const mpreal const_pi (mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  369. friend const mpreal const_euler (mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  370. friend const mpreal const_catalan (mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  371. // returns +inf iff sign>=0 otherwise -inf
  372. friend const mpreal const_infinity(int sign = 1, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  373. // Output/ Input
  374. friend std::ostream& operator<<(std::ostream& os, const mpreal& v);
  375. friend std::istream& operator>>(std::istream& is, mpreal& v);
  376. // Integer Related Functions
  377. friend const mpreal rint (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  378. friend const mpreal ceil (const mpreal& v);
  379. friend const mpreal floor(const mpreal& v);
  380. friend const mpreal round(const mpreal& v);
  381. friend const mpreal trunc(const mpreal& v);
  382. friend const mpreal rint_ceil (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  383. friend const mpreal rint_floor (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  384. friend const mpreal rint_round (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  385. friend const mpreal rint_trunc (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  386. friend const mpreal frac (const mpreal& v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  387. friend const mpreal remainder ( const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  388. friend const mpreal remquo (long* q, const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  389. // Miscellaneous Functions
  390. friend const mpreal nexttoward (const mpreal& x, const mpreal& y);
  391. friend const mpreal nextabove (const mpreal& x);
  392. friend const mpreal nextbelow (const mpreal& x);
  393. // use gmp_randinit_default() to init state, gmp_randclear() to clear
  394. friend const mpreal urandomb (gmp_randstate_t& state);
  395. // MPFR < 2.4.2 Specifics
  396. #if (MPFR_VERSION <= MPFR_VERSION_NUM(2,4,2))
  397. friend const mpreal random2 (mp_size_t size, mp_exp_t exp);
  398. #endif
  399. // Instance Checkers
  400. friend bool isnan (const mpreal& v);
  401. friend bool isinf (const mpreal& v);
  402. friend bool isfinite (const mpreal& v);
  403. friend bool isnum (const mpreal& v);
  404. friend bool iszero (const mpreal& v);
  405. friend bool isint (const mpreal& v);
  406. #if (MPFR_VERSION >= MPFR_VERSION_NUM(3,0,0))
  407. friend bool isregular(const mpreal& v);
  408. #endif
  409. // Set/Get instance properties
  410. inline mp_prec_t get_prec() const;
  411. inline void set_prec(mp_prec_t prec, mp_rnd_t rnd_mode = get_default_rnd()); // Change precision with rounding mode
  412. // Aliases for get_prec(), set_prec() - needed for compatibility with std::complex<mpreal> interface
  413. inline mpreal& setPrecision(int Precision, mp_rnd_t RoundingMode = get_default_rnd());
  414. inline int getPrecision() const;
  415. // Set mpreal to +/- inf, NaN, +/-0
  416. mpreal& setInf (int Sign = +1);
  417. mpreal& setNan ();
  418. mpreal& setZero (int Sign = +1);
  419. mpreal& setSign (int Sign, mp_rnd_t RoundingMode = get_default_rnd());
  420. //Exponent
  421. mp_exp_t get_exp();
  422. int set_exp(mp_exp_t e);
  423. int check_range (int t, mp_rnd_t rnd_mode = get_default_rnd());
  424. int subnormalize (int t,mp_rnd_t rnd_mode = get_default_rnd());
  425. // Inexact conversion from float
  426. inline bool fits_in_bits(double x, int n);
  427. // Set/Get global properties
  428. static void set_default_prec(mp_prec_t prec);
  429. static void set_default_rnd(mp_rnd_t rnd_mode);
  430. static mp_exp_t get_emin (void);
  431. static mp_exp_t get_emax (void);
  432. static mp_exp_t get_emin_min (void);
  433. static mp_exp_t get_emin_max (void);
  434. static mp_exp_t get_emax_min (void);
  435. static mp_exp_t get_emax_max (void);
  436. static int set_emin (mp_exp_t exp);
  437. static int set_emax (mp_exp_t exp);
  438. // Efficient swapping of two mpreal values - needed for std algorithms
  439. friend void swap(mpreal& x, mpreal& y);
  440. friend const mpreal fmax(const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  441. friend const mpreal fmin(const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  442. private:
  443. // Human friendly Debug Preview in Visual Studio.
  444. // Put one of these lines:
  445. //
  446. // mpfr::mpreal=<DebugView> ; Show value only
  447. // mpfr::mpreal=<DebugView>, <mp[0]._mpfr_prec,u>bits ; Show value & precision
  448. //
  449. // at the beginning of
  450. // [Visual Studio Installation Folder]\Common7\Packages\Debugger\autoexp.dat
  451. MPREAL_MSVC_DEBUGVIEW_DATA
  452. };
  453. //////////////////////////////////////////////////////////////////////////
  454. // Exceptions
  455. class conversion_overflow : public std::exception {
  456. public:
  457. std::string why() { return "inexact conversion from floating point"; }
  458. };
  459. //////////////////////////////////////////////////////////////////////////
  460. // Constructors & converters
  461. // Default constructor: creates mp number and initializes it to 0.
  462. inline mpreal::mpreal()
  463. {
  464. mpfr_init2(mp,mpreal::get_default_prec());
  465. mpfr_set_ui(mp,0,mpreal::get_default_rnd());
  466. MPREAL_MSVC_DEBUGVIEW_CODE;
  467. }
  468. inline mpreal::mpreal(const mpreal& u)
  469. {
  470. mpfr_init2(mp,mpfr_get_prec(u.mp));
  471. mpfr_set(mp,u.mp,mpreal::get_default_rnd());
  472. MPREAL_MSVC_DEBUGVIEW_CODE;
  473. }
  474. inline mpreal::mpreal(const mpfr_t u)
  475. {
  476. mpfr_init2(mp,mpfr_get_prec(u));
  477. mpfr_set(mp,u,mpreal::get_default_rnd());
  478. MPREAL_MSVC_DEBUGVIEW_CODE;
  479. }
  480. inline mpreal::mpreal(const mpf_t u)
  481. {
  482. mpfr_init2(mp,(mp_prec_t) mpf_get_prec(u)); // (gmp: mp_bitcnt_t) unsigned long -> long (mpfr: mp_prec_t)
  483. mpfr_set_f(mp,u,mpreal::get_default_rnd());
  484. MPREAL_MSVC_DEBUGVIEW_CODE;
  485. }
  486. inline mpreal::mpreal(const mpz_t u, mp_prec_t prec, mp_rnd_t mode)
  487. {
  488. mpfr_init2(mp,prec);
  489. mpfr_set_z(mp,u,mode);
  490. MPREAL_MSVC_DEBUGVIEW_CODE;
  491. }
  492. inline mpreal::mpreal(const mpq_t u, mp_prec_t prec, mp_rnd_t mode)
  493. {
  494. mpfr_init2(mp,prec);
  495. mpfr_set_q(mp,u,mode);
  496. MPREAL_MSVC_DEBUGVIEW_CODE;
  497. }
  498. inline mpreal::mpreal(const double u, mp_prec_t prec, mp_rnd_t mode)
  499. {
  500. mpfr_init2(mp, prec);
  501. #if (MPREAL_DOUBLE_BITS_OVERFLOW > -1)
  502. if(fits_in_bits(u, MPREAL_DOUBLE_BITS_OVERFLOW))
  503. {
  504. mpfr_set_d(mp, u, mode);
  505. }else
  506. throw conversion_overflow();
  507. #else
  508. mpfr_set_d(mp, u, mode);
  509. #endif
  510. MPREAL_MSVC_DEBUGVIEW_CODE;
  511. }
  512. inline mpreal::mpreal(const long double u, mp_prec_t prec, mp_rnd_t mode)
  513. {
  514. mpfr_init2(mp,prec);
  515. mpfr_set_ld(mp,u,mode);
  516. MPREAL_MSVC_DEBUGVIEW_CODE;
  517. }
  518. inline mpreal::mpreal(const unsigned long int u, mp_prec_t prec, mp_rnd_t mode)
  519. {
  520. mpfr_init2(mp,prec);
  521. mpfr_set_ui(mp,u,mode);
  522. MPREAL_MSVC_DEBUGVIEW_CODE;
  523. }
  524. inline mpreal::mpreal(const unsigned int u, mp_prec_t prec, mp_rnd_t mode)
  525. {
  526. mpfr_init2(mp,prec);
  527. mpfr_set_ui(mp,u,mode);
  528. MPREAL_MSVC_DEBUGVIEW_CODE;
  529. }
  530. inline mpreal::mpreal(const long int u, mp_prec_t prec, mp_rnd_t mode)
  531. {
  532. mpfr_init2(mp,prec);
  533. mpfr_set_si(mp,u,mode);
  534. MPREAL_MSVC_DEBUGVIEW_CODE;
  535. }
  536. inline mpreal::mpreal(const int u, mp_prec_t prec, mp_rnd_t mode)
  537. {
  538. mpfr_init2(mp,prec);
  539. mpfr_set_si(mp,u,mode);
  540. MPREAL_MSVC_DEBUGVIEW_CODE;
  541. }
  542. #if defined (MPREAL_HAVE_INT64_SUPPORT)
  543. inline mpreal::mpreal(const uint64_t u, mp_prec_t prec, mp_rnd_t mode)
  544. {
  545. mpfr_init2(mp,prec);
  546. mpfr_set_uj(mp, u, mode);
  547. MPREAL_MSVC_DEBUGVIEW_CODE;
  548. }
  549. inline mpreal::mpreal(const int64_t u, mp_prec_t prec, mp_rnd_t mode)
  550. {
  551. mpfr_init2(mp,prec);
  552. mpfr_set_sj(mp, u, mode);
  553. MPREAL_MSVC_DEBUGVIEW_CODE;
  554. }
  555. #endif
  556. inline mpreal::mpreal(const char* s, mp_prec_t prec, int base, mp_rnd_t mode)
  557. {
  558. mpfr_init2(mp, prec);
  559. mpfr_set_str(mp, s, base, mode);
  560. MPREAL_MSVC_DEBUGVIEW_CODE;
  561. }
  562. inline mpreal::mpreal(const std::string& s, mp_prec_t prec, int base, mp_rnd_t mode)
  563. {
  564. mpfr_init2(mp, prec);
  565. mpfr_set_str(mp, s.c_str(), base, mode);
  566. MPREAL_MSVC_DEBUGVIEW_CODE;
  567. }
  568. inline mpreal::~mpreal()
  569. {
  570. mpfr_clear(mp);
  571. }
  572. // internal namespace needed for template magic
  573. namespace internal{
  574. // Use SFINAE to restrict arithmetic operations instantiation only for numeric types
  575. // This is needed for smooth integration with libraries based on expression templates, like Eigen.
  576. // TODO: Do the same for boolean operators.
  577. template <typename ArgumentType> struct result_type {};
  578. template <> struct result_type<mpreal> {typedef mpreal type;};
  579. template <> struct result_type<mpz_t> {typedef mpreal type;};
  580. template <> struct result_type<mpq_t> {typedef mpreal type;};
  581. template <> struct result_type<long double> {typedef mpreal type;};
  582. template <> struct result_type<double> {typedef mpreal type;};
  583. template <> struct result_type<unsigned long int> {typedef mpreal type;};
  584. template <> struct result_type<unsigned int> {typedef mpreal type;};
  585. template <> struct result_type<long int> {typedef mpreal type;};
  586. template <> struct result_type<int> {typedef mpreal type;};
  587. #if defined (MPREAL_HAVE_INT64_SUPPORT)
  588. template <> struct result_type<int64_t > {typedef mpreal type;};
  589. template <> struct result_type<uint64_t > {typedef mpreal type;};
  590. #endif
  591. }
  592. // + Addition
  593. template <typename Rhs>
  594. inline const typename internal::result_type<Rhs>::type
  595. operator+(const mpreal& lhs, const Rhs& rhs){ return mpreal(lhs) += rhs; }
  596. template <typename Lhs>
  597. inline const typename internal::result_type<Lhs>::type
  598. operator+(const Lhs& lhs, const mpreal& rhs){ return mpreal(rhs) += lhs; }
  599. // - Subtraction
  600. template <typename Rhs>
  601. inline const typename internal::result_type<Rhs>::type
  602. operator-(const mpreal& lhs, const Rhs& rhs){ return mpreal(lhs) -= rhs; }
  603. template <typename Lhs>
  604. inline const typename internal::result_type<Lhs>::type
  605. operator-(const Lhs& lhs, const mpreal& rhs){ return mpreal(lhs) -= rhs; }
  606. // * Multiplication
  607. template <typename Rhs>
  608. inline const typename internal::result_type<Rhs>::type
  609. operator*(const mpreal& lhs, const Rhs& rhs){ return mpreal(lhs) *= rhs; }
  610. template <typename Lhs>
  611. inline const typename internal::result_type<Lhs>::type
  612. operator*(const Lhs& lhs, const mpreal& rhs){ return mpreal(rhs) *= lhs; }
  613. // / Division
  614. template <typename Rhs>
  615. inline const typename internal::result_type<Rhs>::type
  616. operator/(const mpreal& lhs, const Rhs& rhs){ return mpreal(lhs) /= rhs; }
  617. template <typename Lhs>
  618. inline const typename internal::result_type<Lhs>::type
  619. operator/(const Lhs& lhs, const mpreal& rhs){ return mpreal(lhs) /= rhs; }
  620. //////////////////////////////////////////////////////////////////////////
  621. // sqrt
  622. const mpreal sqrt(const unsigned int v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  623. const mpreal sqrt(const long int v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  624. const mpreal sqrt(const int v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  625. const mpreal sqrt(const long double v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  626. const mpreal sqrt(const double v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  627. //////////////////////////////////////////////////////////////////////////
  628. // pow
  629. const mpreal pow(const mpreal& a, const unsigned int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  630. const mpreal pow(const mpreal& a, const int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  631. const mpreal pow(const mpreal& a, const long double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  632. const mpreal pow(const mpreal& a, const double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  633. const mpreal pow(const unsigned int a, const mpreal& b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  634. const mpreal pow(const long int a, const mpreal& b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  635. const mpreal pow(const int a, const mpreal& b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  636. const mpreal pow(const long double a, const mpreal& b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  637. const mpreal pow(const double a, const mpreal& b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  638. const mpreal pow(const unsigned long int a, const unsigned int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  639. const mpreal pow(const unsigned long int a, const long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  640. const mpreal pow(const unsigned long int a, const int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  641. const mpreal pow(const unsigned long int a, const long double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  642. const mpreal pow(const unsigned long int a, const double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  643. const mpreal pow(const unsigned int a, const unsigned long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  644. const mpreal pow(const unsigned int a, const unsigned int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  645. const mpreal pow(const unsigned int a, const long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  646. const mpreal pow(const unsigned int a, const int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  647. const mpreal pow(const unsigned int a, const long double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  648. const mpreal pow(const unsigned int a, const double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  649. const mpreal pow(const long int a, const unsigned long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  650. const mpreal pow(const long int a, const unsigned int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  651. const mpreal pow(const long int a, const long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  652. const mpreal pow(const long int a, const int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  653. const mpreal pow(const long int a, const long double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  654. const mpreal pow(const long int a, const double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  655. const mpreal pow(const int a, const unsigned long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  656. const mpreal pow(const int a, const unsigned int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  657. const mpreal pow(const int a, const long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  658. const mpreal pow(const int a, const int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  659. const mpreal pow(const int a, const long double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  660. const mpreal pow(const int a, const double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  661. const mpreal pow(const long double a, const long double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  662. const mpreal pow(const long double a, const unsigned long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  663. const mpreal pow(const long double a, const unsigned int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  664. const mpreal pow(const long double a, const long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  665. const mpreal pow(const long double a, const int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  666. const mpreal pow(const double a, const double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  667. const mpreal pow(const double a, const unsigned long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  668. const mpreal pow(const double a, const unsigned int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  669. const mpreal pow(const double a, const long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  670. const mpreal pow(const double a, const int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
  671. //////////////////////////////////////////////////////////////////////////
  672. // Estimate machine epsilon for the given precision
  673. // Returns smallest eps such that 1.0 + eps != 1.0
  674. inline mpreal machine_epsilon(mp_prec_t prec = mpreal::get_default_prec());
  675. // Returns smallest eps such that x + eps != x (relative machine epsilon)
  676. inline mpreal machine_epsilon(const mpreal& x);
  677. // Gives max & min values for the required precision,
  678. // minval is 'safe' meaning 1 / minval does not overflow
  679. // maxval is 'safe' meaning 1 / maxval does not underflow
  680. inline mpreal minval(mp_prec_t prec = mpreal::get_default_prec());
  681. inline mpreal maxval(mp_prec_t prec = mpreal::get_default_prec());
  682. // 'Dirty' equality check 1: |a-b| < min{|a|,|b|} * eps
  683. inline bool isEqualFuzzy(const mpreal& a, const mpreal& b, const mpreal& eps);
  684. // 'Dirty' equality check 2: |a-b| < min{|a|,|b|} * eps( min{|a|,|b|} )
  685. inline bool isEqualFuzzy(const mpreal& a, const mpreal& b);
  686. // 'Bitwise' equality check
  687. // maxUlps - a and b can be apart by maxUlps binary numbers.
  688. inline bool isEqualUlps(const mpreal& a, const mpreal& b, int maxUlps);
  689. //////////////////////////////////////////////////////////////////////////
  690. // Convert precision in 'bits' to decimal digits and vice versa.
  691. // bits = ceil(digits*log[2](10))
  692. // digits = floor(bits*log[10](2))
  693. inline mp_prec_t digits2bits(int d);
  694. inline int bits2digits(mp_prec_t b);
  695. //////////////////////////////////////////////////////////////////////////
  696. // min, max
  697. const mpreal (max)(const mpreal& x, const mpreal& y);
  698. const mpreal (min)(const mpreal& x, const mpreal& y);
  699. //////////////////////////////////////////////////////////////////////////
  700. // Implementation
  701. //////////////////////////////////////////////////////////////////////////
  702. //////////////////////////////////////////////////////////////////////////
  703. // Operators - Assignment
  704. inline mpreal& mpreal::operator=(const mpreal& v)
  705. {
  706. if (this != &v)
  707. {
  708. mp_prec_t tp = mpfr_get_prec(mp);
  709. mp_prec_t vp = mpfr_get_prec(v.mp);
  710. if(tp != vp){
  711. mpfr_clear(mp);
  712. mpfr_init2(mp, vp);
  713. }
  714. mpfr_set(mp, v.mp, mpreal::get_default_rnd());
  715. MPREAL_MSVC_DEBUGVIEW_CODE;
  716. }
  717. return *this;
  718. }
  719. inline mpreal& mpreal::operator=(const mpf_t v)
  720. {
  721. mpfr_set_f(mp, v, mpreal::get_default_rnd());
  722. MPREAL_MSVC_DEBUGVIEW_CODE;
  723. return *this;
  724. }
  725. inline mpreal& mpreal::operator=(const mpz_t v)
  726. {
  727. mpfr_set_z(mp, v, mpreal::get_default_rnd());
  728. MPREAL_MSVC_DEBUGVIEW_CODE;
  729. return *this;
  730. }
  731. inline mpreal& mpreal::operator=(const mpq_t v)
  732. {
  733. mpfr_set_q(mp, v, mpreal::get_default_rnd());
  734. MPREAL_MSVC_DEBUGVIEW_CODE;
  735. return *this;
  736. }
  737. inline mpreal& mpreal::operator=(const long double v)
  738. {
  739. mpfr_set_ld(mp, v, mpreal::get_default_rnd());
  740. MPREAL_MSVC_DEBUGVIEW_CODE;
  741. return *this;
  742. }
  743. inline mpreal& mpreal::operator=(const double v)
  744. {
  745. #if (MPREAL_DOUBLE_BITS_OVERFLOW > -1)
  746. if(fits_in_bits(v, MPREAL_DOUBLE_BITS_OVERFLOW))
  747. {
  748. mpfr_set_d(mp,v,mpreal::get_default_rnd());
  749. }else
  750. throw conversion_overflow();
  751. #else
  752. mpfr_set_d(mp,v,mpreal::get_default_rnd());
  753. #endif
  754. MPREAL_MSVC_DEBUGVIEW_CODE;
  755. return *this;
  756. }
  757. inline mpreal& mpreal::operator=(const unsigned long int v)
  758. {
  759. mpfr_set_ui(mp, v, mpreal::get_default_rnd());
  760. MPREAL_MSVC_DEBUGVIEW_CODE;
  761. return *this;
  762. }
  763. inline mpreal& mpreal::operator=(const unsigned int v)
  764. {
  765. mpfr_set_ui(mp, v, mpreal::get_default_rnd());
  766. MPREAL_MSVC_DEBUGVIEW_CODE;
  767. return *this;
  768. }
  769. inline mpreal& mpreal::operator=(const long int v)
  770. {
  771. mpfr_set_si(mp, v, mpreal::get_default_rnd());
  772. MPREAL_MSVC_DEBUGVIEW_CODE;
  773. return *this;
  774. }
  775. inline mpreal& mpreal::operator=(const int v)
  776. {
  777. mpfr_set_si(mp, v, mpreal::get_default_rnd());
  778. MPREAL_MSVC_DEBUGVIEW_CODE;
  779. return *this;
  780. }
  781. inline mpreal& mpreal::operator=(const char* s)
  782. {
  783. // Use other converters for more precise control on base & precision & rounding:
  784. //
  785. // mpreal(const char* s, mp_prec_t prec, int base, mp_rnd_t mode)
  786. // mpreal(const std::string& s,mp_prec_t prec, int base, mp_rnd_t mode)
  787. //
  788. // Here we assume base = 10 and we use precision of target variable.
  789. mpfr_t t;
  790. mpfr_init2(t, mpfr_get_prec(mp));
  791. if(0 == mpfr_set_str(t, s, 10, mpreal::get_default_rnd()))
  792. {
  793. mpfr_set(mp, t, mpreal::get_default_rnd());
  794. MPREAL_MSVC_DEBUGVIEW_CODE;
  795. }
  796. mpfr_clear(t);
  797. return *this;
  798. }
  799. inline mpreal& mpreal::operator=(const std::string& s)
  800. {
  801. // Use other converters for more precise control on base & precision & rounding:
  802. //
  803. // mpreal(const char* s, mp_prec_t prec, int base, mp_rnd_t mode)
  804. // mpreal(const std::string& s,mp_prec_t prec, int base, mp_rnd_t mode)
  805. //
  806. // Here we assume base = 10 and we use precision of target variable.
  807. mpfr_t t;
  808. mpfr_init2(t, mpfr_get_prec(mp));
  809. if(0 == mpfr_set_str(t, s.c_str(), 10, mpreal::get_default_rnd()))
  810. {
  811. mpfr_set(mp, t, mpreal::get_default_rnd());
  812. MPREAL_MSVC_DEBUGVIEW_CODE;
  813. }
  814. mpfr_clear(t);
  815. return *this;
  816. }
  817. //////////////////////////////////////////////////////////////////////////
  818. // + Addition
  819. inline mpreal& mpreal::operator+=(const mpreal& v)
  820. {
  821. mpfr_add(mp,mp,v.mp,mpreal::get_default_rnd());
  822. MPREAL_MSVC_DEBUGVIEW_CODE;
  823. return *this;
  824. }
  825. inline mpreal& mpreal::operator+=(const mpf_t u)
  826. {
  827. *this += mpreal(u);
  828. MPREAL_MSVC_DEBUGVIEW_CODE;
  829. return *this;
  830. }
  831. inline mpreal& mpreal::operator+=(const mpz_t u)
  832. {
  833. mpfr_add_z(mp,mp,u,mpreal::get_default_rnd());
  834. MPREAL_MSVC_DEBUGVIEW_CODE;
  835. return *this;
  836. }
  837. inline mpreal& mpreal::operator+=(const mpq_t u)
  838. {
  839. mpfr_add_q(mp,mp,u,mpreal::get_default_rnd());
  840. MPREAL_MSVC_DEBUGVIEW_CODE;
  841. return *this;
  842. }
  843. inline mpreal& mpreal::operator+= (const long double u)
  844. {
  845. *this += mpreal(u);
  846. MPREAL_MSVC_DEBUGVIEW_CODE;
  847. return *this;
  848. }
  849. inline mpreal& mpreal::operator+= (const double u)
  850. {
  851. #if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
  852. mpfr_add_d(mp,mp,u,mpreal::get_default_rnd());
  853. #else
  854. *this += mpreal(u);
  855. #endif
  856. MPREAL_MSVC_DEBUGVIEW_CODE;
  857. return *this;
  858. }
  859. inline mpreal& mpreal::operator+=(const unsigned long int u)
  860. {
  861. mpfr_add_ui(mp,mp,u,mpreal::get_default_rnd());
  862. MPREAL_MSVC_DEBUGVIEW_CODE;
  863. return *this;
  864. }
  865. inline mpreal& mpreal::operator+=(const unsigned int u)
  866. {
  867. mpfr_add_ui(mp,mp,u,mpreal::get_default_rnd());
  868. MPREAL_MSVC_DEBUGVIEW_CODE;
  869. return *this;
  870. }
  871. inline mpreal& mpreal::operator+=(const long int u)
  872. {
  873. mpfr_add_si(mp,mp,u,mpreal::get_default_rnd());
  874. MPREAL_MSVC_DEBUGVIEW_CODE;
  875. return *this;
  876. }
  877. inline mpreal& mpreal::operator+=(const int u)
  878. {
  879. mpfr_add_si(mp,mp,u,mpreal::get_default_rnd());
  880. MPREAL_MSVC_DEBUGVIEW_CODE;
  881. return *this;
  882. }
  883. #if defined (MPREAL_HAVE_INT64_SUPPORT)
  884. inline mpreal& mpreal::operator+=(const int64_t u){ *this += mpreal(u); MPREAL_MSVC_DEBUGVIEW_CODE; return *this; }
  885. inline mpreal& mpreal::operator+=(const uint64_t u){ *this += mpreal(u); MPREAL_MSVC_DEBUGVIEW_CODE; return *this; }
  886. inline mpreal& mpreal::operator-=(const int64_t u){ *this -= mpreal(u); MPREAL_MSVC_DEBUGVIEW_CODE; return *this; }
  887. inline mpreal& mpreal::operator-=(const uint64_t u){ *this -= mpreal(u); MPREAL_MSVC_DEBUGVIEW_CODE; return *this; }
  888. inline mpreal& mpreal::operator*=(const int64_t u){ *this *= mpreal(u); MPREAL_MSVC_DEBUGVIEW_CODE; return *this; }
  889. inline mpreal& mpreal::operator*=(const uint64_t u){ *this *= mpreal(u); MPREAL_MSVC_DEBUGVIEW_CODE; return *this; }
  890. inline mpreal& mpreal::operator/=(const int64_t u){ *this /= mpreal(u); MPREAL_MSVC_DEBUGVIEW_CODE; return *this; }
  891. inline mpreal& mpreal::operator/=(const uint64_t u){ *this /= mpreal(u); MPREAL_MSVC_DEBUGVIEW_CODE; return *this; }
  892. #endif
  893. inline const mpreal mpreal::operator+()const { return mpreal(*this); }
  894. inline const mpreal operator+(const mpreal& a, const mpreal& b)
  895. {
  896. mpreal c(0, (std::max)(mpfr_get_prec(a.mpfr_ptr()), mpfr_get_prec(b.mpfr_ptr())));
  897. mpfr_add(c.mpfr_ptr(), a.mpfr_srcptr(), b.mpfr_srcptr(), mpreal::get_default_rnd());
  898. return c;
  899. }
  900. inline mpreal& mpreal::operator++()
  901. {
  902. return *this += 1;
  903. }
  904. inline const mpreal mpreal::operator++ (int)
  905. {
  906. mpreal x(*this);
  907. *this += 1;
  908. return x;
  909. }
  910. inline mpreal& mpreal::operator--()
  911. {
  912. return *this -= 1;
  913. }
  914. inline const mpreal mpreal::operator-- (int)
  915. {
  916. mpreal x(*this);
  917. *this -= 1;
  918. return x;
  919. }
  920. //////////////////////////////////////////////////////////////////////////
  921. // - Subtraction
  922. inline mpreal& mpreal::operator-=(const mpreal& v)
  923. {
  924. mpfr_sub(mp,mp,v.mp,mpreal::get_default_rnd());
  925. MPREAL_MSVC_DEBUGVIEW_CODE;
  926. return *this;
  927. }
  928. inline mpreal& mpreal::operator-=(const mpz_t v)
  929. {
  930. mpfr_sub_z(mp,mp,v,mpreal::get_default_rnd());
  931. MPREAL_MSVC_DEBUGVIEW_CODE;
  932. return *this;
  933. }
  934. inline mpreal& mpreal::operator-=(const mpq_t v)
  935. {
  936. mpfr_sub_q(mp,mp,v,mpreal::get_default_rnd());
  937. MPREAL_MSVC_DEBUGVIEW_CODE;
  938. return *this;
  939. }
  940. inline mpreal& mpreal::operator-=(const long double v)
  941. {
  942. *this -= mpreal(v);
  943. MPREAL_MSVC_DEBUGVIEW_CODE;
  944. return *this;
  945. }
  946. inline mpreal& mpreal::operator-=(const double v)
  947. {
  948. #if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
  949. mpfr_sub_d(mp,mp,v,mpreal::get_default_rnd());
  950. #else
  951. *this -= mpreal(v);
  952. #endif
  953. MPREAL_MSVC_DEBUGVIEW_CODE;
  954. return *this;
  955. }
  956. inline mpreal& mpreal::operator-=(const unsigned long int v)
  957. {
  958. mpfr_sub_ui(mp,mp,v,mpreal::get_default_rnd());
  959. MPREAL_MSVC_DEBUGVIEW_CODE;
  960. return *this;
  961. }
  962. inline mpreal& mpreal::operator-=(const unsigned int v)
  963. {
  964. mpfr_sub_ui(mp,mp,v,mpreal::get_default_rnd());
  965. MPREAL_MSVC_DEBUGVIEW_CODE;
  966. return *this;
  967. }
  968. inline mpreal& mpreal::operator-=(const long int v)
  969. {
  970. mpfr_sub_si(mp,mp,v,mpreal::get_default_rnd());
  971. MPREAL_MSVC_DEBUGVIEW_CODE;
  972. return *this;
  973. }
  974. inline mpreal& mpreal::operator-=(const int v)
  975. {
  976. mpfr_sub_si(mp,mp,v,mpreal::get_default_rnd());
  977. MPREAL_MSVC_DEBUGVIEW_CODE;
  978. return *this;
  979. }
  980. inline const mpreal mpreal::operator-()const
  981. {
  982. mpreal u(*this);
  983. mpfr_neg(u.mp,u.mp,mpreal::get_default_rnd());
  984. return u;
  985. }
  986. inline const mpreal operator-(const mpreal& a, const mpreal& b)
  987. {
  988. mpreal c(0, (std::max)(mpfr_get_prec(a.mpfr_ptr()), mpfr_get_prec(b.mpfr_ptr())));
  989. mpfr_sub(c.mpfr_ptr(), a.mpfr_srcptr(), b.mpfr_srcptr(), mpreal::get_default_rnd());
  990. return c;
  991. }
  992. inline const mpreal operator-(const double b, const mpreal& a)
  993. {
  994. #if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
  995. mpreal x(0, mpfr_get_prec(a.mpfr_ptr()));
  996. mpfr_d_sub(x.mpfr_ptr(), b, a.mpfr_srcptr(), mpreal::get_default_rnd());
  997. return x;
  998. #else
  999. mpreal x(b, mpfr_get_prec(a.mpfr_ptr()));
  1000. x -= a;
  1001. return x;
  1002. #endif
  1003. }
  1004. inline const mpreal operator-(const unsigned long int b, const mpreal& a)
  1005. {
  1006. mpreal x(0, mpfr_get_prec(a.mpfr_ptr()));
  1007. mpfr_ui_sub(x.mpfr_ptr(), b, a.mpfr_srcptr(), mpreal::get_default_rnd());
  1008. return x;
  1009. }
  1010. inline const mpreal operator-(const unsigned int b, const mpreal& a)
  1011. {
  1012. mpreal x(0, mpfr_get_prec(a.mpfr_ptr()));
  1013. mpfr_ui_sub(x.mpfr_ptr(), b, a.mpfr_srcptr(), mpreal::get_default_rnd());
  1014. return x;
  1015. }
  1016. inline const mpreal operator-(const long int b, const mpreal& a)
  1017. {
  1018. mpreal x(0, mpfr_get_prec(a.mpfr_ptr()));
  1019. mpfr_si_sub(x.mpfr_ptr(), b, a.mpfr_srcptr(), mpreal::get_default_rnd());
  1020. return x;
  1021. }
  1022. inline const mpreal operator-(const int b, const mpreal& a)
  1023. {
  1024. mpreal x(0, mpfr_get_prec(a.mpfr_ptr()));
  1025. mpfr_si_sub(x.mpfr_ptr(), b, a.mpfr_srcptr(), mpreal::get_default_rnd());
  1026. return x;
  1027. }
  1028. //////////////////////////////////////////////////////////////////////////
  1029. // * Multiplication
  1030. inline mpreal& mpreal::operator*= (const mpreal& v)
  1031. {
  1032. mpfr_mul(mp,mp,v.mp,mpreal::get_default_rnd());
  1033. MPREAL_MSVC_DEBUGVIEW_CODE;
  1034. return *this;
  1035. }
  1036. inline mpreal& mpreal::operator*=(const mpz_t v)
  1037. {
  1038. mpfr_mul_z(mp,mp,v,mpreal::get_default_rnd());
  1039. MPREAL_MSVC_DEBUGVIEW_CODE;
  1040. return *this;
  1041. }
  1042. inline mpreal& mpreal::operator*=(const mpq_t v)
  1043. {
  1044. mpfr_mul_q(mp,mp,v,mpreal::get_default_rnd());
  1045. MPREAL_MSVC_DEBUGVIEW_CODE;
  1046. return *this;
  1047. }
  1048. inline mpreal& mpreal::operator*=(const long double v)
  1049. {
  1050. *this *= mpreal(v);
  1051. MPREAL_MSVC_DEBUGVIEW_CODE;
  1052. return *this;
  1053. }
  1054. inline mpreal& mpreal::operator*=(const double v)
  1055. {
  1056. #if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
  1057. mpfr_mul_d(mp,mp,v,mpreal::get_default_rnd());
  1058. #else
  1059. *this *= mpreal(v);
  1060. #endif
  1061. MPREAL_MSVC_DEBUGVIEW_CODE;
  1062. return *this;
  1063. }
  1064. inline mpreal& mpreal::operator*=(const unsigned long int v)
  1065. {
  1066. mpfr_mul_ui(mp,mp,v,mpreal::get_default_rnd());
  1067. MPREAL_MSVC_DEBUGVIEW_CODE;
  1068. return *this;
  1069. }
  1070. inline mpreal& mpreal::operator*=(const unsigned int v)
  1071. {
  1072. mpfr_mul_ui(mp,mp,v,mpreal::get_default_rnd());
  1073. MPREAL_MSVC_DEBUGVIEW_CODE;
  1074. return *this;
  1075. }
  1076. inline mpreal& mpreal::operator*=(const long int v)
  1077. {
  1078. mpfr_mul_si(mp,mp,v,mpreal::get_default_rnd());
  1079. MPREAL_MSVC_DEBUGVIEW_CODE;
  1080. return *this;
  1081. }
  1082. inline mpreal& mpreal::operator*=(const int v)
  1083. {
  1084. mpfr_mul_si(mp,mp,v,mpreal::get_default_rnd());
  1085. MPREAL_MSVC_DEBUGVIEW_CODE;
  1086. return *this;
  1087. }
  1088. inline const mpreal operator*(const mpreal& a, const mpreal& b)
  1089. {
  1090. mpreal c(0, (std::max)(mpfr_get_prec(a.mpfr_ptr()), mpfr_get_prec(b.mpfr_ptr())));
  1091. mpfr_mul(c.mpfr_ptr(), a.mpfr_srcptr(), b.mpfr_srcptr(), mpreal::get_default_rnd());
  1092. return c;
  1093. }
  1094. //////////////////////////////////////////////////////////////////////////
  1095. // / Division
  1096. inline mpreal& mpreal::operator/=(const mpreal& v)
  1097. {
  1098. mpfr_div(mp,mp,v.mp,mpreal::get_default_rnd());
  1099. MPREAL_MSVC_DEBUGVIEW_CODE;
  1100. return *this;
  1101. }
  1102. inline mpreal& mpreal::operator/=(const mpz_t v)
  1103. {
  1104. mpfr_div_z(mp,mp,v,mpreal::get_default_rnd());
  1105. MPREAL_MSVC_DEBUGVIEW_CODE;
  1106. return *this;
  1107. }
  1108. inline mpreal& mpreal::operator/=(const mpq_t v)
  1109. {
  1110. mpfr_div_q(mp,mp,v,mpreal::get_default_rnd());
  1111. MPREAL_MSVC_DEBUGVIEW_CODE;
  1112. return *this;
  1113. }
  1114. inline mpreal& mpreal::operator/=(const long double v)
  1115. {
  1116. *this /= mpreal(v);
  1117. MPREAL_MSVC_DEBUGVIEW_CODE;
  1118. return *this;
  1119. }
  1120. inline mpreal& mpreal::operator/=(const double v)
  1121. {
  1122. #if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
  1123. mpfr_div_d(mp,mp,v,mpreal::get_default_rnd());
  1124. #else
  1125. *this /= mpreal(v);
  1126. #endif
  1127. MPREAL_MSVC_DEBUGVIEW_CODE;
  1128. return *this;
  1129. }
  1130. inline mpreal& mpreal::operator/=(const unsigned long int v)
  1131. {
  1132. mpfr_div_ui(mp,mp,v,mpreal::get_default_rnd());
  1133. MPREAL_MSVC_DEBUGVIEW_CODE;
  1134. return *this;
  1135. }
  1136. inline mpreal& mpreal::operator/=(const unsigned int v)
  1137. {
  1138. mpfr_div_ui(mp,mp,v,mpreal::get_default_rnd());
  1139. MPREAL_MSVC_DEBUGVIEW_CODE;
  1140. return *this;
  1141. }
  1142. inline mpreal& mpreal::operator/=(const long int v)
  1143. {
  1144. mpfr_div_si(mp,mp,v,mpreal::get_default_rnd());
  1145. MPREAL_MSVC_DEBUGVIEW_CODE;
  1146. return *this;
  1147. }
  1148. inline mpreal& mpreal::operator/=(const int v)
  1149. {
  1150. mpfr_div_si(mp,mp,v,mpreal::get_default_rnd());
  1151. MPREAL_MSVC_DEBUGVIEW_CODE;
  1152. return *this;
  1153. }
  1154. inline const mpreal operator/(const mpreal& a, const mpreal& b)
  1155. {
  1156. mpreal c(0, (std::max)(mpfr_get_prec(a.mpfr_ptr()), mpfr_get_prec(b.mpfr_ptr())));
  1157. mpfr_div(c.mpfr_ptr(), a.mpfr_srcptr(), b.mpfr_srcptr(), mpreal::get_default_rnd());
  1158. return c;
  1159. }
  1160. inline const mpreal operator/(const unsigned long int b, const mpreal& a)
  1161. {
  1162. mpreal x(0, mpfr_get_prec(a.mpfr_ptr()));
  1163. mpfr_ui_div(x.mpfr_ptr(), b, a.mpfr_srcptr(), mpreal::get_default_rnd());
  1164. return x;
  1165. }
  1166. inline const mpreal operator/(const unsigned int b, const mpreal& a)
  1167. {
  1168. mpreal x(0, mpfr_get_prec(a.mpfr_ptr()));
  1169. mpfr_ui_div(x.mpfr_ptr(), b, a.mpfr_srcptr(), mpreal::get_default_rnd());
  1170. return x;
  1171. }
  1172. inline const mpreal operator/(const long int b, const mpreal& a)
  1173. {
  1174. mpreal x(0, mpfr_get_prec(a.mpfr_ptr()));
  1175. mpfr_si_div(x.mpfr_ptr(), b, a.mpfr_srcptr(),mpreal::get_default_rnd());
  1176. return x;
  1177. }
  1178. inline const mpreal operator/(const int b, const mpreal& a)
  1179. {
  1180. mpreal x(0, mpfr_get_prec(a.mpfr_ptr()));
  1181. mpfr_si_div(x.mpfr_ptr(), b, a.mpfr_srcptr(),mpreal::get_default_rnd());
  1182. return x;
  1183. }
  1184. inline const mpreal operator/(const double b, const mpreal& a)
  1185. {
  1186. #if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
  1187. mpreal x(0, mpfr_get_prec(a.mpfr_ptr()));
  1188. mpfr_d_div(x.mpfr_ptr(), b, a.mpfr_srcptr(),mpreal::get_default_rnd());
  1189. return x;
  1190. #else
  1191. mpreal x(0, mpfr_get_prec(a.mpfr_ptr()));
  1192. x /= a;
  1193. return x;
  1194. #endif
  1195. }
  1196. //////////////////////////////////////////////////////////////////////////
  1197. // Shifts operators - Multiplication/Division by power of 2
  1198. inline mpreal& mpreal::operator<<=(const unsigned long int u)
  1199. {
  1200. mpfr_mul_2ui(mp,mp,u,mpreal::get_default_rnd());
  1201. MPREAL_MSVC_DEBUGVIEW_CODE;
  1202. return *this;
  1203. }
  1204. inline mpreal& mpreal::operator<<=(const unsigned int u)
  1205. {
  1206. mpfr_mul_2ui(mp,mp,static_cast<unsigned long int>(u),mpreal::get_default_rnd());
  1207. MPREAL_MSVC_DEBUGVIEW_CODE;
  1208. return *this;
  1209. }
  1210. inline mpreal& mpreal::operator<<=(const long int u)
  1211. {
  1212. mpfr_mul_2si(mp,mp,u,mpreal::get_default_rnd());
  1213. MPREAL_MSVC_DEBUGVIEW_CODE;
  1214. return *this;
  1215. }
  1216. inline mpreal& mpreal::operator<<=(const int u)
  1217. {
  1218. mpfr_mul_2si(mp,mp,static_cast<long int>(u),mpreal::get_default_rnd());
  1219. MPREAL_MSVC_DEBUGVIEW_CODE;
  1220. return *this;
  1221. }
  1222. inline mpreal& mpreal::operator>>=(const unsigned long int u)
  1223. {
  1224. mpfr_div_2ui(mp,mp,u,mpreal::get_default_rnd());
  1225. MPREAL_MSVC_DEBUGVIEW_CODE;
  1226. return *this;
  1227. }
  1228. inline mpreal& mpreal::operator>>=(const unsigned int u)
  1229. {
  1230. mpfr_div_2ui(mp,mp,static_cast<unsigned long int>(u),mpreal::get_default_rnd());
  1231. MPREAL_MSVC_DEBUGVIEW_CODE;
  1232. return *this;
  1233. }
  1234. inline mpreal& mpreal::operator>>=(const long int u)
  1235. {
  1236. mpfr_div_2si(mp,mp,u,mpreal::get_default_rnd());
  1237. MPREAL_MSVC_DEBUGVIEW_CODE;
  1238. return *this;
  1239. }
  1240. inline mpreal& mpreal::operator>>=(const int u)
  1241. {
  1242. mpfr_div_2si(mp,mp,static_cast<long int>(u),mpreal::get_default_rnd());
  1243. MPREAL_MSVC_DEBUGVIEW_CODE;
  1244. return *this;
  1245. }
  1246. inline const mpreal operator<<(const mpreal& v, const unsigned long int k)
  1247. {
  1248. return mul_2ui(v,k);
  1249. }
  1250. inline const mpreal operator<<(const mpreal& v, const unsigned int k)
  1251. {
  1252. return mul_2ui(v,static_cast<unsigned long int>(k));
  1253. }
  1254. inline const mpreal operator<<(const mpreal& v, const long int k)
  1255. {
  1256. return mul_2si(v,k);
  1257. }
  1258. inline const mpreal operator<<(const mpreal& v, const int k)
  1259. {
  1260. return mul_2si(v,static_cast<long int>(k));
  1261. }
  1262. inline const mpreal operator>>(const mpreal& v, const unsigned long int k)
  1263. {
  1264. return div_2ui(v,k);
  1265. }
  1266. inline const mpreal operator>>(const mpreal& v, const long int k)
  1267. {
  1268. return div_2si(v,k);
  1269. }
  1270. inline const mpreal operator>>(const mpreal& v, const unsigned int k)
  1271. {
  1272. return div_2ui(v,static_cast<unsigned long int>(k));
  1273. }
  1274. inline const mpreal operator>>(const mpreal& v, const int k)
  1275. {
  1276. return div_2si(v,static_cast<long int>(k));
  1277. }
  1278. // mul_2ui
  1279. inline const mpreal mul_2ui(const mpreal& v, unsigned long int k, mp_rnd_t rnd_mode)
  1280. {
  1281. mpreal x(v);
  1282. mpfr_mul_2ui(x.mp,v.mp,k,rnd_mode);
  1283. return x;
  1284. }
  1285. // mul_2si
  1286. inline const mpreal mul_2si(const mpreal& v, long int k, mp_rnd_t rnd_mode)
  1287. {
  1288. mpreal x(v);
  1289. mpfr_mul_2si(x.mp,v.mp,k,rnd_mode);
  1290. return x;
  1291. }
  1292. inline const mpreal div_2ui(const mpreal& v, unsigned long int k, mp_rnd_t rnd_mode)
  1293. {
  1294. mpreal x(v);
  1295. mpfr_div_2ui(x.mp,v.mp,k,rnd_mode);
  1296. return x;
  1297. }
  1298. inline const mpreal div_2si(const mpreal& v, long int k, mp_rnd_t rnd_mode)
  1299. {
  1300. mpreal x(v);
  1301. mpfr_div_2si(x.mp,v.mp,k,rnd_mode);
  1302. return x;
  1303. }
  1304. //////////////////////////////////////////////////////////////////////////
  1305. //Boolean operators
  1306. inline bool operator > (const mpreal& a, const mpreal& b){ return (mpfr_greater_p(a.mp,b.mp) !=0); }
  1307. inline bool operator >= (const mpreal& a, const mpreal& b){ return (mpfr_greaterequal_p(a.mp,b.mp) !=0); }
  1308. inline bool operator < (const mpreal& a, const mpreal& b){ return (mpfr_less_p(a.mp,b.mp) !=0); }
  1309. inline bool operator <= (const mpreal& a, const mpreal& b){ return (mpfr_lessequal_p(a.mp,b.mp) !=0); }
  1310. inline bool operator == (const mpreal& a, const mpreal& b){ return (mpfr_equal_p(a.mp,b.mp) !=0); }
  1311. inline bool operator != (const mpreal& a, const mpreal& b){ return (mpfr_lessgreater_p(a.mp,b.mp) !=0); }
  1312. inline bool operator == (const mpreal& a, const unsigned long int b ){ return (mpfr_cmp_ui(a.mp,b) == 0); }
  1313. inline bool operator == (const mpreal& a, const unsigned int b ){ return (mpfr_cmp_ui(a.mp,b) == 0); }
  1314. inline bool operator == (const mpreal& a, const long int b ){ return (mpfr_cmp_si(a.mp,b) == 0); }
  1315. inline bool operator == (const mpreal& a, const int b ){ return (mpfr_cmp_si(a.mp,b) == 0); }
  1316. inline bool operator == (const mpreal& a, const long double b ){ return (mpfr_cmp_ld(a.mp,b) == 0); }
  1317. inline bool operator == (const mpreal& a, const double b ){ return (mpfr_cmp_d(a.mp,b) == 0); }
  1318. inline bool isnan (const mpreal& v){ return (mpfr_nan_p(v.mp) != 0); }
  1319. inline bool isinf (const mpreal& v){ return (mpfr_inf_p(v.mp) != 0); }
  1320. inline bool isfinite (const mpreal& v){ return (mpfr_number_p(v.mp) != 0); }
  1321. inline bool iszero (const mpreal& v){ return (mpfr_zero_p(v.mp) != 0); }
  1322. inline bool isint (const mpreal& v){ return (mpfr_integer_p(v.mp) != 0); }
  1323. #if (MPFR_VERSION >= MPFR_VERSION_NUM(3,0,0))
  1324. inline bool isregular(const mpreal& v){ return (mpfr_regular_p(v.mp));}
  1325. #endif
  1326. //////////////////////////////////////////////////////////////////////////
  1327. // Type Converters
  1328. inline long mpreal::toLong (mp_rnd_t mode) const { return mpfr_get_si(mp, mode); }
  1329. inline unsigned long mpreal::toULong (mp_rnd_t mode) const { return mpfr_get_ui(mp, mode); }
  1330. inline double mpreal::toDouble (mp_rnd_t mode) const { return mpfr_get_d (mp, mode); }
  1331. inline long double mpreal::toLDouble(mp_rnd_t mode) const { return mpfr_get_ld(mp, mode); }
  1332. #if defined (MPREAL_HAVE_INT64_SUPPORT)
  1333. inline int64_t mpreal::toInt64 (mp_rnd_t mode) const{ return mpfr_get_sj(mp, mode); }
  1334. inline uint64_t mpreal::toUInt64(mp_rnd_t mode) const{ return mpfr_get_uj(mp, mode); }
  1335. #endif
  1336. inline ::mpfr_ptr mpreal::mpfr_ptr() { return mp; }
  1337. inline ::mpfr_srcptr mpreal::mpfr_ptr() const { return mp; }
  1338. inline ::mpfr_srcptr mpreal::mpfr_srcptr() const { return mp; }
  1339. template <class T>
  1340. inline std::string toString(T t, std::ios_base & (*f)(std::ios_base&))
  1341. {
  1342. std::ostringstream oss;
  1343. oss << f << t;
  1344. return oss.str();
  1345. }
  1346. #if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
  1347. inline std::string mpreal::toString(const std::string& format) const
  1348. {
  1349. char *s = NULL;
  1350. std::string out;
  1351. if( !format.empty() )
  1352. {
  1353. if(!(mpfr_asprintf(&s,format.c_str(),mp) < 0))
  1354. {
  1355. out = std::string(s);
  1356. mpfr_free_str(s);
  1357. }
  1358. }
  1359. return out;
  1360. }
  1361. #endif
  1362. inline std::string mpreal::toString(int n, int b, mp_rnd_t mode) const
  1363. {
  1364. (void)b;
  1365. (void)mode;
  1366. #if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
  1367. // Use MPFR native function for output
  1368. char format[128];
  1369. int digits;
  1370. digits = n > 0 ? n : bits2digits(mpfr_get_prec(mp));
  1371. sprintf(format,"%%.%dRNg",digits); // Default format
  1372. return toString(std::string(format));
  1373. #else
  1374. char *s, *ns = NULL;
  1375. size_t slen, nslen;
  1376. mp_exp_t exp;
  1377. std::string out;
  1378. if(mpfr_inf_p(mp))
  1379. {
  1380. if(mpfr_sgn(mp)>0) return "+Inf";
  1381. else return "-Inf";
  1382. }
  1383. if(mpfr_zero_p(mp)) return "0";
  1384. if(mpfr_nan_p(mp)) return "NaN";
  1385. s = mpfr_get_str(NULL,&exp,b,0,mp,mode);
  1386. ns = mpfr_get_str(NULL,&exp,b,n,mp,mode);
  1387. if(s!=NULL && ns!=NULL)
  1388. {
  1389. slen = strlen(s);
  1390. nslen = strlen(ns);
  1391. if(nslen<=slen)
  1392. {
  1393. mpfr_free_str(s);
  1394. s = ns;
  1395. slen = nslen;
  1396. }
  1397. else {
  1398. mpfr_free_str(ns);
  1399. }
  1400. // Make human eye-friendly formatting if possible
  1401. if (exp>0 && static_cast<size_t>(exp)<slen)
  1402. {
  1403. if(s[0]=='-')
  1404. {
  1405. // Remove zeros starting from right end
  1406. char* ptr = s+slen-1;
  1407. while (*ptr=='0' && ptr>s+exp) ptr--;
  1408. if(ptr==s+exp) out = std::string(s,exp+1);
  1409. else out = std::string(s,exp+1)+'.'+std::string(s+exp+1,ptr-(s+exp+1)+1);
  1410. //out = string(s,exp+1)+'.'+string(s+exp+1);
  1411. }
  1412. else
  1413. {
  1414. // Remove zeros starting from right end
  1415. char* ptr = s+slen-1;
  1416. while (*ptr=='0' && ptr>s+exp-1) ptr--;
  1417. if(ptr==s+exp-1) out = std::string(s,exp);
  1418. else out = std::string(s,exp)+'.'+std::string(s+exp,ptr-(s+exp)+1);
  1419. //out = string(s,exp)+'.'+string(s+exp);
  1420. }
  1421. }else{ // exp<0 || exp>slen
  1422. if(s[0]=='-')
  1423. {
  1424. // Remove zeros starting from right end
  1425. char* ptr = s+slen-1;
  1426. while (*ptr=='0' && ptr>s+1) ptr--;
  1427. if(ptr==s+1) out = std::string(s,2);
  1428. else out = std::string(s,2)+'.'+std::string(s+2,ptr-(s+2)+1);
  1429. //out = string(s,2)+'.'+string(s+2);
  1430. }
  1431. else
  1432. {
  1433. // Remove zeros starting from right end
  1434. char* ptr = s+slen-1;
  1435. while (*ptr=='0' && ptr>s) ptr--;
  1436. if(ptr==s) out = std::string(s,1);
  1437. else out = std::string(s,1)+'.'+std::string(s+1,ptr-(s+1)+1);
  1438. //out = string(s,1)+'.'+string(s+1);
  1439. }
  1440. // Make final string
  1441. if(--exp)
  1442. {
  1443. if(exp>0) out += "e+"+mpfr::toString<mp_exp_t>(exp,std::dec);
  1444. else out += "e"+mpfr::toString<mp_exp_t>(exp,std::dec);
  1445. }
  1446. }
  1447. mpfr_free_str(s);
  1448. return out;
  1449. }else{
  1450. return "conversion error!";
  1451. }
  1452. #endif
  1453. }
  1454. //////////////////////////////////////////////////////////////////////////
  1455. // I/O
  1456. inline std::ostream& operator<<(std::ostream& os, const mpreal& v)
  1457. {
  1458. return os<<v.toString(static_cast<int>(os.precision()));
  1459. }
  1460. inline std::istream& operator>>(std::istream &is, mpreal& v)
  1461. {
  1462. // ToDo, use cout::hexfloat and other flags to setup base
  1463. std::string tmp;
  1464. is >> tmp;
  1465. mpfr_set_str(v.mp, tmp.c_str(), 10, mpreal::get_default_rnd());
  1466. return is;
  1467. }
  1468. //////////////////////////////////////////////////////////////////////////
  1469. // Bits - decimal digits relation
  1470. // bits = ceil(digits*log[2](10))
  1471. // digits = floor(bits*log[10](2))
  1472. inline mp_prec_t digits2bits(int d)
  1473. {
  1474. const double LOG2_10 = 3.3219280948873624;
  1475. return (mp_prec_t) std::ceil( d * LOG2_10 );
  1476. }
  1477. inline int bits2digits(mp_prec_t b)
  1478. {
  1479. const double LOG10_2 = 0.30102999566398119;
  1480. return (int) std::floor( b * LOG10_2 );
  1481. }
  1482. //////////////////////////////////////////////////////////////////////////
  1483. // Set/Get number properties
  1484. inline int sgn(const mpreal& v)
  1485. {
  1486. int r = mpfr_signbit(v.mp);
  1487. return (r>0?-1:1);
  1488. }
  1489. inline mpreal& mpreal::setSign(int sign, mp_rnd_t RoundingMode)
  1490. {
  1491. mpfr_setsign(mp,mp,(sign < 0 ? 1 : 0),RoundingMode);
  1492. MPREAL_MSVC_DEBUGVIEW_CODE;
  1493. return *this;
  1494. }
  1495. inline int mpreal::getPrecision() const
  1496. {
  1497. return mpfr_get_prec(mp);
  1498. }
  1499. inline mpreal& mpreal::setPrecision(int Precision, mp_rnd_t RoundingMode)
  1500. {
  1501. mpfr_prec_round(mp, Precision, RoundingMode);
  1502. MPREAL_MSVC_DEBUGVIEW_CODE;
  1503. return *this;
  1504. }
  1505. inline mpreal& mpreal::setInf(int sign)
  1506. {
  1507. mpfr_set_inf(mp,sign);
  1508. MPREAL_MSVC_DEBUGVIEW_CODE;
  1509. return *this;
  1510. }
  1511. inline mpreal& mpreal::setNan()
  1512. {
  1513. mpfr_set_nan(mp);
  1514. MPREAL_MSVC_DEBUGVIEW_CODE;
  1515. return *this;
  1516. }
  1517. inline mpreal& mpreal::setZero(int sign)
  1518. {
  1519. #if (MPFR_VERSION >= MPFR_VERSION_NUM(3,0,0))
  1520. mpfr_set_zero(mp, sign);
  1521. #else
  1522. mpfr_set_si(mp, 0, (mpfr_get_default_rounding_mode)());
  1523. setSign(sign);
  1524. #endif
  1525. MPREAL_MSVC_DEBUGVIEW_CODE;
  1526. return *this;
  1527. }
  1528. inline mp_prec_t mpreal::get_prec() const
  1529. {
  1530. return mpfr_get_prec(mp);
  1531. }
  1532. inline void mpreal::set_prec(mp_prec_t prec, mp_rnd_t rnd_mode)
  1533. {
  1534. mpfr_prec_round(mp,prec,rnd_mode);
  1535. MPREAL_MSVC_DEBUGVIEW_CODE;
  1536. }
  1537. inline mp_exp_t mpreal::get_exp ()
  1538. {
  1539. return mpfr_get_exp(mp);
  1540. }
  1541. inline int mpreal::set_exp (mp_exp_t e)
  1542. {
  1543. int x = mpfr_set_exp(mp, e);
  1544. MPREAL_MSVC_DEBUGVIEW_CODE;
  1545. return x;
  1546. }
  1547. inline const mpreal frexp(const mpreal& v, mp_exp_t* exp)
  1548. {
  1549. mpreal x(v);
  1550. *exp = x.get_exp();
  1551. x.set_exp(0);
  1552. return x;
  1553. }
  1554. inline const mpreal ldexp(const mpreal& v, mp_exp_t exp)
  1555. {
  1556. mpreal x(v);
  1557. // rounding is not important since we just increasing the exponent
  1558. mpfr_mul_2si(x.mp,x.mp,exp,mpreal::get_default_rnd());
  1559. return x;
  1560. }
  1561. inline mpreal machine_epsilon(mp_prec_t prec)
  1562. {
  1563. /* the smallest eps such that 1 + eps != 1 */
  1564. return machine_epsilon(mpreal(1, prec));
  1565. }
  1566. inline mpreal machine_epsilon(const mpreal& x)
  1567. {
  1568. /* the smallest eps such that x + eps != x */
  1569. if( x < 0)
  1570. {
  1571. return nextabove(-x)+x;
  1572. }else{
  1573. return nextabove(x)-x;
  1574. }
  1575. }
  1576. // minval is 'safe' meaning 1 / minval does not overflow
  1577. inline mpreal minval(mp_prec_t prec)
  1578. {
  1579. /* min = 1/2 * 2^emin = 2^(emin - 1) */
  1580. return mpreal(1, prec) << mpreal::get_emin()-1;
  1581. }
  1582. // maxval is 'safe' meaning 1 / maxval does not underflow
  1583. inline mpreal maxval(mp_prec_t prec)
  1584. {
  1585. /* max = (1 - eps) * 2^emax, eps is machine epsilon */
  1586. return (mpreal(1, prec) - machine_epsilon(prec)) << mpreal::get_emax();
  1587. }
  1588. inline bool isEqualUlps(const mpreal& a, const mpreal& b, int maxUlps)
  1589. {
  1590. return abs(a - b) <= machine_epsilon((max)(abs(a), abs(b))) * maxUlps;
  1591. }
  1592. inline bool isEqualFuzzy(const mpreal& a, const mpreal& b, const mpreal& eps)
  1593. {
  1594. return abs(a - b) <= (min)(abs(a), abs(b)) * eps;
  1595. }
  1596. inline bool isEqualFuzzy(const mpreal& a, const mpreal& b)
  1597. {
  1598. return isEqualFuzzy(a, b, machine_epsilon((min)(abs(a), abs(b))));
  1599. }
  1600. inline const mpreal modf(const mpreal& v, mpreal& n)
  1601. {
  1602. mpreal frac(v);
  1603. // rounding is not important since we are using the same number
  1604. mpfr_frac(frac.mp,frac.mp,mpreal::get_default_rnd());
  1605. mpfr_trunc(n.mp,v.mp);
  1606. return frac;
  1607. }
  1608. inline int mpreal::check_range (int t, mp_rnd_t rnd_mode)
  1609. {
  1610. return mpfr_check_range(mp,t,rnd_mode);
  1611. }
  1612. inline int mpreal::subnormalize (int t,mp_rnd_t rnd_mode)
  1613. {
  1614. int r = mpfr_subnormalize(mp,t,rnd_mode);
  1615. MPREAL_MSVC_DEBUGVIEW_CODE;
  1616. return r;
  1617. }
  1618. inline mp_exp_t mpreal::get_emin (void)
  1619. {
  1620. return mpfr_get_emin();
  1621. }
  1622. inline int mpreal::set_emin (mp_exp_t exp)
  1623. {
  1624. return mpfr_set_emin(exp);
  1625. }
  1626. inline mp_exp_t mpreal::get_emax (void)
  1627. {
  1628. return mpfr_get_emax();
  1629. }
  1630. inline int mpreal::set_emax (mp_exp_t exp)
  1631. {
  1632. return mpfr_set_emax(exp);
  1633. }
  1634. inline mp_exp_t mpreal::get_emin_min (void)
  1635. {
  1636. return mpfr_get_emin_min();
  1637. }
  1638. inline mp_exp_t mpreal::get_emin_max (void)
  1639. {
  1640. return mpfr_get_emin_max();
  1641. }
  1642. inline mp_exp_t mpreal::get_emax_min (void)
  1643. {
  1644. return mpfr_get_emax_min();
  1645. }
  1646. inline mp_exp_t mpreal::get_emax_max (void)
  1647. {
  1648. return mpfr_get_emax_max();
  1649. }
  1650. //////////////////////////////////////////////////////////////////////////
  1651. // Mathematical Functions
  1652. //////////////////////////////////////////////////////////////////////////
  1653. #define MPREAL_UNARY_MATH_FUNCTION_BODY(f) \
  1654. mpreal y(0, mpfr_get_prec(x.mpfr_srcptr())); \
  1655. mpfr_##f(y.mpfr_ptr(), x.mpfr_srcptr(), r); \
  1656. return y;
  1657. inline const mpreal sqr (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(sqr ); }
  1658. inline const mpreal sqrt (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(sqrt); }
  1659. inline const mpreal sqrt(const unsigned long int x, mp_rnd_t r)
  1660. {
  1661. mpreal y;
  1662. mpfr_sqrt_ui(y.mpfr_ptr(), x, r);
  1663. return y;
  1664. }
  1665. inline const mpreal sqrt(const unsigned int v, mp_rnd_t rnd_mode)
  1666. {
  1667. return sqrt(static_cast<unsigned long int>(v),rnd_mode);
  1668. }
  1669. inline const mpreal sqrt(const long int v, mp_rnd_t rnd_mode)
  1670. {
  1671. if (v>=0) return sqrt(static_cast<unsigned long int>(v),rnd_mode);
  1672. else return mpreal().setNan(); // NaN
  1673. }
  1674. inline const mpreal sqrt(const int v, mp_rnd_t rnd_mode)
  1675. {
  1676. if (v>=0) return sqrt(static_cast<unsigned long int>(v),rnd_mode);
  1677. else return mpreal().setNan(); // NaN
  1678. }
  1679. inline const mpreal root(const mpreal& x, unsigned long int k, mp_rnd_t r)
  1680. {
  1681. mpreal y(0, mpfr_get_prec(x.mpfr_srcptr()));
  1682. mpfr_root(y.mpfr_ptr(), x.mpfr_srcptr(), k, r);
  1683. return y;
  1684. }
  1685. inline const mpreal dim(const mpreal& a, const mpreal& b, mp_rnd_t r)
  1686. {
  1687. mpreal y(0, mpfr_get_prec(a.mpfr_srcptr()));
  1688. mpfr_dim(y.mpfr_ptr(), a.mpfr_srcptr(), b.mpfr_srcptr(), r);
  1689. return y;
  1690. }
  1691. inline int cmpabs(const mpreal& a,const mpreal& b)
  1692. {
  1693. return mpfr_cmpabs(a.mp,b.mp);
  1694. }
  1695. inline int sin_cos(mpreal& s, mpreal& c, const mpreal& v, mp_rnd_t rnd_mode)
  1696. {
  1697. return mpfr_sin_cos(s.mp,c.mp,v.mp,rnd_mode);
  1698. }
  1699. inline const mpreal sqrt (const long double v, mp_rnd_t rnd_mode) { return sqrt(mpreal(v),rnd_mode); }
  1700. inline const mpreal sqrt (const double v, mp_rnd_t rnd_mode) { return sqrt(mpreal(v),rnd_mode); }
  1701. inline const mpreal cbrt (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(cbrt ); }
  1702. inline const mpreal fabs (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(abs ); }
  1703. inline const mpreal abs (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(abs ); }
  1704. inline const mpreal log (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(log ); }
  1705. inline const mpreal log2 (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(log2 ); }
  1706. inline const mpreal log10 (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(log10); }
  1707. inline const mpreal exp (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(exp ); }
  1708. inline const mpreal exp2 (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(exp2 ); }
  1709. inline const mpreal exp10 (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(exp10); }
  1710. inline const mpreal cos (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(cos ); }
  1711. inline const mpreal sin (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(sin ); }
  1712. inline const mpreal tan (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(tan ); }
  1713. inline const mpreal sec (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(sec ); }
  1714. inline const mpreal csc (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(csc ); }
  1715. inline const mpreal cot (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(cot ); }
  1716. inline const mpreal acos (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(acos); }
  1717. inline const mpreal asin (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(asin); }
  1718. inline const mpreal atan (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(atan); }
  1719. inline const mpreal acot (const mpreal& v, mp_rnd_t r) { return atan (1/v, r); }
  1720. inline const mpreal asec (const mpreal& v, mp_rnd_t r) { return acos (1/v, r); }
  1721. inline const mpreal acsc (const mpreal& v, mp_rnd_t r) { return asin (1/v, r); }
  1722. inline const mpreal acoth (const mpreal& v, mp_rnd_t r) { return atanh(1/v, r); }
  1723. inline const mpreal asech (const mpreal& v, mp_rnd_t r) { return acosh(1/v, r); }
  1724. inline const mpreal acsch (const mpreal& v, mp_rnd_t r) { return asinh(1/v, r); }
  1725. inline const mpreal cosh (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(cosh ); }
  1726. inline const mpreal sinh (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(sinh ); }
  1727. inline const mpreal tanh (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(tanh ); }
  1728. inline const mpreal sech (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(sech ); }
  1729. inline const mpreal csch (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(csch ); }
  1730. inline const mpreal coth (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(coth ); }
  1731. inline const mpreal acosh (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(acosh); }
  1732. inline const mpreal asinh (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(asinh); }
  1733. inline const mpreal atanh (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(atanh); }
  1734. inline const mpreal log1p (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(log1p ); }
  1735. inline const mpreal expm1 (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(expm1 ); }
  1736. inline const mpreal eint (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(eint ); }
  1737. inline const mpreal gamma (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(gamma ); }
  1738. inline const mpreal lngamma (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(lngamma); }
  1739. inline const mpreal zeta (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(zeta ); }
  1740. inline const mpreal erf (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(erf ); }
  1741. inline const mpreal erfc (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(erfc ); }
  1742. inline const mpreal besselj0(const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(j0 ); }
  1743. inline const mpreal besselj1(const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(j1 ); }
  1744. inline const mpreal bessely0(const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(y0 ); }
  1745. inline const mpreal bessely1(const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(y1 ); }
  1746. inline const mpreal atan2 (const mpreal& y, const mpreal& x, mp_rnd_t rnd_mode)
  1747. {
  1748. mpreal a;
  1749. mp_prec_t yp, xp;
  1750. yp = y.get_prec();
  1751. xp = x.get_prec();
  1752. a.set_prec(yp>xp?yp:xp);
  1753. mpfr_atan2(a.mp, y.mp, x.mp, rnd_mode);
  1754. return a;
  1755. }
  1756. inline const mpreal hypot (const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode)
  1757. {
  1758. mpreal a;
  1759. mp_prec_t yp, xp;
  1760. yp = y.get_prec();
  1761. xp = x.get_prec();
  1762. a.set_prec(yp>xp?yp:xp);
  1763. mpfr_hypot(a.mp, x.mp, y.mp, rnd_mode);
  1764. return a;
  1765. }
  1766. inline const mpreal remainder (const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode)
  1767. {
  1768. mpreal a;
  1769. mp_prec_t yp, xp;
  1770. yp = y.get_prec();
  1771. xp = x.get_prec();
  1772. a.set_prec(yp>xp?yp:xp);
  1773. mpfr_remainder(a.mp, x.mp, y.mp, rnd_mode);
  1774. return a;
  1775. }
  1776. inline const mpreal remquo (long* q, const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode)
  1777. {
  1778. mpreal a;
  1779. mp_prec_t yp, xp;
  1780. yp = y.get_prec();
  1781. xp = x.get_prec();
  1782. a.set_prec(yp>xp?yp:xp);
  1783. mpfr_remquo(a.mp,q, x.mp, y.mp, rnd_mode);
  1784. return a;
  1785. }
  1786. inline const mpreal fac_ui (unsigned long int v, mp_prec_t prec, mp_rnd_t rnd_mode)
  1787. {
  1788. mpreal x(0, prec);
  1789. mpfr_fac_ui(x.mp,v,rnd_mode);
  1790. return x;
  1791. }
  1792. inline const mpreal lgamma (const mpreal& v, int *signp, mp_rnd_t rnd_mode)
  1793. {
  1794. mpreal x(v);
  1795. int tsignp;
  1796. if(signp) mpfr_lgamma(x.mp,signp,v.mp,rnd_mode);
  1797. else mpfr_lgamma(x.mp,&tsignp,v.mp,rnd_mode);
  1798. return x;
  1799. }
  1800. inline const mpreal besseljn (long n, const mpreal& x, mp_rnd_t r)
  1801. {
  1802. mpreal y(0, mpfr_get_prec(x.mpfr_srcptr()));
  1803. mpfr_jn(y.mpfr_ptr(), n, x.mpfr_srcptr(), r);
  1804. return y;
  1805. }
  1806. inline const mpreal besselyn (long n, const mpreal& x, mp_rnd_t r)
  1807. {
  1808. mpreal y(0, mpfr_get_prec(x.mpfr_srcptr()));
  1809. mpfr_yn(y.mpfr_ptr(), n, x.mpfr_srcptr(), r);
  1810. return y;
  1811. }
  1812. inline const mpreal fma (const mpreal& v1, const mpreal& v2, const mpreal& v3, mp_rnd_t rnd_mode)
  1813. {
  1814. mpreal a;
  1815. mp_prec_t p1, p2, p3;
  1816. p1 = v1.get_prec();
  1817. p2 = v2.get_prec();
  1818. p3 = v3.get_prec();
  1819. a.set_prec(p3>p2?(p3>p1?p3:p1):(p2>p1?p2:p1));
  1820. mpfr_fma(a.mp,v1.mp,v2.mp,v3.mp,rnd_mode);
  1821. return a;
  1822. }
  1823. inline const mpreal fms (const mpreal& v1, const mpreal& v2, const mpreal& v3, mp_rnd_t rnd_mode)
  1824. {
  1825. mpreal a;
  1826. mp_prec_t p1, p2, p3;
  1827. p1 = v1.get_prec();
  1828. p2 = v2.get_prec();
  1829. p3 = v3.get_prec();
  1830. a.set_prec(p3>p2?(p3>p1?p3:p1):(p2>p1?p2:p1));
  1831. mpfr_fms(a.mp,v1.mp,v2.mp,v3.mp,rnd_mode);
  1832. return a;
  1833. }
  1834. inline const mpreal agm (const mpreal& v1, const mpreal& v2, mp_rnd_t rnd_mode)
  1835. {
  1836. mpreal a;
  1837. mp_prec_t p1, p2;
  1838. p1 = v1.get_prec();
  1839. p2 = v2.get_prec();
  1840. a.set_prec(p1>p2?p1:p2);
  1841. mpfr_agm(a.mp, v1.mp, v2.mp, rnd_mode);
  1842. return a;
  1843. }
  1844. inline const mpreal sum (const mpreal tab[], unsigned long int n, mp_rnd_t rnd_mode)
  1845. {
  1846. mpreal x;
  1847. mpfr_ptr* t;
  1848. unsigned long int i;
  1849. t = new mpfr_ptr[n];
  1850. for (i=0;i<n;i++) t[i] = (mpfr_ptr)tab[i].mp;
  1851. mpfr_sum(x.mp,t,n,rnd_mode);
  1852. delete[] t;
  1853. return x;
  1854. }
  1855. //////////////////////////////////////////////////////////////////////////
  1856. // MPFR 2.4.0 Specifics
  1857. #if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
  1858. inline int sinh_cosh(mpreal& s, mpreal& c, const mpreal& v, mp_rnd_t rnd_mode)
  1859. {
  1860. return mpfr_sinh_cosh(s.mp,c.mp,v.mp,rnd_mode);
  1861. }
  1862. inline const mpreal li2 (const mpreal& x, mp_rnd_t r)
  1863. {
  1864. MPREAL_UNARY_MATH_FUNCTION_BODY(li2);
  1865. }
  1866. inline const mpreal rem (const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode)
  1867. {
  1868. /* R = rem(X,Y) if Y != 0, returns X - n * Y where n = trunc(X/Y). */
  1869. return fmod(x, y, rnd_mode);
  1870. }
  1871. inline const mpreal mod (const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode)
  1872. {
  1873. (void)rnd_mode;
  1874. /*
  1875. m = mod(x,y) if y != 0, returns x - n*y where n = floor(x/y)
  1876. The following are true by convention:
  1877. - mod(x,0) is x
  1878. - mod(x,x) is 0
  1879. - mod(x,y) for x != y and y != 0 has the same sign as y.
  1880. */
  1881. if(iszero(y)) return x;
  1882. if(x == y) return 0;
  1883. mpreal m = x - floor(x / y) * y;
  1884. m.setSign(sgn(y)); // make sure result has the same sign as Y
  1885. return m;
  1886. }
  1887. inline const mpreal fmod (const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode)
  1888. {
  1889. mpreal a;
  1890. mp_prec_t yp, xp;
  1891. yp = y.get_prec();
  1892. xp = x.get_prec();
  1893. a.set_prec(yp>xp?yp:xp);
  1894. mpfr_fmod(a.mp, x.mp, y.mp, rnd_mode);
  1895. return a;
  1896. }
  1897. inline const mpreal rec_sqrt(const mpreal& v, mp_rnd_t rnd_mode)
  1898. {
  1899. mpreal x(v);
  1900. mpfr_rec_sqrt(x.mp,v.mp,rnd_mode);
  1901. return x;
  1902. }
  1903. #endif // MPFR 2.4.0 Specifics
  1904. //////////////////////////////////////////////////////////////////////////
  1905. // MPFR 3.0.0 Specifics
  1906. #if (MPFR_VERSION >= MPFR_VERSION_NUM(3,0,0))
  1907. inline const mpreal digamma (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(digamma); }
  1908. inline const mpreal ai (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(ai); }
  1909. #endif // MPFR 3.0.0 Specifics
  1910. //////////////////////////////////////////////////////////////////////////
  1911. // Constants
  1912. inline const mpreal const_log2 (mp_prec_t p, mp_rnd_t r)
  1913. {
  1914. mpreal x(0, p);
  1915. mpfr_const_log2(x.mpfr_ptr(), r);
  1916. return x;
  1917. }
  1918. inline const mpreal const_pi (mp_prec_t p, mp_rnd_t r)
  1919. {
  1920. mpreal x(0, p);
  1921. mpfr_const_pi(x.mpfr_ptr(), r);
  1922. return x;
  1923. }
  1924. inline const mpreal const_euler (mp_prec_t p, mp_rnd_t r)
  1925. {
  1926. mpreal x(0, p);
  1927. mpfr_const_euler(x.mpfr_ptr(), r);
  1928. return x;
  1929. }
  1930. inline const mpreal const_catalan (mp_prec_t p, mp_rnd_t r)
  1931. {
  1932. mpreal x(0, p);
  1933. mpfr_const_catalan(x.mpfr_ptr(), r);
  1934. return x;
  1935. }
  1936. inline const mpreal const_infinity (int sign, mp_prec_t p, mp_rnd_t /*r*/)
  1937. {
  1938. mpreal x(0, p);
  1939. mpfr_set_inf(x.mpfr_ptr(), sign);
  1940. return x;
  1941. }
  1942. //////////////////////////////////////////////////////////////////////////
  1943. // Integer Related Functions
  1944. inline const mpreal ceil(const mpreal& v)
  1945. {
  1946. mpreal x(v);
  1947. mpfr_ceil(x.mp,v.mp);
  1948. return x;
  1949. }
  1950. inline const mpreal floor(const mpreal& v)
  1951. {
  1952. mpreal x(v);
  1953. mpfr_floor(x.mp,v.mp);
  1954. return x;
  1955. }
  1956. inline const mpreal round(const mpreal& v)
  1957. {
  1958. mpreal x(v);
  1959. mpfr_round(x.mp,v.mp);
  1960. return x;
  1961. }
  1962. inline const mpreal trunc(const mpreal& v)
  1963. {
  1964. mpreal x(v);
  1965. mpfr_trunc(x.mp,v.mp);
  1966. return x;
  1967. }
  1968. inline const mpreal rint (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(rint ); }
  1969. inline const mpreal rint_ceil (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(rint_ceil ); }
  1970. inline const mpreal rint_floor (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(rint_floor); }
  1971. inline const mpreal rint_round (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(rint_round); }
  1972. inline const mpreal rint_trunc (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(rint_trunc); }
  1973. inline const mpreal frac (const mpreal& x, mp_rnd_t r) { MPREAL_UNARY_MATH_FUNCTION_BODY(frac ); }
  1974. //////////////////////////////////////////////////////////////////////////
  1975. // Miscellaneous Functions
  1976. inline void swap (mpreal& a, mpreal& b) { mpfr_swap(a.mp,b.mp); }
  1977. inline const mpreal (max)(const mpreal& x, const mpreal& y){ return (x>y?x:y); }
  1978. inline const mpreal (min)(const mpreal& x, const mpreal& y){ return (x<y?x:y); }
  1979. inline const mpreal fmax(const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode)
  1980. {
  1981. mpreal a;
  1982. mpfr_max(a.mp,x.mp,y.mp,rnd_mode);
  1983. return a;
  1984. }
  1985. inline const mpreal fmin(const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode)
  1986. {
  1987. mpreal a;
  1988. mpfr_min(a.mp,x.mp,y.mp,rnd_mode);
  1989. return a;
  1990. }
  1991. inline const mpreal nexttoward (const mpreal& x, const mpreal& y)
  1992. {
  1993. mpreal a(x);
  1994. mpfr_nexttoward(a.mp,y.mp);
  1995. return a;
  1996. }
  1997. inline const mpreal nextabove (const mpreal& x)
  1998. {
  1999. mpreal a(x);
  2000. mpfr_nextabove(a.mp);
  2001. return a;
  2002. }
  2003. inline const mpreal nextbelow (const mpreal& x)
  2004. {
  2005. mpreal a(x);
  2006. mpfr_nextbelow(a.mp);
  2007. return a;
  2008. }
  2009. inline const mpreal urandomb (gmp_randstate_t& state)
  2010. {
  2011. mpreal x;
  2012. mpfr_urandomb(x.mp,state);
  2013. return x;
  2014. }
  2015. #if (MPFR_VERSION >= MPFR_VERSION_NUM(3,0,0))
  2016. // use gmp_randinit_default() to init state, gmp_randclear() to clear
  2017. inline const mpreal urandom (gmp_randstate_t& state, mp_rnd_t rnd_mode)
  2018. {
  2019. mpreal x;
  2020. mpfr_urandom(x.mp,state,rnd_mode);
  2021. return x;
  2022. }
  2023. #endif
  2024. #if (MPFR_VERSION <= MPFR_VERSION_NUM(2,4,2))
  2025. inline const mpreal random2 (mp_size_t size, mp_exp_t exp)
  2026. {
  2027. mpreal x;
  2028. mpfr_random2(x.mp,size,exp);
  2029. return x;
  2030. }
  2031. #endif
  2032. // Uniformly distributed random number generation
  2033. // a = random(seed); <- initialization & first random number generation
  2034. // a = random(); <- next random numbers generation
  2035. // seed != 0
  2036. inline const mpreal random(unsigned int seed)
  2037. {
  2038. #if (MPFR_VERSION >= MPFR_VERSION_NUM(3,0,0))
  2039. static gmp_randstate_t state;
  2040. static bool isFirstTime = true;
  2041. if(isFirstTime)
  2042. {
  2043. gmp_randinit_default(state);
  2044. gmp_randseed_ui(state,0);
  2045. isFirstTime = false;
  2046. }
  2047. if(seed != 0) gmp_randseed_ui(state,seed);
  2048. return mpfr::urandom(state);
  2049. #else
  2050. if(seed != 0) std::srand(seed);
  2051. return mpfr::mpreal(std::rand()/(double)RAND_MAX);
  2052. #endif
  2053. }
  2054. //////////////////////////////////////////////////////////////////////////
  2055. // Set/Get global properties
  2056. inline void mpreal::set_default_prec(mp_prec_t prec)
  2057. {
  2058. mpfr_set_default_prec(prec);
  2059. }
  2060. inline void mpreal::set_default_rnd(mp_rnd_t rnd_mode)
  2061. {
  2062. mpfr_set_default_rounding_mode(rnd_mode);
  2063. }
  2064. inline bool mpreal::fits_in_bits(double x, int n)
  2065. {
  2066. int i;
  2067. double t;
  2068. return IsInf(x) || (std::modf ( std::ldexp ( std::frexp ( x, &i ), n ), &t ) == 0.0);
  2069. }
  2070. inline const mpreal pow(const mpreal& a, const mpreal& b, mp_rnd_t rnd_mode)
  2071. {
  2072. mpreal x(a);
  2073. mpfr_pow(x.mp,x.mp,b.mp,rnd_mode);
  2074. return x;
  2075. }
  2076. inline const mpreal pow(const mpreal& a, const mpz_t b, mp_rnd_t rnd_mode)
  2077. {
  2078. mpreal x(a);
  2079. mpfr_pow_z(x.mp,x.mp,b,rnd_mode);
  2080. return x;
  2081. }
  2082. inline const mpreal pow(const mpreal& a, const unsigned long int b, mp_rnd_t rnd_mode)
  2083. {
  2084. mpreal x(a);
  2085. mpfr_pow_ui(x.mp,x.mp,b,rnd_mode);
  2086. return x;
  2087. }
  2088. inline const mpreal pow(const mpreal& a, const unsigned int b, mp_rnd_t rnd_mode)
  2089. {
  2090. return pow(a,static_cast<unsigned long int>(b),rnd_mode);
  2091. }
  2092. inline const mpreal pow(const mpreal& a, const long int b, mp_rnd_t rnd_mode)
  2093. {
  2094. mpreal x(a);
  2095. mpfr_pow_si(x.mp,x.mp,b,rnd_mode);
  2096. return x;
  2097. }
  2098. inline const mpreal pow(const mpreal& a, const int b, mp_rnd_t rnd_mode)
  2099. {
  2100. return pow(a,static_cast<long int>(b),rnd_mode);
  2101. }
  2102. inline const mpreal pow(const mpreal& a, const long double b, mp_rnd_t rnd_mode)
  2103. {
  2104. return pow(a,mpreal(b),rnd_mode);
  2105. }
  2106. inline const mpreal pow(const mpreal& a, const double b, mp_rnd_t rnd_mode)
  2107. {
  2108. return pow(a,mpreal(b),rnd_mode);
  2109. }
  2110. inline const mpreal pow(const unsigned long int a, const mpreal& b, mp_rnd_t rnd_mode)
  2111. {
  2112. mpreal x(a);
  2113. mpfr_ui_pow(x.mp,a,b.mp,rnd_mode);
  2114. return x;
  2115. }
  2116. inline const mpreal pow(const unsigned int a, const mpreal& b, mp_rnd_t rnd_mode)
  2117. {
  2118. return pow(static_cast<unsigned long int>(a),b,rnd_mode);
  2119. }
  2120. inline const mpreal pow(const long int a, const mpreal& b, mp_rnd_t rnd_mode)
  2121. {
  2122. if (a>=0) return pow(static_cast<unsigned long int>(a),b,rnd_mode);
  2123. else return pow(mpreal(a),b,rnd_mode);
  2124. }
  2125. inline const mpreal pow(const int a, const mpreal& b, mp_rnd_t rnd_mode)
  2126. {
  2127. if (a>=0) return pow(static_cast<unsigned long int>(a),b,rnd_mode);
  2128. else return pow(mpreal(a),b,rnd_mode);
  2129. }
  2130. inline const mpreal pow(const long double a, const mpreal& b, mp_rnd_t rnd_mode)
  2131. {
  2132. return pow(mpreal(a),b,rnd_mode);
  2133. }
  2134. inline const mpreal pow(const double a, const mpreal& b, mp_rnd_t rnd_mode)
  2135. {
  2136. return pow(mpreal(a),b,rnd_mode);
  2137. }
  2138. // pow unsigned long int
  2139. inline const mpreal pow(const unsigned long int a, const unsigned long int b, mp_rnd_t rnd_mode)
  2140. {
  2141. mpreal x(a);
  2142. mpfr_ui_pow_ui(x.mp,a,b,rnd_mode);
  2143. return x;
  2144. }
  2145. inline const mpreal pow(const unsigned long int a, const unsigned int b, mp_rnd_t rnd_mode)
  2146. {
  2147. return pow(a,static_cast<unsigned long int>(b),rnd_mode); //mpfr_ui_pow_ui
  2148. }
  2149. inline const mpreal pow(const unsigned long int a, const long int b, mp_rnd_t rnd_mode)
  2150. {
  2151. if(b>0) return pow(a,static_cast<unsigned long int>(b),rnd_mode); //mpfr_ui_pow_ui
  2152. else return pow(a,mpreal(b),rnd_mode); //mpfr_ui_pow
  2153. }
  2154. inline const mpreal pow(const unsigned long int a, const int b, mp_rnd_t rnd_mode)
  2155. {
  2156. if(b>0) return pow(a,static_cast<unsigned long int>(b),rnd_mode); //mpfr_ui_pow_ui
  2157. else return pow(a,mpreal(b),rnd_mode); //mpfr_ui_pow
  2158. }
  2159. inline const mpreal pow(const unsigned long int a, const long double b, mp_rnd_t rnd_mode)
  2160. {
  2161. return pow(a,mpreal(b),rnd_mode); //mpfr_ui_pow
  2162. }
  2163. inline const mpreal pow(const unsigned long int a, const double b, mp_rnd_t rnd_mode)
  2164. {
  2165. return pow(a,mpreal(b),rnd_mode); //mpfr_ui_pow
  2166. }
  2167. // pow unsigned int
  2168. inline const mpreal pow(const unsigned int a, const unsigned long int b, mp_rnd_t rnd_mode)
  2169. {
  2170. return pow(static_cast<unsigned long int>(a),b,rnd_mode); //mpfr_ui_pow_ui
  2171. }
  2172. inline const mpreal pow(const unsigned int a, const unsigned int b, mp_rnd_t rnd_mode)
  2173. {
  2174. return pow(static_cast<unsigned long int>(a),static_cast<unsigned long int>(b),rnd_mode); //mpfr_ui_pow_ui
  2175. }
  2176. inline const mpreal pow(const unsigned int a, const long int b, mp_rnd_t rnd_mode)
  2177. {
  2178. if(b>0) return pow(static_cast<unsigned long int>(a),static_cast<unsigned long int>(b),rnd_mode); //mpfr_ui_pow_ui
  2179. else return pow(static_cast<unsigned long int>(a),mpreal(b),rnd_mode); //mpfr_ui_pow
  2180. }
  2181. inline const mpreal pow(const unsigned int a, const int b, mp_rnd_t rnd_mode)
  2182. {
  2183. if(b>0) return pow(static_cast<unsigned long int>(a),static_cast<unsigned long int>(b),rnd_mode); //mpfr_ui_pow_ui
  2184. else return pow(static_cast<unsigned long int>(a),mpreal(b),rnd_mode); //mpfr_ui_pow
  2185. }
  2186. inline const mpreal pow(const unsigned int a, const long double b, mp_rnd_t rnd_mode)
  2187. {
  2188. return pow(static_cast<unsigned long int>(a),mpreal(b),rnd_mode); //mpfr_ui_pow
  2189. }
  2190. inline const mpreal pow(const unsigned int a, const double b, mp_rnd_t rnd_mode)
  2191. {
  2192. return pow(static_cast<unsigned long int>(a),mpreal(b),rnd_mode); //mpfr_ui_pow
  2193. }
  2194. // pow long int
  2195. inline const mpreal pow(const long int a, const unsigned long int b, mp_rnd_t rnd_mode)
  2196. {
  2197. if (a>0) return pow(static_cast<unsigned long int>(a),b,rnd_mode); //mpfr_ui_pow_ui
  2198. else return pow(mpreal(a),b,rnd_mode); //mpfr_pow_ui
  2199. }
  2200. inline const mpreal pow(const long int a, const unsigned int b, mp_rnd_t rnd_mode)
  2201. {
  2202. if (a>0) return pow(static_cast<unsigned long int>(a),static_cast<unsigned long int>(b),rnd_mode); //mpfr_ui_pow_ui
  2203. else return pow(mpreal(a),static_cast<unsigned long int>(b),rnd_mode); //mpfr_pow_ui
  2204. }
  2205. inline const mpreal pow(const long int a, const long int b, mp_rnd_t rnd_mode)
  2206. {
  2207. if (a>0)
  2208. {
  2209. if(b>0) return pow(static_cast<unsigned long int>(a),static_cast<unsigned long int>(b),rnd_mode); //mpfr_ui_pow_ui
  2210. else return pow(static_cast<unsigned long int>(a),mpreal(b),rnd_mode); //mpfr_ui_pow
  2211. }else{
  2212. return pow(mpreal(a),b,rnd_mode); // mpfr_pow_si
  2213. }
  2214. }
  2215. inline const mpreal pow(const long int a, const int b, mp_rnd_t rnd_mode)
  2216. {
  2217. if (a>0)
  2218. {
  2219. if(b>0) return pow(static_cast<unsigned long int>(a),static_cast<unsigned long int>(b),rnd_mode); //mpfr_ui_pow_ui
  2220. else return pow(static_cast<unsigned long int>(a),mpreal(b),rnd_mode); //mpfr_ui_pow
  2221. }else{
  2222. return pow(mpreal(a),static_cast<long int>(b),rnd_mode); // mpfr_pow_si
  2223. }
  2224. }
  2225. inline const mpreal pow(const long int a, const long double b, mp_rnd_t rnd_mode)
  2226. {
  2227. if (a>=0) return pow(static_cast<unsigned long int>(a),mpreal(b),rnd_mode); //mpfr_ui_pow
  2228. else return pow(mpreal(a),mpreal(b),rnd_mode); //mpfr_pow
  2229. }
  2230. inline const mpreal pow(const long int a, const double b, mp_rnd_t rnd_mode)
  2231. {
  2232. if (a>=0) return pow(static_cast<unsigned long int>(a),mpreal(b),rnd_mode); //mpfr_ui_pow
  2233. else return pow(mpreal(a),mpreal(b),rnd_mode); //mpfr_pow
  2234. }
  2235. // pow int
  2236. inline const mpreal pow(const int a, const unsigned long int b, mp_rnd_t rnd_mode)
  2237. {
  2238. if (a>0) return pow(static_cast<unsigned long int>(a),b,rnd_mode); //mpfr_ui_pow_ui
  2239. else return pow(mpreal(a),b,rnd_mode); //mpfr_pow_ui
  2240. }
  2241. inline const mpreal pow(const int a, const unsigned int b, mp_rnd_t rnd_mode)
  2242. {
  2243. if (a>0) return pow(static_cast<unsigned long int>(a),static_cast<unsigned long int>(b),rnd_mode); //mpfr_ui_pow_ui
  2244. else return pow(mpreal(a),static_cast<unsigned long int>(b),rnd_mode); //mpfr_pow_ui
  2245. }
  2246. inline const mpreal pow(const int a, const long int b, mp_rnd_t rnd_mode)
  2247. {
  2248. if (a>0)
  2249. {
  2250. if(b>0) return pow(static_cast<unsigned long int>(a),static_cast<unsigned long int>(b),rnd_mode); //mpfr_ui_pow_ui
  2251. else return pow(static_cast<unsigned long int>(a),mpreal(b),rnd_mode); //mpfr_ui_pow
  2252. }else{
  2253. return pow(mpreal(a),b,rnd_mode); // mpfr_pow_si
  2254. }
  2255. }
  2256. inline const mpreal pow(const int a, const int b, mp_rnd_t rnd_mode)
  2257. {
  2258. if (a>0)
  2259. {
  2260. if(b>0) return pow(static_cast<unsigned long int>(a),static_cast<unsigned long int>(b),rnd_mode); //mpfr_ui_pow_ui
  2261. else return pow(static_cast<unsigned long int>(a),mpreal(b),rnd_mode); //mpfr_ui_pow
  2262. }else{
  2263. return pow(mpreal(a),static_cast<long int>(b),rnd_mode); // mpfr_pow_si
  2264. }
  2265. }
  2266. inline const mpreal pow(const int a, const long double b, mp_rnd_t rnd_mode)
  2267. {
  2268. if (a>=0) return pow(static_cast<unsigned long int>(a),mpreal(b),rnd_mode); //mpfr_ui_pow
  2269. else return pow(mpreal(a),mpreal(b),rnd_mode); //mpfr_pow
  2270. }
  2271. inline const mpreal pow(const int a, const double b, mp_rnd_t rnd_mode)
  2272. {
  2273. if (a>=0) return pow(static_cast<unsigned long int>(a),mpreal(b),rnd_mode); //mpfr_ui_pow
  2274. else return pow(mpreal(a),mpreal(b),rnd_mode); //mpfr_pow
  2275. }
  2276. // pow long double
  2277. inline const mpreal pow(const long double a, const long double b, mp_rnd_t rnd_mode)
  2278. {
  2279. return pow(mpreal(a),mpreal(b),rnd_mode);
  2280. }
  2281. inline const mpreal pow(const long double a, const unsigned long int b, mp_rnd_t rnd_mode)
  2282. {
  2283. return pow(mpreal(a),b,rnd_mode); //mpfr_pow_ui
  2284. }
  2285. inline const mpreal pow(const long double a, const unsigned int b, mp_rnd_t rnd_mode)
  2286. {
  2287. return pow(mpreal(a),static_cast<unsigned long int>(b),rnd_mode); //mpfr_pow_ui
  2288. }
  2289. inline const mpreal pow(const long double a, const long int b, mp_rnd_t rnd_mode)
  2290. {
  2291. return pow(mpreal(a),b,rnd_mode); // mpfr_pow_si
  2292. }
  2293. inline const mpreal pow(const long double a, const int b, mp_rnd_t rnd_mode)
  2294. {
  2295. return pow(mpreal(a),static_cast<long int>(b),rnd_mode); // mpfr_pow_si
  2296. }
  2297. inline const mpreal pow(const double a, const double b, mp_rnd_t rnd_mode)
  2298. {
  2299. return pow(mpreal(a),mpreal(b),rnd_mode);
  2300. }
  2301. inline const mpreal pow(const double a, const unsigned long int b, mp_rnd_t rnd_mode)
  2302. {
  2303. return pow(mpreal(a),b,rnd_mode); // mpfr_pow_ui
  2304. }
  2305. inline const mpreal pow(const double a, const unsigned int b, mp_rnd_t rnd_mode)
  2306. {
  2307. return pow(mpreal(a),static_cast<unsigned long int>(b),rnd_mode); // mpfr_pow_ui
  2308. }
  2309. inline const mpreal pow(const double a, const long int b, mp_rnd_t rnd_mode)
  2310. {
  2311. return pow(mpreal(a),b,rnd_mode); // mpfr_pow_si
  2312. }
  2313. inline const mpreal pow(const double a, const int b, mp_rnd_t rnd_mode)
  2314. {
  2315. return pow(mpreal(a),static_cast<long int>(b),rnd_mode); // mpfr_pow_si
  2316. }
  2317. } // End of mpfr namespace
  2318. // Explicit specialization of std::swap for mpreal numbers
  2319. // Thus standard algorithms will use efficient version of swap (due to Koenig lookup)
  2320. // Non-throwing swap C++ idiom: http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Non-throwing_swap
  2321. namespace std
  2322. {
  2323. // only allowed to extend namespace std with specializations
  2324. template <>
  2325. inline void swap(mpfr::mpreal& x, mpfr::mpreal& y)
  2326. {
  2327. return mpfr::swap(x, y);
  2328. }
  2329. template<>
  2330. class numeric_limits<mpfr::mpreal>
  2331. {
  2332. public:
  2333. static const bool is_specialized = true;
  2334. static const bool is_signed = true;
  2335. static const bool is_integer = false;
  2336. static const bool is_exact = false;
  2337. static const int radix = 2;
  2338. static const bool has_infinity = true;
  2339. static const bool has_quiet_NaN = true;
  2340. static const bool has_signaling_NaN = true;
  2341. static const bool is_iec559 = true; // = IEEE 754
  2342. static const bool is_bounded = true;
  2343. static const bool is_modulo = false;
  2344. static const bool traps = true;
  2345. static const bool tinyness_before = true;
  2346. static const float_denorm_style has_denorm = denorm_absent;
  2347. inline static float_round_style round_style()
  2348. {
  2349. mp_rnd_t r = mpfr::mpreal::get_default_rnd();
  2350. switch (r)
  2351. {
  2352. case MPFR_RNDN: return round_to_nearest;
  2353. case MPFR_RNDZ: return round_toward_zero;
  2354. case MPFR_RNDU: return round_toward_infinity;
  2355. case MPFR_RNDD: return round_toward_neg_infinity;
  2356. default: return round_indeterminate;
  2357. }
  2358. }
  2359. inline static mpfr::mpreal (min) (mp_prec_t precision = mpfr::mpreal::get_default_prec()) { return mpfr::minval(precision); }
  2360. inline static mpfr::mpreal (max) (mp_prec_t precision = mpfr::mpreal::get_default_prec()) { return mpfr::maxval(precision); }
  2361. inline static mpfr::mpreal lowest (mp_prec_t precision = mpfr::mpreal::get_default_prec()) { return -mpfr::maxval(precision); }
  2362. // Returns smallest eps such that 1 + eps != 1 (classic machine epsilon)
  2363. inline static mpfr::mpreal epsilon(mp_prec_t precision = mpfr::mpreal::get_default_prec()) { return mpfr::machine_epsilon(precision); }
  2364. // Returns smallest eps such that x + eps != x (relative machine epsilon)
  2365. inline static mpfr::mpreal epsilon(const mpfr::mpreal& x) { return mpfr::machine_epsilon(x); }
  2366. inline static mpfr::mpreal round_error(mp_prec_t precision = mpfr::mpreal::get_default_prec())
  2367. {
  2368. mp_rnd_t r = mpfr::mpreal::get_default_rnd();
  2369. if(r == MPFR_RNDN) return mpfr::mpreal(0.5, precision);
  2370. else return mpfr::mpreal(1.0, precision);
  2371. }
  2372. inline static const mpfr::mpreal infinity() { return mpfr::const_infinity(); }
  2373. inline static const mpfr::mpreal quiet_NaN() { return mpfr::mpreal().setNan(); }
  2374. inline static const mpfr::mpreal signaling_NaN() { return mpfr::mpreal().setNan(); }
  2375. inline static const mpfr::mpreal denorm_min() { return (min)(); }
  2376. // Please note, exponent range is not fixed in MPFR
  2377. static const int min_exponent = MPFR_EMIN_DEFAULT;
  2378. static const int max_exponent = MPFR_EMAX_DEFAULT;
  2379. MPREAL_PERMISSIVE_EXPR static const int min_exponent10 = (int) (MPFR_EMIN_DEFAULT * 0.3010299956639811);
  2380. MPREAL_PERMISSIVE_EXPR static const int max_exponent10 = (int) (MPFR_EMAX_DEFAULT * 0.3010299956639811);
  2381. // Should be constant according to standard, but 'digits' depends on precision in MPFR
  2382. inline static int digits() { return mpfr::mpreal::get_default_prec(); }
  2383. inline static int digits(const mpfr::mpreal& x) { return x.getPrecision(); }
  2384. inline static int digits10(mp_prec_t precision = mpfr::mpreal::get_default_prec())
  2385. {
  2386. return mpfr::bits2digits(precision);
  2387. }
  2388. inline static int digits10(const mpfr::mpreal& x)
  2389. {
  2390. return mpfr::bits2digits(x.getPrecision());
  2391. }
  2392. inline static int max_digits10(mp_prec_t precision = mpfr::mpreal::get_default_prec())
  2393. {
  2394. return digits10(precision);
  2395. }
  2396. };
  2397. }
  2398. #endif /* __MPREAL_H__ */