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.

3104 lines
116 KiB

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