NanoStructures  1.0
DMFT solver for layered, strongly correlated nanostructures
mpreal.h
1 /*
2  Multi-precision real number class. C++ wrapper fo MPFR library.
3  Project homepage: http://www.holoborodko.com/pavel/
4  Contact e-mail: pavel@holoborodko.com
5 
6  Copyright (c) 2008-2009 Pavel Holoborodko
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22  Contributors:
23  Brain Gladman, Helmut Jarausch, Fokko Beekhof, Ulrich Mutze, Heinz van Saanen.
24  */
25 
26 #ifndef __MP_REAL_H__
27 #define __MP_REAL_H__
28 
29 #include <string>
30 #include <iostream>
31 #include <stdexcept>
32 #include <cfloat>
33 #include <cmath>
34 #include <mpfr.h>
35 
36 // Detect compiler using signatures from http://predef.sourceforge.net/
37 #if defined(__GNUC__) && defined(__INTEL_COMPILER)
38 #define IsInf(x) isinf(x) // GNU C/C++ + Intel ICC compiler
39 
40 #elif defined(__GNUC__)
41 #define IsInf(x) std::isinf(x) // GNU C/C++
42 
43 #elif defined(_MSC_VER)
44 #define IsInf(x) (!_finite(x)) // Microsoft Visual C++
45 
46 #else
47 #define IsInf(x) std::isinf(x) // C99 conformance
48 #endif
49 
50 namespace mpfr {
51 
52  class mpreal {
53  private:
54  mpfr_t mp;
55 
56  public:
57  static mp_rnd_t default_rnd;
58  static mp_prec_t default_prec;
59  static int default_base;
60  static int double_bits;
61 
62  public:
63  // Constructors && type conversion
64  mpreal();
65  mpreal(const mpreal& u);
66 
67  mpreal(const mpfr_t u);
68  mpreal(const mpf_t u);
69 
70  mpreal(const mpz_t u, mp_prec_t prec = default_prec, mp_rnd_t mode = default_rnd);
71  mpreal(const mpq_t u, mp_prec_t prec = default_prec, mp_rnd_t mode = default_rnd);
72  mpreal(const double u, mp_prec_t prec = default_prec, mp_rnd_t mode = default_rnd);
73  mpreal(const long double u, mp_prec_t prec = default_prec, mp_rnd_t mode = default_rnd);
74  mpreal(const unsigned long int u, mp_prec_t prec = default_prec, mp_rnd_t mode = default_rnd);
75  mpreal(const unsigned int u, mp_prec_t prec = default_prec, mp_rnd_t mode = default_rnd);
76  mpreal(const long int u, mp_prec_t prec = default_prec, mp_rnd_t mode = default_rnd);
77  mpreal(const int u, mp_prec_t prec = default_prec, mp_rnd_t mode = default_rnd);
78  mpreal(const char* s, mp_prec_t prec = default_prec, int base = default_base, mp_rnd_t mode = default_rnd);
79 
80  ~mpreal();
81 
82  // Operations
83  // =
84  // +, -, *, /, ++, --, <<, >>
85  // *=, +=, -=, /=,
86  // <, >, ==, <=, >=
87 
88  // =
89  mpreal& operator=(const mpreal& v);
90  mpreal& operator=(const mpf_t v);
91  mpreal& operator=(const mpz_t v);
92  mpreal& operator=(const mpq_t v);
93  mpreal& operator=(const long double v);
94  mpreal& operator=(const double v);
95  mpreal& operator=(const unsigned long int v);
96  mpreal& operator=(const unsigned int v);
97  mpreal& operator=(const long int v);
98  mpreal& operator=(const int v);
99  mpreal& operator=(const char* s);
100 
101  // +
102  mpreal& operator+=(const mpreal& v);
103  mpreal& operator+=(const mpf_t v);
104  mpreal& operator+=(const mpz_t v);
105  mpreal& operator+=(const mpq_t v);
106  mpreal& operator+=(const long double u);
107  mpreal& operator+=(const double u);
108  mpreal& operator+=(const unsigned long int u);
109  mpreal& operator+=(const unsigned int u);
110  mpreal& operator+=(const long int u);
111  mpreal& operator+=(const int u);
112  const mpreal operator+() const;
113  mpreal& operator++();
114  const mpreal operator++(int);
115 
116  // -
117  mpreal& operator-=(const mpreal& v);
118  mpreal& operator-=(const mpz_t v);
119  mpreal& operator-=(const mpq_t v);
120  mpreal& operator-=(const long double u);
121  mpreal& operator-=(const double u);
122  mpreal& operator-=(const unsigned long int u);
123  mpreal& operator-=(const unsigned int u);
124  mpreal& operator-=(const long int u);
125  mpreal& operator-=(const int u);
126  const mpreal operator-() const;
127  friend const mpreal operator-(const unsigned long int b, const mpreal& a);
128  friend const mpreal operator-(const unsigned int b, const mpreal& a);
129  friend const mpreal operator-(const long int b, const mpreal& a);
130  friend const mpreal operator-(const int b, const mpreal& a);
131  friend const mpreal operator-(const double b, const mpreal& a);
132  mpreal& operator--();
133  const mpreal operator--(int);
134 
135  // *
136  mpreal& operator*=(const mpreal& v);
137  mpreal& operator*=(const mpz_t v);
138  mpreal& operator*=(const mpq_t v);
139  mpreal& operator*=(const long double v);
140  mpreal& operator*=(const double v);
141  mpreal& operator*=(const unsigned long int v);
142  mpreal& operator*=(const unsigned int v);
143  mpreal& operator*=(const long int v);
144  mpreal& operator*=(const int v);
145 
146  // /
147  mpreal& operator/=(const mpreal& v);
148  mpreal& operator/=(const mpz_t v);
149  mpreal& operator/=(const mpq_t v);
150  mpreal& operator/=(const long double v);
151  mpreal& operator/=(const double v);
152  mpreal& operator/=(const unsigned long int v);
153  mpreal& operator/=(const unsigned int v);
154  mpreal& operator/=(const long int v);
155  mpreal& operator/=(const int v);
156  friend const mpreal operator/(const unsigned long int b, const mpreal& a);
157  friend const mpreal operator/(const unsigned int b, const mpreal& a);
158  friend const mpreal operator/(const long int b, const mpreal& a);
159  friend const mpreal operator/(const int b, const mpreal& a);
160  friend const mpreal operator/(const double b, const mpreal& a);
161 
162  //<<= Fast Multiplication by 2^u
163  mpreal& operator<<=(const unsigned long int u);
164  mpreal& operator<<=(const unsigned int u);
165  mpreal& operator<<=(const long int u);
166  mpreal& operator<<=(const int u);
167 
168  //>>= Fast Division by 2^u
169  mpreal& operator>>=(const unsigned long int u);
170  mpreal& operator>>=(const unsigned int u);
171  mpreal& operator>>=(const long int u);
172  mpreal& operator>>=(const int u);
173 
174  // Boolean Operators
175  friend bool operator>(const mpreal& a, const mpreal& b);
176  friend bool operator >=(const mpreal& a, const mpreal& b);
177  friend bool operator<(const mpreal& a, const mpreal& b);
178  friend bool operator <=(const mpreal& a, const mpreal& b);
179  friend bool operator ==(const mpreal& a, const mpreal& b);
180  friend bool operator !=(const mpreal& a, const mpreal& b);
181 
182  // Type Conversion operators
183  operator long double() const;
184  operator double() const;
185  operator float() const;
186  operator unsigned long() const;
187  operator unsigned int() const;
188  operator long() const;
189  operator std::string() const;
190  operator mpfr_ptr();
191 
192  // Math Functions
193  friend const mpreal sqr(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
194  friend const mpreal sqrt(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
195  friend const mpreal sqrt(const unsigned long int v, mp_rnd_t rnd_mode = mpreal::default_rnd);
196  friend const mpreal cbrt(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
197  friend const mpreal root(const mpreal& v, unsigned long int k, mp_rnd_t rnd_mode = mpreal::default_rnd);
198  friend const mpreal pow(const mpreal& a, const mpreal& b, mp_rnd_t rnd_mode = mpreal::default_rnd);
199  friend const mpreal pow(const mpreal& a, const mpz_t b, mp_rnd_t rnd_mode = mpreal::default_rnd);
200  friend const mpreal pow(const mpreal& a, const unsigned long int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
201  friend const mpreal pow(const mpreal& a, const long int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
202  friend const mpreal pow(const unsigned long int a, const mpreal& b, mp_rnd_t rnd_mode = mpreal::default_rnd);
203  friend const mpreal pow(const unsigned long int a, const unsigned long int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
204  friend const mpreal fabs(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
205  friend const mpreal abs(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
206  friend const mpreal dim(const mpreal& a, const mpreal& b, mp_rnd_t rnd_mode = mpreal::default_rnd);
207  friend const mpreal mul_2ui(const mpreal& v, unsigned long int k, mp_rnd_t rnd_mode = mpreal::default_rnd);
208  friend const mpreal mul_2si(const mpreal& v, long int k, mp_rnd_t rnd_mode = mpreal::default_rnd);
209  friend const mpreal div_2ui(const mpreal& v, unsigned long int k, mp_rnd_t rnd_mode = mpreal::default_rnd);
210  friend const mpreal div_2si(const mpreal& v, long int k, mp_rnd_t rnd_mode = mpreal::default_rnd);
211  friend int cmpabs(const mpreal& a, const mpreal& b);
212 
213  friend const mpreal log(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
214  friend const mpreal log2(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
215  friend const mpreal log10(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
216  friend const mpreal exp(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
217  friend const mpreal exp2(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
218  friend const mpreal exp10(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
219 
220  friend const mpreal cos(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
221  friend const mpreal sin(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
222  friend const mpreal tan(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
223  friend const mpreal sec(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
224  friend const mpreal csc(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
225  friend const mpreal cot(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
226  friend int sin_cos(mpreal& s, mpreal& c, const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
227 
228  friend const mpreal acos(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
229  friend const mpreal asin(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
230  friend const mpreal atan(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
231  friend const mpreal atan2(const mpreal& y, const mpreal& x, mp_rnd_t rnd_mode = mpreal::default_rnd);
232  friend const mpreal cosh(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
233  friend const mpreal sinh(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
234  friend const mpreal tanh(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
235  friend const mpreal sech(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
236  friend const mpreal csch(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
237  friend const mpreal coth(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
238 
239  friend const mpreal acosh(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
240  friend const mpreal asinh(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
241  friend const mpreal atanh(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
242  friend const mpreal fac_ui(unsigned long int v, mp_rnd_t rnd_mode = mpreal::default_rnd);
243  friend const mpreal log1p(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
244  friend const mpreal expm1(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
245  friend const mpreal eint(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
246 
247  friend const mpreal gamma(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
248  friend const mpreal lngamma(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
249  friend const mpreal lgamma(const mpreal& v, int *signp, mp_rnd_t rnd_mode = mpreal::default_rnd);
250  friend const mpreal zeta(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
251  friend const mpreal erf(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
252  friend const mpreal erfc(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
253  friend const mpreal _j0(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
254  friend const mpreal _j1(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
255  friend const mpreal _jn(long n, const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
256  friend const mpreal _y0(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
257  friend const mpreal _y1(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
258  friend const mpreal _yn(long n, const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
259  friend const mpreal fma(const mpreal& v1, const mpreal& v2, const mpreal& v3, mp_rnd_t rnd_mode = mpreal::default_rnd);
260  friend const mpreal fms(const mpreal& v1, const mpreal& v2, const mpreal& v3, mp_rnd_t rnd_mode = mpreal::default_rnd);
261  friend const mpreal agm(const mpreal& v1, const mpreal& v2, mp_rnd_t rnd_mode = mpreal::default_rnd);
262  friend const mpreal hypot(const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode = mpreal::default_rnd);
263  friend const mpreal sum(const mpreal tab[], unsigned long int n, mp_rnd_t rnd_mode = mpreal::default_rnd);
264  friend int sgn(const mpreal& v); // -1 or +1
265 
266  // MPFR 2.4.0 Specifics
267 #if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
268  friend int sinh_cosh(mpreal& s, mpreal& c, const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
269  friend const mpreal li2(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
270  friend const mpreal fmod(const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode = mpreal::default_rnd);
271  friend const mpreal rec_sqrt(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
272 #endif
273 
274  // Exponent and mantissa manipulation
275  friend const mpreal frexp(const mpreal& v, mp_exp_t* exp);
276  friend const mpreal ldexp(const mpreal& v, mp_exp_t exp);
277 
278  // Splits mpreal value into fractional and integer parts.
279  // Returns fractional part and stores integer part in n.
280  friend const mpreal modf(const mpreal& v, mpreal& n);
281 
282  // Constants
283  // don't forget to call mpfr_free_cache() for every thread where you are using const-functions
284  friend const mpreal const_log2(mp_prec_t prec = mpreal::default_prec, mp_rnd_t rnd_mode = mpreal::default_rnd);
285  friend const mpreal const_pi(mp_prec_t prec = mpreal::default_prec, mp_rnd_t rnd_mode = mpreal::default_rnd);
286  friend const mpreal const_euler(mp_prec_t prec = mpreal::default_prec, mp_rnd_t rnd_mode = mpreal::default_rnd);
287  friend const mpreal const_catalan(mp_prec_t prec = mpreal::default_prec, mp_rnd_t rnd_mode = mpreal::default_rnd);
288 
289  // Output/ Input
290  friend std::ostream& operator<<(std::ostream& os, const mpreal& v);
291  friend std::istream& operator>>(std::istream& is, mpreal& v);
292 
293  // Integer Related Functions
294  friend const mpreal rint(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
295  friend const mpreal ceil(const mpreal& v);
296  friend const mpreal floor(const mpreal& v);
297  friend const mpreal round(const mpreal& v);
298  friend const mpreal trunc(const mpreal& v);
299  friend const mpreal rint_ceil(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
300  friend const mpreal rint_floor(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
301  friend const mpreal rint_round(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
302  friend const mpreal rint_trunc(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
303  friend const mpreal frac(const mpreal& v, mp_rnd_t rnd_mode = mpreal::default_rnd);
304  friend const mpreal remainder(const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode = mpreal::default_rnd);
305  friend const mpreal remquo(long* q, const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode = mpreal::default_rnd);
306 
307  // Miscellaneous Functions
308  friend const mpreal nexttoward(const mpreal& x, const mpreal& y);
309  friend const mpreal nextabove(const mpreal& x);
310  friend const mpreal nextbelow(const mpreal& x);
311 
312  // use gmp_randinit_default() to init state, gmp_randclear() to clear
313  friend const mpreal urandomb(gmp_randstate_t& state);
314  friend const mpreal random2(mp_size_t size, mp_exp_t exp);
315 
316  // Instance Checkers
317  friend bool _isnan(const mpreal& v);
318  friend bool _isinf(const mpreal& v);
319  friend bool _isnum(const mpreal& v);
320  friend bool _iszero(const mpreal& v);
321  friend bool _isint(const mpreal& v);
322 
323  // Set/Get instance properties
324  mp_prec_t get_prec() const;
325  void set_prec(mp_prec_t prec, mp_rnd_t rnd_mode = default_rnd); // Change precision with rounding mode
326 
327  // Set mpreal to +-inf, NaN
328  void set_inf(int sign = +1);
329  void set_nan();
330 
331  // sign = -1 or +1
332  void set_sign(int sign, mp_rnd_t rnd_mode = default_rnd);
333 
334  //Exponent
335  mp_exp_t get_exp();
336  int set_exp(mp_exp_t e);
337  int check_range(int t, mp_rnd_t rnd_mode = default_rnd);
338  int subnormalize(int t, mp_rnd_t rnd_mode = default_rnd);
339 
340  // Inexact conversion from float
341  bool fits_in_bits(double x, int n);
342 
343  // Set/Get global properties
344  static void set_default_prec(mp_prec_t prec);
345  static mp_prec_t get_default_prec();
346  static void set_default_base(int base);
347  static int get_default_base();
348  static void set_double_bits(int dbits);
349  static int get_double_bits();
350  static void set_default_rnd(mp_rnd_t rnd_mode);
351  static mp_rnd_t get_default_rnd();
352  static mp_exp_t get_emin(void);
353  static mp_exp_t get_emax(void);
354  static mp_exp_t get_emin_min(void);
355  static mp_exp_t get_emin_max(void);
356  static mp_exp_t get_emax_min(void);
357  static mp_exp_t get_emax_max(void);
358  static int set_emin(mp_exp_t exp);
359  static int set_emax(mp_exp_t exp);
360 
361  // Get/Set conversions
362  // Convert mpreal to string with n significant digits in base b
363  // n = 0 -> convert with the maximum available digits
364  std::string to_string(size_t n = 0, int b = default_base, mp_rnd_t mode = default_rnd) const;
365 
366  // Efficient swapping of two mpreal values
367  friend void swap(mpreal& x, mpreal& y);
368 
369  //Min Max - macros is evil. Needed for systems which defines max and min globally as macros (e.g. Windows)
370  //Hope that globally defined macros use > < operations only
371 #ifndef max
372  friend const mpreal max(const mpreal& x, const mpreal& y);
373 #endif
374 
375 #ifndef min
376  friend const mpreal min(const mpreal& x, const mpreal& y);
377 #endif
378  };
379 
381  // Exceptions
382 
383  class conversion_overflow : public std::exception {
384  public:
385 
386  std::string why() {
387  return "inexact conversion from floating point";
388  }
389  };
390 
392  // + Addition
393  const mpreal operator+(const mpreal& a, const mpreal& b);
394 
395  // + Fast specialized addition - implemented through fast += operations
396  const mpreal operator+(const mpreal& a, const mpz_t b);
397  const mpreal operator+(const mpreal& a, const mpq_t b);
398  const mpreal operator+(const mpreal& a, const long double b);
399  const mpreal operator+(const mpreal& a, const double b);
400  const mpreal operator+(const mpreal& a, const unsigned long int b);
401  const mpreal operator+(const mpreal& a, const unsigned int b);
402  const mpreal operator+(const mpreal& a, const long int b);
403  const mpreal operator+(const mpreal& a, const int b);
404  const mpreal operator+(const mpreal& a, const char* b);
405  const mpreal operator+(const char* a, const mpreal& b);
406  const std::string operator+(const mpreal& a, const std::string b);
407  const std::string operator+(const std::string a, const mpreal& b);
408 
409  const mpreal operator+(const mpz_t b, const mpreal& a);
410  const mpreal operator+(const mpq_t b, const mpreal& a);
411  const mpreal operator+(const long double b, const mpreal& a);
412  const mpreal operator+(const double b, const mpreal& a);
413  const mpreal operator+(const unsigned long int b, const mpreal& a);
414  const mpreal operator+(const unsigned int b, const mpreal& a);
415  const mpreal operator+(const long int b, const mpreal& a);
416  const mpreal operator+(const int b, const mpreal& a);
417 
419  // - Subtraction
420  const mpreal operator-(const mpreal& a, const mpreal& b);
421 
422  // - Fast specialized subtraction - implemented through fast -= operations
423  const mpreal operator-(const mpreal& a, const mpz_t b);
424  const mpreal operator-(const mpreal& a, const mpq_t b);
425  const mpreal operator-(const mpreal& a, const long double b);
426  const mpreal operator-(const mpreal& a, const double b);
427  const mpreal operator-(const mpreal& a, const unsigned long int b);
428  const mpreal operator-(const mpreal& a, const unsigned int b);
429  const mpreal operator-(const mpreal& a, const long int b);
430  const mpreal operator-(const mpreal& a, const int b);
431  const mpreal operator-(const mpreal& a, const char* b);
432  const mpreal operator-(const char* a, const mpreal& b);
433 
434  const mpreal operator-(const mpz_t b, const mpreal& a);
435  const mpreal operator-(const mpq_t b, const mpreal& a);
436  const mpreal operator-(const long double b, const mpreal& a);
437  //const mpreal operator-(const double b, const mpreal& a);
438 
440  // * Multiplication
441  const mpreal operator*(const mpreal& a, const mpreal& b);
442 
443  // * Fast specialized multiplication - implemented through fast *= operations
444  const mpreal operator*(const mpreal& a, const mpz_t b);
445  const mpreal operator*(const mpreal& a, const mpq_t b);
446  const mpreal operator*(const mpreal& a, const long double b);
447  const mpreal operator*(const mpreal& a, const double b);
448  const mpreal operator*(const mpreal& a, const unsigned long int b);
449  const mpreal operator*(const mpreal& a, const unsigned int b);
450  const mpreal operator*(const mpreal& a, const long int b);
451  const mpreal operator*(const mpreal& a, const int b);
452 
453  const mpreal operator*(const mpz_t b, const mpreal& a);
454  const mpreal operator*(const mpq_t b, const mpreal& a);
455  const mpreal operator*(const long double b, const mpreal& a);
456  const mpreal operator*(const double b, const mpreal& a);
457  const mpreal operator*(const unsigned long int b, const mpreal& a);
458  const mpreal operator*(const unsigned int b, const mpreal& a);
459  const mpreal operator*(const long int b, const mpreal& a);
460  const mpreal operator*(const int b, const mpreal& a);
461 
463  // / Division
464  const mpreal operator/(const mpreal& a, const mpreal& b);
465 
466  // / Fast specialized division - implemented through fast /= operations
467  const mpreal operator/(const mpreal& a, const mpz_t b);
468  const mpreal operator/(const mpreal& a, const mpq_t b);
469  const mpreal operator/(const mpreal& a, const long double b);
470  const mpreal operator/(const mpreal& a, const double b);
471  const mpreal operator/(const mpreal& a, const unsigned long int b);
472  const mpreal operator/(const mpreal& a, const unsigned int b);
473  const mpreal operator/(const mpreal& a, const long int b);
474  const mpreal operator/(const mpreal& a, const int b);
475 
476  const mpreal operator/(const long double b, const mpreal& a);
477 
479  // Shifts operators - Multiplication/Division by a power of 2
480  const mpreal operator<<(const mpreal& v, const unsigned long int k);
481  const mpreal operator<<(const mpreal& v, const unsigned int k);
482  const mpreal operator<<(const mpreal& v, const long int k);
483  const mpreal operator<<(const mpreal& v, const int k);
484 
485  const mpreal operator>>(const mpreal& v, const unsigned long int k);
486  const mpreal operator>>(const mpreal& v, const unsigned int k);
487  const mpreal operator>>(const mpreal& v, const long int k);
488  const mpreal operator>>(const mpreal& v, const int k);
489 
491  // Boolean operators
492  bool operator<(const mpreal& a, const unsigned long int b);
493  bool operator<(const mpreal& a, const unsigned int b);
494  bool operator<(const mpreal& a, const long int b);
495  bool operator<(const mpreal& a, const int b);
496  bool operator<(const mpreal& a, const long double b);
497  bool operator<(const mpreal& a, const double b);
498 
499  bool operator<(const unsigned long int a, const mpreal& b);
500  bool operator<(const unsigned int a, const mpreal& b);
501  bool operator<(const long int a, const mpreal& b);
502  bool operator<(const int a, const mpreal& b);
503  bool operator<(const long double a, const mpreal& b);
504  bool operator<(const double a, const mpreal& b);
505 
506  bool operator>(const mpreal& a, const unsigned long int b);
507  bool operator>(const mpreal& a, const unsigned int b);
508  bool operator>(const mpreal& a, const long int b);
509  bool operator>(const mpreal& a, const int b);
510  bool operator>(const mpreal& a, const long double b);
511  bool operator>(const mpreal& a, const double b);
512 
513  bool operator>(const unsigned long int a, const mpreal& b);
514  bool operator>(const unsigned int a, const mpreal& b);
515  bool operator>(const long int a, const mpreal& b);
516  bool operator>(const int a, const mpreal& b);
517  bool operator>(const long double a, const mpreal& b);
518  bool operator>(const double a, const mpreal& b);
519 
520  bool operator >=(const mpreal& a, const unsigned long int b);
521  bool operator >=(const mpreal& a, const unsigned int b);
522  bool operator >=(const mpreal& a, const long int b);
523  bool operator >=(const mpreal& a, const int b);
524  bool operator >=(const mpreal& a, const long double b);
525  bool operator >=(const mpreal& a, const double b);
526 
527  bool operator >=(const unsigned long int a, const mpreal& b);
528  bool operator >=(const unsigned int a, const mpreal& b);
529  bool operator >=(const long int a, const mpreal& b);
530  bool operator >=(const int a, const mpreal& b);
531  bool operator >=(const long double a, const mpreal& b);
532  bool operator >=(const double a, const mpreal& b);
533 
534  bool operator <=(const mpreal& a, const unsigned long int b);
535  bool operator <=(const mpreal& a, const unsigned int b);
536  bool operator <=(const mpreal& a, const long int b);
537  bool operator <=(const mpreal& a, const int b);
538  bool operator <=(const mpreal& a, const long double b);
539  bool operator <=(const mpreal& a, const double b);
540 
541  bool operator <=(const unsigned long int a, const mpreal& b);
542  bool operator <=(const unsigned int a, const mpreal& b);
543  bool operator <=(const long int a, const mpreal& b);
544  bool operator <=(const int a, const mpreal& b);
545  bool operator <=(const long double a, const mpreal& b);
546  bool operator <=(const double a, const mpreal& b);
547 
548  bool operator ==(const mpreal& a, const unsigned long int b);
549  bool operator ==(const mpreal& a, const unsigned int b);
550  bool operator ==(const mpreal& a, const long int b);
551  bool operator ==(const mpreal& a, const int b);
552  bool operator ==(const mpreal& a, const long double b);
553  bool operator ==(const mpreal& a, const double b);
554 
555  bool operator ==(const unsigned long int a, const mpreal& b);
556  bool operator ==(const unsigned int a, const mpreal& b);
557  bool operator ==(const long int a, const mpreal& b);
558  bool operator ==(const int a, const mpreal& b);
559  bool operator ==(const long double a, const mpreal& b);
560  bool operator ==(const double a, const mpreal& b);
561 
562  bool operator !=(const mpreal& a, const unsigned long int b);
563  bool operator !=(const mpreal& a, const unsigned int b);
564  bool operator !=(const mpreal& a, const long int b);
565  bool operator !=(const mpreal& a, const int b);
566  bool operator !=(const mpreal& a, const long double b);
567  bool operator !=(const mpreal& a, const double b);
568 
569  bool operator !=(const unsigned long int a, const mpreal& b);
570  bool operator !=(const unsigned int a, const mpreal& b);
571  bool operator !=(const long int a, const mpreal& b);
572  bool operator !=(const int a, const mpreal& b);
573  bool operator !=(const long double a, const mpreal& b);
574  bool operator !=(const double a, const mpreal& b);
575 
577  // sqrt
578  const mpreal sqrt(const unsigned int v, mp_rnd_t rnd_mode = mpreal::default_rnd);
579  const mpreal sqrt(const long int v, mp_rnd_t rnd_mode = mpreal::default_rnd);
580  const mpreal sqrt(const int v, mp_rnd_t rnd_mode = mpreal::default_rnd);
581  const mpreal sqrt(const long double v, mp_rnd_t rnd_mode = mpreal::default_rnd);
582  const mpreal sqrt(const double v, mp_rnd_t rnd_mode = mpreal::default_rnd);
583 
585  // pow
586  const mpreal pow(const mpreal& a, const unsigned int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
587  const mpreal pow(const mpreal& a, const int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
588  const mpreal pow(const mpreal& a, const long double b, mp_rnd_t rnd_mode = mpreal::default_rnd);
589  const mpreal pow(const mpreal& a, const double b, mp_rnd_t rnd_mode = mpreal::default_rnd);
590 
591  const mpreal pow(const unsigned int a, const mpreal& b, mp_rnd_t rnd_mode = mpreal::default_rnd);
592  const mpreal pow(const long int a, const mpreal& b, mp_rnd_t rnd_mode = mpreal::default_rnd);
593  const mpreal pow(const int a, const mpreal& b, mp_rnd_t rnd_mode = mpreal::default_rnd);
594  const mpreal pow(const long double a, const mpreal& b, mp_rnd_t rnd_mode = mpreal::default_rnd);
595  const mpreal pow(const double a, const mpreal& b, mp_rnd_t rnd_mode = mpreal::default_rnd);
596 
597  const mpreal pow(const unsigned long int a, const unsigned int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
598  const mpreal pow(const unsigned long int a, const long int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
599  const mpreal pow(const unsigned long int a, const int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
600  const mpreal pow(const unsigned long int a, const long double b, mp_rnd_t rnd_mode = mpreal::default_rnd);
601  const mpreal pow(const unsigned long int a, const double b, mp_rnd_t rnd_mode = mpreal::default_rnd);
602 
603  const mpreal pow(const unsigned int a, const unsigned long int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
604  const mpreal pow(const unsigned int a, const unsigned int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
605  const mpreal pow(const unsigned int a, const long int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
606  const mpreal pow(const unsigned int a, const int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
607  const mpreal pow(const unsigned int a, const long double b, mp_rnd_t rnd_mode = mpreal::default_rnd);
608  const mpreal pow(const unsigned int a, const double b, mp_rnd_t rnd_mode = mpreal::default_rnd);
609 
610  const mpreal pow(const long int a, const unsigned long int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
611  const mpreal pow(const long int a, const unsigned int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
612  const mpreal pow(const long int a, const long int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
613  const mpreal pow(const long int a, const int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
614  const mpreal pow(const long int a, const long double b, mp_rnd_t rnd_mode = mpreal::default_rnd);
615  const mpreal pow(const long int a, const double b, mp_rnd_t rnd_mode = mpreal::default_rnd);
616 
617  const mpreal pow(const int a, const unsigned long int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
618  const mpreal pow(const int a, const unsigned int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
619  const mpreal pow(const int a, const long int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
620  const mpreal pow(const int a, const int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
621  const mpreal pow(const int a, const long double b, mp_rnd_t rnd_mode = mpreal::default_rnd);
622  const mpreal pow(const int a, const double b, mp_rnd_t rnd_mode = mpreal::default_rnd);
623 
624  const mpreal pow(const long double a, const long double b, mp_rnd_t rnd_mode = mpreal::default_rnd);
625  const mpreal pow(const long double a, const unsigned long int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
626  const mpreal pow(const long double a, const unsigned int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
627  const mpreal pow(const long double a, const long int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
628  const mpreal pow(const long double a, const int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
629 
630  const mpreal pow(const double a, const double b, mp_rnd_t rnd_mode = mpreal::default_rnd);
631  const mpreal pow(const double a, const unsigned long int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
632  const mpreal pow(const double a, const unsigned int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
633  const mpreal pow(const double a, const long int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
634  const mpreal pow(const double a, const int b, mp_rnd_t rnd_mode = mpreal::default_rnd);
635 
637  // Implementation of inline functions
639 
641  // Operators - Assignment
642 
643  inline mpreal& mpreal::operator=(const mpreal& v) {
644  if (this != &v) mpfr_set(mp, v.mp, default_rnd);
645  return *this;
646  }
647 
648  inline mpreal& mpreal::operator=(const mpf_t v) {
649  mpfr_set_f(mp, v, default_rnd);
650  return *this;
651  }
652 
653  inline mpreal& mpreal::operator=(const mpz_t v) {
654  mpfr_set_z(mp, v, default_rnd);
655  return *this;
656  }
657 
658  inline mpreal& mpreal::operator=(const mpq_t v) {
659  mpfr_set_q(mp, v, default_rnd);
660  return *this;
661  }
662 
663  inline mpreal& mpreal::operator=(const long double v) {
664  mpfr_set_ld(mp, v, default_rnd);
665  return *this;
666  }
667 
668  inline mpreal& mpreal::operator=(const double v) {
669  if (double_bits == -1 || fits_in_bits(v, double_bits)) {
670  mpfr_set_d(mp, v, default_rnd);
671  } else
672  throw conversion_overflow();
673 
674  return *this;
675  }
676 
677  inline mpreal& mpreal::operator=(const unsigned long int v) {
678  mpfr_set_ui(mp, v, default_rnd);
679  return *this;
680  }
681 
682  inline mpreal& mpreal::operator=(const unsigned int v) {
683  mpfr_set_ui(mp, v, default_rnd);
684  return *this;
685  }
686 
687  inline mpreal& mpreal::operator=(const long int v) {
688  mpfr_set_si(mp, v, default_rnd);
689  return *this;
690  }
691 
692  inline mpreal& mpreal::operator=(const int v) {
693  mpfr_set_si(mp, v, default_rnd);
694  return *this;
695  }
696 
698  // + Addition
699 
700  inline mpreal& mpreal::operator+=(const mpreal& v) {
701  mpfr_add(mp, mp, v.mp, default_rnd);
702  return *this;
703  }
704 
705  inline mpreal& mpreal::operator+=(const mpf_t u) {
706  *this += mpreal(u);
707  return *this;
708  }
709 
710  inline mpreal& mpreal::operator+=(const mpz_t u) {
711  mpfr_add_z(mp, mp, u, default_rnd);
712  return *this;
713  }
714 
715  inline mpreal& mpreal::operator+=(const mpq_t u) {
716  mpfr_add_q(mp, mp, u, default_rnd);
717  return *this;
718  }
719 
720  inline mpreal& mpreal::operator+=(const long double u) {
721  return *this += mpreal(u);
722  }
723 
724  inline mpreal& mpreal::operator+=(const double u) {
725 #if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
726  mpfr_add_d(mp, mp, u, default_rnd);
727  return *this;
728 #else
729  return *this += mpreal(u);
730 #endif
731  }
732 
733  inline mpreal& mpreal::operator+=(const unsigned long int u) {
734  mpfr_add_ui(mp, mp, u, default_rnd);
735  return *this;
736  }
737 
738  inline mpreal& mpreal::operator+=(const unsigned int u) {
739  mpfr_add_ui(mp, mp, u, default_rnd);
740  return *this;
741  }
742 
743  inline mpreal& mpreal::operator+=(const long int u) {
744  mpfr_add_si(mp, mp, u, default_rnd);
745  return *this;
746  }
747 
748  inline mpreal& mpreal::operator+=(const int u) {
749  mpfr_add_si(mp, mp, u, default_rnd);
750  return *this;
751  }
752 
753  inline const mpreal mpreal::operator+()const {
754  return mpreal(*this);
755  }
756 
757  inline const mpreal operator+(const mpreal& a, const mpreal& b) {
758  // prec(a+b) = max(prec(a),prec(b))
759  if (a.get_prec() > b.get_prec()) return mpreal(a) += b;
760  else return mpreal(b) += a;
761  }
762 
763  inline const std::string operator+(const mpreal& a, const std::string b) {
764  return (std::string)a + b;
765  }
766 
767  inline const std::string operator+(const std::string a, const mpreal& b) {
768  return a + (std::string)b;
769  }
770 
771  inline const mpreal operator+(const mpreal& a, const mpz_t b) {
772  return mpreal(a) += b;
773  }
774 
775  inline const mpreal operator+(const mpreal& a, const char* b) {
776  return a + mpreal(b);
777  }
778 
779  inline const mpreal operator+(const char* a, const mpreal& b) {
780  return mpreal(a) + b;
781 
782  }
783 
784  inline const mpreal operator+(const mpreal& a, const mpq_t b) {
785  return mpreal(a) += b;
786  }
787 
788  inline const mpreal operator+(const mpreal& a, const long double b) {
789  return mpreal(a) += b;
790  }
791 
792  inline const mpreal operator+(const mpreal& a, const double b) {
793  return mpreal(a) += b;
794  }
795 
796  inline const mpreal operator+(const mpreal& a, const unsigned long int b) {
797  return mpreal(a) += b;
798  }
799 
800  inline const mpreal operator+(const mpreal& a, const unsigned int b) {
801  return mpreal(a) += b;
802  }
803 
804  inline const mpreal operator+(const mpreal& a, const long int b) {
805  return mpreal(a) += b;
806  }
807 
808  inline const mpreal operator+(const mpreal& a, const int b) {
809  return mpreal(a) += b;
810  }
811 
812  inline const mpreal operator+(const mpz_t b, const mpreal& a) {
813  return mpreal(a) += b;
814  }
815 
816  inline const mpreal operator+(const mpq_t b, const mpreal& a) {
817  return mpreal(a) += b;
818  }
819 
820  inline const mpreal operator+(const long double b, const mpreal& a) {
821  return mpreal(a) += b;
822  }
823 
824  inline const mpreal operator+(const double b, const mpreal& a) {
825  return mpreal(a) += b;
826  }
827 
828  inline const mpreal operator+(const unsigned long int b, const mpreal& a) {
829  return mpreal(a) += b;
830  }
831 
832  inline const mpreal operator+(const unsigned int b, const mpreal& a) {
833  return mpreal(a) += b;
834  }
835 
836  inline const mpreal operator+(const long int b, const mpreal& a) {
837  return mpreal(a) += b;
838  }
839 
840  inline const mpreal operator+(const int b, const mpreal& a) {
841  return mpreal(a) += b;
842  }
843 
844  inline mpreal& mpreal::operator++() {
845  *this += 1;
846  return *this;
847  }
848 
849  inline const mpreal mpreal::operator++(int) {
850  mpreal x(*this);
851  *this += 1;
852  return x;
853  }
854 
855  inline mpreal& mpreal::operator--() {
856  *this -= 1;
857  return *this;
858  }
859 
860  inline const mpreal mpreal::operator--(int) {
861  mpreal x(*this);
862  *this -= 1;
863  return x;
864  }
865 
867  // - Subtraction
868 
869  inline mpreal& mpreal::operator-=(const mpreal& v) {
870  mpfr_sub(mp, mp, v.mp, default_rnd);
871  return *this;
872  }
873 
874  inline mpreal& mpreal::operator-=(const mpz_t v) {
875  mpfr_sub_z(mp, mp, v, default_rnd);
876  return *this;
877  }
878 
879  inline mpreal& mpreal::operator-=(const mpq_t v) {
880  mpfr_sub_q(mp, mp, v, default_rnd);
881  return *this;
882  }
883 
884  inline mpreal& mpreal::operator-=(const long double v) {
885  return *this -= mpreal(v);
886  }
887 
888  inline mpreal& mpreal::operator-=(const double v) {
889 #if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
890  mpfr_sub_d(mp, mp, v, default_rnd);
891  return *this;
892 #else
893  return *this -= mpreal(v);
894 #endif
895  }
896 
897  inline mpreal& mpreal::operator-=(const unsigned long int v) {
898  mpfr_sub_ui(mp, mp, v, default_rnd);
899  return *this;
900  }
901 
902  inline mpreal& mpreal::operator-=(const unsigned int v) {
903  mpfr_sub_ui(mp, mp, v, default_rnd);
904  return *this;
905  }
906 
907  inline mpreal& mpreal::operator-=(const long int v) {
908  mpfr_sub_si(mp, mp, v, default_rnd);
909  return *this;
910  }
911 
912  inline mpreal& mpreal::operator-=(const int v) {
913  mpfr_sub_si(mp, mp, v, default_rnd);
914  return *this;
915  }
916 
917  inline const mpreal mpreal::operator-()const {
918  mpreal u(*this);
919  mpfr_neg(u.mp, u.mp, default_rnd);
920  return u;
921  }
922 
923  inline const mpreal operator-(const mpreal& a, const mpreal& b) {
924  // prec(a-b) = max(prec(a),prec(b))
925  if (a.get_prec() > b.get_prec()) return mpreal(a) -= b;
926  else return -(mpreal(b) -= a);
927  }
928 
929  inline const mpreal operator-(const mpreal& a, const mpz_t b) {
930  return mpreal(a) -= b;
931  }
932 
933  inline const mpreal operator-(const mpreal& a, const mpq_t b) {
934  return mpreal(a) -= b;
935  }
936 
937  inline const mpreal operator-(const mpreal& a, const long double b) {
938  return mpreal(a) -= b;
939  }
940 
941  inline const mpreal operator-(const mpreal& a, const double b) {
942  return mpreal(a) -= b;
943  }
944 
945  inline const mpreal operator-(const mpreal& a, const unsigned long int b) {
946  return mpreal(a) -= b;
947  }
948 
949  inline const mpreal operator-(const mpreal& a, const unsigned int b) {
950  return mpreal(a) -= b;
951  }
952 
953  inline const mpreal operator-(const mpreal& a, const long int b) {
954  return mpreal(a) -= b;
955  }
956 
957  inline const mpreal operator-(const mpreal& a, const int b) {
958  return mpreal(a) -= b;
959  }
960 
961  inline const mpreal operator-(const mpz_t b, const mpreal& a) {
962  return -(mpreal(a) -= b);
963  }
964 
965  inline const mpreal operator-(const mpq_t b, const mpreal& a) {
966  return -(mpreal(a) -= b);
967  }
968 
969  inline const mpreal operator-(const long double b, const mpreal& a) {
970  return -(mpreal(a) -= b);
971  }
972 
973  inline const mpreal operator-(const double b, const mpreal& a) {
974 #if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
975  mpreal x(a);
976  mpfr_d_sub(x.mp, b, a.mp, mpreal::default_rnd);
977  return x;
978 #else
979  return -(mpreal(a) -= b);
980 #endif
981  }
982 
983  inline const mpreal operator-(const unsigned long int b, const mpreal& a) {
984  mpreal x(a);
985  mpfr_ui_sub(x.mp, b, a.mp, mpreal::default_rnd);
986  return x;
987  }
988 
989  inline const mpreal operator-(const unsigned int b, const mpreal& a) {
990  mpreal x(a);
991  mpfr_ui_sub(x.mp, b, a.mp, mpreal::default_rnd);
992  return x;
993  }
994 
995  inline const mpreal operator-(const long int b, const mpreal& a) {
996  mpreal x(a);
997  mpfr_si_sub(x.mp, b, a.mp, mpreal::default_rnd);
998  return x;
999  }
1000 
1001  inline const mpreal operator-(const int b, const mpreal& a) {
1002  mpreal x(a);
1003  mpfr_si_sub(x.mp, b, a.mp, mpreal::default_rnd);
1004  return x;
1005  }
1006 
1007  inline const mpreal operator-(const mpreal& a, const char* b) {
1008  return a - mpreal(b);
1009  }
1010 
1011  inline const mpreal operator-(const char* a, const mpreal& b) {
1012  return mpreal(a) - b;
1013  }
1014 
1016  // * Multiplication
1017 
1018  inline mpreal& mpreal::operator*=(const mpreal& v) {
1019  mpfr_mul(mp, mp, v.mp, default_rnd);
1020  return *this;
1021  }
1022 
1023  inline mpreal& mpreal::operator*=(const mpz_t v) {
1024  mpfr_mul_z(mp, mp, v, default_rnd);
1025  return *this;
1026  }
1027 
1028  inline mpreal& mpreal::operator*=(const mpq_t v) {
1029  mpfr_mul_q(mp, mp, v, default_rnd);
1030  return *this;
1031  }
1032 
1033  inline mpreal& mpreal::operator*=(const long double v) {
1034  return *this *= mpreal(v);
1035  }
1036 
1037  inline mpreal& mpreal::operator*=(const double v) {
1038 #if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
1039  mpfr_mul_d(mp, mp, v, default_rnd);
1040  return *this;
1041 #else
1042  return *this *= mpreal(v);
1043 #endif
1044  }
1045 
1046  inline mpreal& mpreal::operator*=(const unsigned long int v) {
1047  mpfr_mul_ui(mp, mp, v, default_rnd);
1048  return *this;
1049  }
1050 
1051  inline mpreal& mpreal::operator*=(const unsigned int v) {
1052  mpfr_mul_ui(mp, mp, v, default_rnd);
1053  return *this;
1054  }
1055 
1056  inline mpreal& mpreal::operator*=(const long int v) {
1057  mpfr_mul_si(mp, mp, v, default_rnd);
1058  return *this;
1059  }
1060 
1061  inline mpreal& mpreal::operator*=(const int v) {
1062  mpfr_mul_si(mp, mp, v, default_rnd);
1063  return *this;
1064  }
1065 
1066  inline const mpreal operator*(const mpreal& a, const mpreal& b) {
1067  // prec(a*b) = max(prec(a),prec(b))
1068  if (a.get_prec() > b.get_prec()) return mpreal(a) *= b;
1069  else return mpreal(b) *= a;
1070  }
1071 
1072  inline const mpreal operator*(const mpreal& a, const mpz_t b) {
1073  return mpreal(a) *= b;
1074  }
1075 
1076  inline const mpreal operator*(const mpreal& a, const mpq_t b) {
1077  return mpreal(a) *= b;
1078  }
1079 
1080  inline const mpreal operator*(const mpreal& a, const long double b) {
1081  return mpreal(a) *= b;
1082  }
1083 
1084  inline const mpreal operator*(const mpreal& a, const double b) {
1085  return mpreal(a) *= b;
1086  }
1087 
1088  inline const mpreal operator*(const mpreal& a, const unsigned long int b) {
1089  return mpreal(a) *= b;
1090  }
1091 
1092  inline const mpreal operator*(const mpreal& a, const unsigned int b) {
1093  return mpreal(a) *= b;
1094  }
1095 
1096  inline const mpreal operator*(const mpreal& a, const long int b) {
1097  return mpreal(a) *= b;
1098  }
1099 
1100  inline const mpreal operator*(const mpreal& a, const int b) {
1101  return mpreal(a) *= b;
1102  }
1103 
1104  inline const mpreal operator*(const mpz_t b, const mpreal& a) {
1105  return mpreal(a) *= b;
1106  }
1107 
1108  inline const mpreal operator*(const mpq_t b, const mpreal& a) {
1109  return mpreal(a) *= b;
1110  }
1111 
1112  inline const mpreal operator*(const long double b, const mpreal& a) {
1113  return mpreal(a) *= b;
1114  }
1115 
1116  inline const mpreal operator*(const double b, const mpreal& a) {
1117  return mpreal(a) *= b;
1118  }
1119 
1120  inline const mpreal operator*(const unsigned long int b, const mpreal& a) {
1121  return mpreal(a) *= b;
1122  }
1123 
1124  inline const mpreal operator*(const unsigned int b, const mpreal& a) {
1125  return mpreal(a) *= b;
1126  }
1127 
1128  inline const mpreal operator*(const long int b, const mpreal& a) {
1129  return mpreal(a) *= b;
1130  }
1131 
1132  inline const mpreal operator*(const int b, const mpreal& a) {
1133  return mpreal(a) *= b;
1134  }
1135 
1137  // / Division
1138 
1139  inline mpreal& mpreal::operator/=(const mpreal& v) {
1140  mpfr_div(mp, mp, v.mp, default_rnd);
1141  return *this;
1142  }
1143 
1144  inline mpreal& mpreal::operator/=(const mpz_t v) {
1145  mpfr_div_z(mp, mp, v, default_rnd);
1146  return *this;
1147  }
1148 
1149  inline mpreal& mpreal::operator/=(const mpq_t v) {
1150  mpfr_div_q(mp, mp, v, default_rnd);
1151  return *this;
1152  }
1153 
1154  inline mpreal& mpreal::operator/=(const long double v) {
1155  return *this /= mpreal(v);
1156  }
1157 
1158  inline mpreal& mpreal::operator/=(const double v) {
1159 #if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
1160  mpfr_div_d(mp, mp, v, default_rnd);
1161  return *this;
1162 #else
1163  return *this /= mpreal(v);
1164 #endif
1165  }
1166 
1167  inline mpreal& mpreal::operator/=(const unsigned long int v) {
1168  mpfr_div_ui(mp, mp, v, default_rnd);
1169  return *this;
1170  }
1171 
1172  inline mpreal& mpreal::operator/=(const unsigned int v) {
1173  mpfr_div_ui(mp, mp, v, default_rnd);
1174  return *this;
1175  }
1176 
1177  inline mpreal& mpreal::operator/=(const long int v) {
1178  mpfr_div_si(mp, mp, v, default_rnd);
1179  return *this;
1180  }
1181 
1182  inline mpreal& mpreal::operator/=(const int v) {
1183  mpfr_div_si(mp, mp, v, default_rnd);
1184  return *this;
1185  }
1186 
1187  inline const mpreal operator/(const mpreal& a, const mpreal& b) {
1188  mpreal x(a);
1189  mp_prec_t pb;
1190  mp_prec_t pa;
1191 
1192  // prec(a/b) = max(prec(a),prec(b))
1193  pa = a.get_prec();
1194  pb = b.get_prec();
1195  if (pb > pa) x.set_prec(pb);
1196 
1197  return x /= b;
1198  }
1199 
1200  inline const mpreal operator/(const mpreal& a, const mpz_t b) {
1201  return mpreal(a) /= b;
1202  }
1203 
1204  inline const mpreal operator/(const mpreal& a, const mpq_t b) {
1205  return mpreal(a) /= b;
1206  }
1207 
1208  inline const mpreal operator/(const mpreal& a, const long double b) {
1209  return mpreal(a) /= b;
1210  }
1211 
1212  inline const mpreal operator/(const mpreal& a, const double b) {
1213  return mpreal(a) /= b;
1214  }
1215 
1216  inline const mpreal operator/(const mpreal& a, const unsigned long int b) {
1217  return mpreal(a) /= b;
1218  }
1219 
1220  inline const mpreal operator/(const mpreal& a, const unsigned int b) {
1221  return mpreal(a) /= b;
1222  }
1223 
1224  inline const mpreal operator/(const mpreal& a, const long int b) {
1225  return mpreal(a) /= b;
1226  }
1227 
1228  inline const mpreal operator/(const mpreal& a, const int b) {
1229  return mpreal(a) /= b;
1230  }
1231 
1232  inline const mpreal operator/(const unsigned long int b, const mpreal& a) {
1233  mpreal x(a);
1234  mpfr_ui_div(x.mp, b, a.mp, mpreal::default_rnd);
1235  return x;
1236  }
1237 
1238  inline const mpreal operator/(const unsigned int b, const mpreal& a) {
1239  mpreal x(a);
1240  mpfr_ui_div(x.mp, b, a.mp, mpreal::default_rnd);
1241  return x;
1242  }
1243 
1244  inline const mpreal operator/(const long int b, const mpreal& a) {
1245  mpreal x(a);
1246  mpfr_si_div(x.mp, b, a.mp, mpreal::default_rnd);
1247  return x;
1248  }
1249 
1250  inline const mpreal operator/(const int b, const mpreal& a) {
1251  mpreal x(a);
1252  mpfr_si_div(x.mp, b, a.mp, mpreal::default_rnd);
1253  return x;
1254  }
1255 
1256  inline const mpreal operator/(const long double b, const mpreal& a) {
1257  mpreal x(b);
1258  return x / a;
1259  }
1260 
1261  inline const mpreal operator/(const double b, const mpreal& a) {
1262 #if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
1263  mpreal x(a);
1264  mpfr_d_div(x.mp, b, a.mp, mpreal::default_rnd);
1265  return x;
1266 #else
1267  mpreal x(b);
1268  return x / a;
1269 #endif
1270  }
1271 
1273  // Shifts operators - Multiplication/Division by power of 2
1274 
1275  inline mpreal& mpreal::operator<<=(const unsigned long int u) {
1276  mpfr_mul_2ui(mp, mp, u, default_rnd);
1277  return *this;
1278  }
1279 
1280  inline mpreal& mpreal::operator<<=(const unsigned int u) {
1281  mpfr_mul_2ui(mp, mp, static_cast<unsigned long int> (u), default_rnd);
1282  return *this;
1283  }
1284 
1285  inline mpreal& mpreal::operator<<=(const long int u) {
1286  mpfr_mul_2si(mp, mp, u, default_rnd);
1287  return *this;
1288  }
1289 
1290  inline mpreal& mpreal::operator<<=(const int u) {
1291  mpfr_mul_2si(mp, mp, static_cast<long int> (u), default_rnd);
1292  return *this;
1293  }
1294 
1295  inline mpreal& mpreal::operator>>=(const unsigned long int u) {
1296  mpfr_div_2ui(mp, mp, u, default_rnd);
1297  return *this;
1298  }
1299 
1300  inline mpreal& mpreal::operator>>=(const unsigned int u) {
1301  mpfr_div_2ui(mp, mp, static_cast<unsigned long int> (u), default_rnd);
1302  return *this;
1303  }
1304 
1305  inline mpreal& mpreal::operator>>=(const long int u) {
1306  mpfr_div_2si(mp, mp, u, default_rnd);
1307  return *this;
1308  }
1309 
1310  inline mpreal& mpreal::operator>>=(const int u) {
1311  mpfr_div_2si(mp, mp, static_cast<long int> (u), default_rnd);
1312  return *this;
1313  }
1314 
1315  inline const mpreal operator<<(const mpreal& v, const unsigned long int k) {
1316  return mul_2ui(v, k);
1317  }
1318 
1319  inline const mpreal operator<<(const mpreal& v, const unsigned int k) {
1320  return mul_2ui(v, static_cast<unsigned long int> (k));
1321  }
1322 
1323  inline const mpreal operator<<(const mpreal& v, const long int k) {
1324  return mul_2si(v, k);
1325  }
1326 
1327  inline const mpreal operator<<(const mpreal& v, const int k) {
1328  return mul_2si(v, static_cast<long int> (k));
1329  }
1330 
1331  inline const mpreal operator>>(const mpreal& v, const unsigned long int k) {
1332  return div_2ui(v, k);
1333  }
1334 
1335  inline const mpreal operator>>(const mpreal& v, const long int k) {
1336  return div_2si(v, k);
1337  }
1338 
1339  inline const mpreal operator>>(const mpreal& v, const unsigned int k) {
1340  return div_2ui(v, static_cast<unsigned long int> (k));
1341  }
1342 
1343  inline const mpreal operator>>(const mpreal& v, const int k) {
1344  return div_2si(v, static_cast<long int> (k));
1345  }
1346 
1347  // mul_2ui
1348 
1349  inline const mpreal mul_2ui(const mpreal& v, unsigned long int k, mp_rnd_t rnd_mode) {
1350  mpreal x(v);
1351  mpfr_mul_2ui(x.mp, v.mp, k, rnd_mode);
1352  return x;
1353  }
1354 
1355  // mul_2si
1356 
1357  inline const mpreal mul_2si(const mpreal& v, long int k, mp_rnd_t rnd_mode) {
1358  mpreal x(v);
1359  mpfr_mul_2si(x.mp, v.mp, k, rnd_mode);
1360  return x;
1361  }
1362 
1363  inline const mpreal div_2ui(const mpreal& v, unsigned long int k, mp_rnd_t rnd_mode) {
1364  mpreal x(v);
1365  mpfr_div_2ui(x.mp, v.mp, k, rnd_mode);
1366  return x;
1367  }
1368 
1369  inline const mpreal div_2si(const mpreal& v, long int k, mp_rnd_t rnd_mode) {
1370  mpreal x(v);
1371  mpfr_div_2si(x.mp, v.mp, k, rnd_mode);
1372  return x;
1373  }
1374 
1376  //Boolean operators
1377 
1378  inline bool operator>(const mpreal& a, const mpreal& b) {
1379  return (mpfr_greater_p(a.mp, b.mp) != 0);
1380  }
1381 
1382  inline bool operator>(const mpreal& a, const unsigned long int b) {
1383  return a > mpreal(b);
1384  }
1385 
1386  inline bool operator>(const mpreal& a, const unsigned int b) {
1387  return a > mpreal(b);
1388  }
1389 
1390  inline bool operator>(const mpreal& a, const long int b) {
1391  return a > mpreal(b);
1392  }
1393 
1394  inline bool operator>(const mpreal& a, const int b) {
1395  return a > mpreal(b);
1396  }
1397 
1398  inline bool operator>(const mpreal& a, const long double b) {
1399  return a > mpreal(b);
1400  }
1401 
1402  inline bool operator>(const mpreal& a, const double b) {
1403  return a > mpreal(b);
1404  }
1405 
1406  inline bool operator>(const unsigned long int a, const mpreal& b) {
1407  return mpreal(a) > b;
1408  }
1409 
1410  inline bool operator>(const unsigned int a, const mpreal& b) {
1411  return mpreal(a) > b;
1412  }
1413 
1414  inline bool operator>(const long int a, const mpreal& b) {
1415  return mpreal(a) > b;
1416  }
1417 
1418  inline bool operator>(const int a, const mpreal& b) {
1419  return mpreal(a) > b;
1420  }
1421 
1422  inline bool operator>(const long double a, const mpreal& b) {
1423  return mpreal(a) > b;
1424  }
1425 
1426  inline bool operator>(const double a, const mpreal& b) {
1427  return mpreal(a) > b;
1428  }
1429 
1430  inline bool operator >=(const mpreal& a, const mpreal& b) {
1431  return (mpfr_greaterequal_p(a.mp, b.mp) != 0);
1432  }
1433 
1434  inline bool operator >=(const mpreal& a, const unsigned long int b) {
1435  return a >= mpreal(b);
1436  }
1437 
1438  inline bool operator >=(const mpreal& a, const unsigned int b) {
1439  return a >= mpreal(b);
1440  }
1441 
1442  inline bool operator >=(const mpreal& a, const long int b) {
1443  return a >= mpreal(b);
1444  }
1445 
1446  inline bool operator >=(const mpreal& a, const int b) {
1447  return a >= mpreal(b);
1448  }
1449 
1450  inline bool operator >=(const mpreal& a, const long double b) {
1451  return a >= mpreal(b);
1452  }
1453 
1454  inline bool operator >=(const mpreal& a, const double b) {
1455  return a >= mpreal(b);
1456  }
1457 
1458  inline bool operator >=(const unsigned long int a, const mpreal& b) {
1459  return mpreal(a) >= b;
1460  }
1461 
1462  inline bool operator >=(const unsigned int a, const mpreal& b) {
1463  return mpreal(a) >= b;
1464  }
1465 
1466  inline bool operator >=(const long int a, const mpreal& b) {
1467  return mpreal(a) >= b;
1468  }
1469 
1470  inline bool operator >=(const int a, const mpreal& b) {
1471  return mpreal(a) >= b;
1472  }
1473 
1474  inline bool operator >=(const long double a, const mpreal& b) {
1475  return mpreal(a) >= b;
1476  }
1477 
1478  inline bool operator >=(const double a, const mpreal& b) {
1479  return mpreal(a) >= b;
1480  }
1481 
1482  inline bool operator<(const mpreal& a, const mpreal& b) {
1483  return (mpfr_less_p(a.mp, b.mp) != 0);
1484  }
1485 
1486  inline bool operator<(const mpreal& a, const unsigned long int b) {
1487  return a < mpreal(b);
1488  }
1489 
1490  inline bool operator<(const mpreal& a, const unsigned int b) {
1491  return a < mpreal(b);
1492  }
1493 
1494  inline bool operator<(const mpreal& a, const long int b) {
1495  return a < mpreal(b);
1496  }
1497 
1498  inline bool operator<(const mpreal& a, const int b) {
1499  return a < mpreal(b);
1500  }
1501 
1502  inline bool operator<(const mpreal& a, const long double b) {
1503  return a < mpreal(b);
1504  }
1505 
1506  inline bool operator<(const mpreal& a, const double b) {
1507  return a < mpreal(b);
1508  }
1509 
1510  inline bool operator<(const unsigned long int a, const mpreal& b) {
1511  return mpreal(a) < b;
1512  }
1513 
1514  inline bool operator<(const unsigned int a, const mpreal& b) {
1515  return mpreal(a) < b;
1516  }
1517 
1518  inline bool operator<(const long int a, const mpreal& b) {
1519  return mpreal(a) < b;
1520  }
1521 
1522  inline bool operator<(const int a, const mpreal& b) {
1523  return mpreal(a) < b;
1524  }
1525 
1526  inline bool operator<(const long double a, const mpreal& b) {
1527  return mpreal(a) < b;
1528  }
1529 
1530  inline bool operator<(const double a, const mpreal& b) {
1531  return mpreal(a) < b;
1532  }
1533 
1534  inline bool operator <=(const mpreal& a, const mpreal& b) {
1535  return (mpfr_lessequal_p(a.mp, b.mp) != 0);
1536  }
1537 
1538  inline bool operator <=(const mpreal& a, const unsigned long int b) {
1539  return a <= mpreal(b);
1540  }
1541 
1542  inline bool operator <=(const mpreal& a, const unsigned int b) {
1543  return a <= mpreal(b);
1544  }
1545 
1546  inline bool operator <=(const mpreal& a, const long int b) {
1547  return a <= mpreal(b);
1548  }
1549 
1550  inline bool operator <=(const mpreal& a, const int b) {
1551  return a <= mpreal(b);
1552  }
1553 
1554  inline bool operator <=(const mpreal& a, const long double b) {
1555  return a <= mpreal(b);
1556  }
1557 
1558  inline bool operator <=(const mpreal& a, const double b) {
1559  return a <= mpreal(b);
1560  }
1561 
1562  inline bool operator <=(const unsigned long int a, const mpreal& b) {
1563  return mpreal(a) <= b;
1564  }
1565 
1566  inline bool operator <=(const unsigned int a, const mpreal& b) {
1567  return mpreal(a) <= b;
1568  }
1569 
1570  inline bool operator <=(const long int a, const mpreal& b) {
1571  return mpreal(a) <= b;
1572  }
1573 
1574  inline bool operator <=(const int a, const mpreal& b) {
1575  return mpreal(a) <= b;
1576  }
1577 
1578  inline bool operator <=(const long double a, const mpreal& b) {
1579  return mpreal(a) <= b;
1580  }
1581 
1582  inline bool operator <=(const double a, const mpreal& b) {
1583  return mpreal(a) <= b;
1584  }
1585 
1586  inline bool operator ==(const mpreal& a, const mpreal& b) {
1587  return (mpfr_equal_p(a.mp, b.mp) != 0);
1588  }
1589 
1590  inline bool operator ==(const mpreal& a, const unsigned long int b) {
1591  return a == mpreal(b);
1592  }
1593 
1594  inline bool operator ==(const mpreal& a, const unsigned int b) {
1595  return a == mpreal(b);
1596  }
1597 
1598  inline bool operator ==(const mpreal& a, const long int b) {
1599  return a == mpreal(b);
1600  }
1601 
1602  inline bool operator ==(const mpreal& a, const int b) {
1603  return a == mpreal(b);
1604  }
1605 
1606  inline bool operator ==(const mpreal& a, const long double b) {
1607  return a == mpreal(b);
1608  }
1609 
1610  inline bool operator ==(const mpreal& a, const double b) {
1611  return a == mpreal(b);
1612  }
1613 
1614  inline bool operator ==(const unsigned long int a, const mpreal& b) {
1615  return mpreal(a) == b;
1616  }
1617 
1618  inline bool operator ==(const unsigned int a, const mpreal& b) {
1619  return mpreal(a) == b;
1620  }
1621 
1622  inline bool operator ==(const long int a, const mpreal& b) {
1623  return mpreal(a) == b;
1624  }
1625 
1626  inline bool operator ==(const int a, const mpreal& b) {
1627  return mpreal(a) == b;
1628  }
1629 
1630  inline bool operator ==(const long double a, const mpreal& b) {
1631  return mpreal(a) == b;
1632  }
1633 
1634  inline bool operator ==(const double a, const mpreal& b) {
1635  return mpreal(a) == b;
1636  }
1637 
1638  inline bool operator !=(const mpreal& a, const mpreal& b) {
1639  return (mpfr_lessgreater_p(a.mp, b.mp) != 0);
1640  }
1641 
1642  inline bool operator !=(const mpreal& a, const unsigned long int b) {
1643  return a != mpreal(b);
1644  }
1645 
1646  inline bool operator !=(const mpreal& a, const unsigned int b) {
1647  return a != mpreal(b);
1648  }
1649 
1650  inline bool operator !=(const mpreal& a, const long int b) {
1651  return a != mpreal(b);
1652  }
1653 
1654  inline bool operator !=(const mpreal& a, const int b) {
1655  return a != mpreal(b);
1656  }
1657 
1658  inline bool operator !=(const mpreal& a, const long double b) {
1659  return a != mpreal(b);
1660  }
1661 
1662  inline bool operator !=(const mpreal& a, const double b) {
1663  return a != mpreal(b);
1664  }
1665 
1666  inline bool operator !=(const unsigned long int a, const mpreal& b) {
1667  return mpreal(a) != b;
1668  }
1669 
1670  inline bool operator !=(const unsigned int a, const mpreal& b) {
1671  return mpreal(a) != b;
1672  }
1673 
1674  inline bool operator !=(const long int a, const mpreal& b) {
1675  return mpreal(a) != b;
1676  }
1677 
1678  inline bool operator !=(const int a, const mpreal& b) {
1679  return mpreal(a) != b;
1680  }
1681 
1682  inline bool operator !=(const long double a, const mpreal& b) {
1683  return mpreal(a) != b;
1684  }
1685 
1686  inline bool operator !=(const double a, const mpreal& b) {
1687  return mpreal(a) != b;
1688  }
1689 
1690  inline bool _isnan(const mpreal& v) {
1691  return (mpfr_nan_p(v.mp) != 0);
1692  }
1693 
1694  inline bool _isinf(const mpreal& v) {
1695  return (mpfr_inf_p(v.mp) != 0);
1696  }
1697 
1698  inline bool _isnum(const mpreal& v) {
1699  return (mpfr_number_p(v.mp) != 0);
1700  }
1701 
1702  inline bool _iszero(const mpreal& v) {
1703  return (mpfr_zero_p(v.mp) != 0);
1704  }
1705 
1706  inline bool _isint(const mpreal& v) {
1707  return (mpfr_integer_p(v.mp) != 0);
1708  }
1709 
1711  // Type Converters
1712 
1713  inline mpreal::operator double() const {
1714  return mpfr_get_d(mp, default_rnd);
1715  }
1716 
1717  inline mpreal::operator float() const {
1718  return (float) mpfr_get_d(mp, default_rnd);
1719  }
1720 
1721  inline mpreal::operator long double() const {
1722  return mpfr_get_ld(mp, default_rnd);
1723  }
1724 
1725  inline mpreal::operator unsigned long() const {
1726  return mpfr_get_ui(mp, default_rnd);
1727  }
1728 
1729  inline mpreal::operator unsigned int() const {
1730  return mpfr_get_ui(mp, default_rnd);
1731  }
1732 
1733  inline mpreal::operator long() const {
1734  return mpfr_get_si(mp, default_rnd);
1735  }
1736 
1737  inline mpreal::operator std::string() const {
1738  return to_string();
1739  }
1740 
1741  inline mpreal::operator mpfr_ptr() {
1742  return mp;
1743  }
1744 
1746  // Set/Get number properties
1747 
1748  inline int sgn(const mpreal& v) {
1749  int r = mpfr_signbit(v.mp);
1750  return (r > 0 ? -1 : 1);
1751  }
1752 
1753  inline void mpreal::set_sign(int sign, mp_rnd_t rnd_mode) {
1754  mpfr_setsign(mp, mp, (sign < 0 ? 1 : 0), rnd_mode);
1755  }
1756 
1757  inline mp_prec_t mpreal::get_prec() const {
1758  return mpfr_get_prec(mp);
1759  }
1760 
1761  inline void mpreal::set_prec(mp_prec_t prec, mp_rnd_t rnd_mode) {
1762  mpfr_prec_round(mp, prec, rnd_mode);
1763  }
1764 
1765  inline void mpreal::set_inf(int sign) {
1766  mpfr_set_inf(mp, sign);
1767  }
1768 
1769  inline void mpreal::set_nan() {
1770  mpfr_set_nan(mp);
1771  }
1772 
1773  inline mp_exp_t mpreal::get_exp() {
1774  return mpfr_get_exp(mp);
1775  }
1776 
1777  inline int mpreal::set_exp(mp_exp_t e) {
1778  return mpfr_set_exp(mp, e);
1779  }
1780 
1781  inline const mpreal frexp(const mpreal& v, mp_exp_t* exp) {
1782  mpreal x(v);
1783  *exp = x.get_exp();
1784  x.set_exp(0);
1785  return x;
1786  }
1787 
1788  inline const mpreal ldexp(const mpreal& v, mp_exp_t exp) {
1789  mpreal x(v);
1790 
1791  // rounding is not important since we just increasing the exponent
1792  mpfr_mul_2si(x.mp, x.mp, exp, mpreal::default_rnd);
1793  return x;
1794  }
1795 
1796  inline const mpreal modf(const mpreal& v, mpreal& n) {
1797  mpreal frac(v);
1798 
1799  // rounding is not important since we are using the same number
1800  mpfr_frac(frac.mp, frac.mp, mpreal::default_rnd);
1801  mpfr_trunc(n.mp, v.mp);
1802  return frac;
1803  }
1804 
1805  inline int mpreal::check_range(int t, mp_rnd_t rnd_mode) {
1806  return mpfr_check_range(mp, t, rnd_mode);
1807  }
1808 
1809  inline int mpreal::subnormalize(int t, mp_rnd_t rnd_mode) {
1810  return mpfr_subnormalize(mp, t, rnd_mode);
1811  }
1812 
1813  inline mp_exp_t mpreal::get_emin(void) {
1814  return mpfr_get_emin();
1815  }
1816 
1817  inline int mpreal::set_emin(mp_exp_t exp) {
1818  return mpfr_set_emin(exp);
1819  }
1820 
1821  inline mp_exp_t mpreal::get_emax(void) {
1822  return mpfr_get_emax();
1823  }
1824 
1825  inline int mpreal::set_emax(mp_exp_t exp) {
1826  return mpfr_set_emax(exp);
1827  }
1828 
1829  inline mp_exp_t mpreal::get_emin_min(void) {
1830  return mpfr_get_emin_min();
1831  }
1832 
1833  inline mp_exp_t mpreal::get_emin_max(void) {
1834  return mpfr_get_emin_max();
1835  }
1836 
1837  inline mp_exp_t mpreal::get_emax_min(void) {
1838  return mpfr_get_emax_min();
1839  }
1840 
1841  inline mp_exp_t mpreal::get_emax_max(void) {
1842  return mpfr_get_emax_max();
1843  }
1844 
1846  // Mathematical Functions
1848 
1849  inline const mpreal sqr(const mpreal& v, mp_rnd_t rnd_mode) {
1850  mpreal x(v);
1851  mpfr_sqr(x.mp, x.mp, rnd_mode);
1852  return x;
1853  }
1854 
1855  inline const mpreal sqrt(const mpreal& v, mp_rnd_t rnd_mode) {
1856  mpreal x(v);
1857  mpfr_sqrt(x.mp, x.mp, rnd_mode);
1858  return x;
1859  }
1860 
1861  inline const mpreal sqrt(const unsigned long int v, mp_rnd_t rnd_mode) {
1862  mpreal x;
1863  mpfr_sqrt_ui(x.mp, v, rnd_mode);
1864  return x;
1865  }
1866 
1867  inline const mpreal sqrt(const unsigned int v, mp_rnd_t rnd_mode) {
1868  return sqrt(static_cast<unsigned long int> (v), rnd_mode);
1869  }
1870 
1871  inline const mpreal sqrt(const long int v, mp_rnd_t rnd_mode) {
1872  if (v >= 0) return sqrt(static_cast<unsigned long int> (v), rnd_mode);
1873  else return mpreal(); // NaN
1874  }
1875 
1876  inline const mpreal sqrt(const int v, mp_rnd_t rnd_mode) {
1877  if (v >= 0) return sqrt(static_cast<unsigned long int> (v), rnd_mode);
1878  else return mpreal(); // NaN
1879  }
1880 
1881  inline const mpreal sqrt(const long double v, mp_rnd_t rnd_mode) {
1882  return sqrt(mpreal(v), rnd_mode);
1883  }
1884 
1885  inline const mpreal sqrt(const double v, mp_rnd_t rnd_mode) {
1886  return sqrt(mpreal(v), rnd_mode);
1887  }
1888 
1889  inline const mpreal cbrt(const mpreal& v, mp_rnd_t rnd_mode) {
1890  mpreal x(v);
1891  mpfr_cbrt(x.mp, x.mp, rnd_mode);
1892  return x;
1893  }
1894 
1895  inline const mpreal root(const mpreal& v, unsigned long int k, mp_rnd_t rnd_mode) {
1896  mpreal x(v);
1897  mpfr_root(x.mp, x.mp, k, rnd_mode);
1898  return x;
1899  }
1900 
1901  inline const mpreal fabs(const mpreal& v, mp_rnd_t rnd_mode) {
1902  mpreal x(v);
1903  mpfr_abs(x.mp, x.mp, rnd_mode);
1904  return x;
1905  }
1906 
1907  inline const mpreal abs(const mpreal& v, mp_rnd_t rnd_mode) {
1908  mpreal x(v);
1909  mpfr_abs(x.mp, x.mp, rnd_mode);
1910  return x;
1911  }
1912 
1913  inline const mpreal dim(const mpreal& a, const mpreal& b, mp_rnd_t rnd_mode) {
1914  mpreal x(a);
1915  mpfr_dim(x.mp, a.mp, b.mp, rnd_mode);
1916  return x;
1917  }
1918 
1919  inline int cmpabs(const mpreal& a, const mpreal& b) {
1920  return mpfr_cmpabs(a.mp, b.mp);
1921  }
1922 
1923  inline const mpreal log(const mpreal& v, mp_rnd_t rnd_mode) {
1924  mpreal x(v);
1925  mpfr_log(x.mp, v.mp, rnd_mode);
1926  return x;
1927  }
1928 
1929  inline const mpreal log2(const mpreal& v, mp_rnd_t rnd_mode) {
1930  mpreal x(v);
1931  mpfr_log2(x.mp, v.mp, rnd_mode);
1932  return x;
1933  }
1934 
1935  inline const mpreal log10(const mpreal& v, mp_rnd_t rnd_mode) {
1936  mpreal x(v);
1937  mpfr_log10(x.mp, v.mp, rnd_mode);
1938  return x;
1939  }
1940 
1941  inline const mpreal exp(const mpreal& v, mp_rnd_t rnd_mode) {
1942  mpreal x(v);
1943  mpfr_exp(x.mp, v.mp, rnd_mode);
1944  return x;
1945  }
1946 
1947  inline const mpreal exp2(const mpreal& v, mp_rnd_t rnd_mode) {
1948  mpreal x(v);
1949  mpfr_exp2(x.mp, v.mp, rnd_mode);
1950  return x;
1951  }
1952 
1953  inline const mpreal exp10(const mpreal& v, mp_rnd_t rnd_mode) {
1954  mpreal x(v);
1955  mpfr_exp10(x.mp, v.mp, rnd_mode);
1956  return x;
1957  }
1958 
1959  inline const mpreal cos(const mpreal& v, mp_rnd_t rnd_mode) {
1960  mpreal x(v);
1961  mpfr_cos(x.mp, v.mp, rnd_mode);
1962  return x;
1963  }
1964 
1965  inline const mpreal sin(const mpreal& v, mp_rnd_t rnd_mode) {
1966  mpreal x(v);
1967  mpfr_sin(x.mp, v.mp, rnd_mode);
1968  return x;
1969  }
1970 
1971  inline const mpreal tan(const mpreal& v, mp_rnd_t rnd_mode) {
1972  mpreal x(v);
1973  mpfr_tan(x.mp, v.mp, rnd_mode);
1974  return x;
1975  }
1976 
1977  inline const mpreal sec(const mpreal& v, mp_rnd_t rnd_mode) {
1978  mpreal x(v);
1979  mpfr_sec(x.mp, v.mp, rnd_mode);
1980  return x;
1981  }
1982 
1983  inline const mpreal csc(const mpreal& v, mp_rnd_t rnd_mode) {
1984  mpreal x(v);
1985  mpfr_csc(x.mp, v.mp, rnd_mode);
1986  return x;
1987  }
1988 
1989  inline const mpreal cot(const mpreal& v, mp_rnd_t rnd_mode) {
1990  mpreal x(v);
1991  mpfr_cot(x.mp, v.mp, rnd_mode);
1992  return x;
1993  }
1994 
1995  inline int sin_cos(mpreal& s, mpreal& c, const mpreal& v, mp_rnd_t rnd_mode) {
1996  return mpfr_sin_cos(s.mp, c.mp, v.mp, rnd_mode);
1997  }
1998 
1999  inline const mpreal acos(const mpreal& v, mp_rnd_t rnd_mode) {
2000  mpreal x(v);
2001  mpfr_acos(x.mp, v.mp, rnd_mode);
2002  return x;
2003  }
2004 
2005  inline const mpreal asin(const mpreal& v, mp_rnd_t rnd_mode) {
2006  mpreal x(v);
2007  mpfr_asin(x.mp, v.mp, rnd_mode);
2008  return x;
2009  }
2010 
2011  inline const mpreal atan(const mpreal& v, mp_rnd_t rnd_mode) {
2012  mpreal x(v);
2013  mpfr_atan(x.mp, v.mp, rnd_mode);
2014  return x;
2015  }
2016 
2017  inline const mpreal atan2(const mpreal& y, const mpreal& x, mp_rnd_t rnd_mode) {
2018  mpreal a;
2019  mp_prec_t yp, xp;
2020 
2021  yp = y.get_prec();
2022  xp = x.get_prec();
2023 
2024  a.set_prec(yp > xp ? yp : xp);
2025 
2026  mpfr_atan2(a.mp, y.mp, x.mp, rnd_mode);
2027 
2028  return a;
2029  }
2030 
2031  inline const mpreal cosh(const mpreal& v, mp_rnd_t rnd_mode) {
2032  mpreal x(v);
2033  mpfr_cosh(x.mp, v.mp, rnd_mode);
2034  return x;
2035  }
2036 
2037  inline const mpreal sinh(const mpreal& v, mp_rnd_t rnd_mode) {
2038  mpreal x(v);
2039  mpfr_sinh(x.mp, v.mp, rnd_mode);
2040  return x;
2041  }
2042 
2043  inline const mpreal tanh(const mpreal& v, mp_rnd_t rnd_mode) {
2044  mpreal x(v);
2045  mpfr_tanh(x.mp, v.mp, rnd_mode);
2046  return x;
2047  }
2048 
2049  inline const mpreal sech(const mpreal& v, mp_rnd_t rnd_mode) {
2050  mpreal x(v);
2051  mpfr_sech(x.mp, v.mp, rnd_mode);
2052  return x;
2053  }
2054 
2055  inline const mpreal csch(const mpreal& v, mp_rnd_t rnd_mode) {
2056  mpreal x(v);
2057  mpfr_csch(x.mp, v.mp, rnd_mode);
2058  return x;
2059  }
2060 
2061  inline const mpreal coth(const mpreal& v, mp_rnd_t rnd_mode) {
2062  mpreal x(v);
2063  mpfr_coth(x.mp, v.mp, rnd_mode);
2064  return x;
2065  }
2066 
2067  inline const mpreal acosh(const mpreal& v, mp_rnd_t rnd_mode) {
2068  mpreal x(v);
2069  mpfr_acosh(x.mp, v.mp, rnd_mode);
2070  return x;
2071  }
2072 
2073  inline const mpreal asinh(const mpreal& v, mp_rnd_t rnd_mode) {
2074  mpreal x(v);
2075  mpfr_asinh(x.mp, v.mp, rnd_mode);
2076  return x;
2077  }
2078 
2079  inline const mpreal atanh(const mpreal& v, mp_rnd_t rnd_mode) {
2080  mpreal x(v);
2081  mpfr_atanh(x.mp, v.mp, rnd_mode);
2082  return x;
2083  }
2084 
2085  inline const mpreal fac_ui(unsigned long int v, mp_rnd_t rnd_mode) {
2086  mpreal x;
2087  mpfr_fac_ui(x.mp, v, rnd_mode);
2088  return x;
2089  }
2090 
2091  inline const mpreal log1p(const mpreal& v, mp_rnd_t rnd_mode) {
2092  mpreal x(v);
2093  mpfr_log1p(x.mp, v.mp, rnd_mode);
2094  return x;
2095  }
2096 
2097  inline const mpreal expm1(const mpreal& v, mp_rnd_t rnd_mode) {
2098  mpreal x(v);
2099  mpfr_expm1(x.mp, v.mp, rnd_mode);
2100  return x;
2101  }
2102 
2103  inline const mpreal eint(const mpreal& v, mp_rnd_t rnd_mode) {
2104  mpreal x(v);
2105  mpfr_eint(x.mp, v.mp, rnd_mode);
2106  return x;
2107  }
2108 
2109  inline const mpreal gamma(const mpreal& v, mp_rnd_t rnd_mode) {
2110  mpreal x(v);
2111  mpfr_gamma(x.mp, v.mp, rnd_mode);
2112  return x;
2113  }
2114 
2115  inline const mpreal lngamma(const mpreal& v, mp_rnd_t rnd_mode) {
2116  mpreal x(v);
2117  mpfr_lngamma(x.mp, v.mp, rnd_mode);
2118  return x;
2119  }
2120 
2121  inline const mpreal lgamma(const mpreal& v, int *signp, mp_rnd_t rnd_mode) {
2122  mpreal x(v);
2123  mpfr_lgamma(x.mp, signp, v.mp, rnd_mode);
2124  return x;
2125  }
2126 
2127  inline const mpreal zeta(const mpreal& v, mp_rnd_t rnd_mode) {
2128  mpreal x(v);
2129  mpfr_zeta(x.mp, v.mp, rnd_mode);
2130  return x;
2131  }
2132 
2133  inline const mpreal erf(const mpreal& v, mp_rnd_t rnd_mode) {
2134  mpreal x(v);
2135  mpfr_erf(x.mp, v.mp, rnd_mode);
2136  return x;
2137  }
2138 
2139  inline const mpreal erfc(const mpreal& v, mp_rnd_t rnd_mode) {
2140  mpreal x(v);
2141  mpfr_erfc(x.mp, v.mp, rnd_mode);
2142  return x;
2143  }
2144 
2145  inline const mpreal _j0(const mpreal& v, mp_rnd_t rnd_mode) {
2146  mpreal x(v);
2147  mpfr_j0(x.mp, v.mp, rnd_mode);
2148  return x;
2149  }
2150 
2151  inline const mpreal _j1(const mpreal& v, mp_rnd_t rnd_mode) {
2152  mpreal x(v);
2153  mpfr_j1(x.mp, v.mp, rnd_mode);
2154  return x;
2155  }
2156 
2157  inline const mpreal _jn(long n, const mpreal& v, mp_rnd_t rnd_mode) {
2158  mpreal x(v);
2159  mpfr_jn(x.mp, n, v.mp, rnd_mode);
2160  return x;
2161  }
2162 
2163  inline const mpreal _y0(const mpreal& v, mp_rnd_t rnd_mode) {
2164  mpreal x(v);
2165  mpfr_y0(x.mp, v.mp, rnd_mode);
2166  return x;
2167  }
2168 
2169  inline const mpreal _y1(const mpreal& v, mp_rnd_t rnd_mode) {
2170  mpreal x(v);
2171  mpfr_y1(x.mp, v.mp, rnd_mode);
2172  return x;
2173  }
2174 
2175  inline const mpreal _yn(long n, const mpreal& v, mp_rnd_t rnd_mode) {
2176  mpreal x(v);
2177  mpfr_yn(x.mp, n, v.mp, rnd_mode);
2178  return x;
2179  }
2180 
2181 
2182  // MPFR 2.4.0 Specifics
2183 #if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
2184 
2185  inline int sinh_cosh(mpreal& s, mpreal& c, const mpreal& v, mp_rnd_t rnd_mode) {
2186  return mpfr_sinh_cosh(s.mp, c.mp, v.mp, rnd_mode);
2187  }
2188 
2189  inline const mpreal li2(const mpreal& v, mp_rnd_t rnd_mode) {
2190  mpreal x(v);
2191  mpfr_li2(x.mp, v.mp, rnd_mode);
2192  return x;
2193  }
2194 
2195  inline const mpreal fmod(const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode) {
2196  mpreal a;
2197  mp_prec_t yp, xp;
2198 
2199  yp = y.get_prec();
2200  xp = x.get_prec();
2201 
2202  a.set_prec(yp > xp ? yp : xp);
2203 
2204  mpfr_fmod(a.mp, x.mp, y.mp, rnd_mode);
2205 
2206  return a;
2207  }
2208 
2209  inline const mpreal rec_sqrt(const mpreal& v, mp_rnd_t rnd_mode) {
2210  mpreal x(v);
2211  mpfr_rec_sqrt(x.mp, v.mp, rnd_mode);
2212  return x;
2213  }
2214 
2215 #endif
2216 
2218  // Constants
2219 
2220  inline const mpreal const_log2(mp_prec_t prec, mp_rnd_t rnd_mode) {
2221  mpreal x;
2222  x.set_prec(prec);
2223  mpfr_const_log2(x.mp, rnd_mode);
2224  return x;
2225  }
2226 
2227  inline const mpreal const_pi(mp_prec_t prec, mp_rnd_t rnd_mode) {
2228  mpreal x;
2229  x.set_prec(prec);
2230  mpfr_const_pi(x.mp, rnd_mode);
2231  return x;
2232  }
2233 
2234  inline const mpreal const_euler(mp_prec_t prec, mp_rnd_t rnd_mode) {
2235  mpreal x;
2236  x.set_prec(prec);
2237  mpfr_const_euler(x.mp, rnd_mode);
2238  return x;
2239  }
2240 
2241  inline const mpreal const_catalan(mp_prec_t prec, mp_rnd_t rnd_mode) {
2242  mpreal x;
2243  x.set_prec(prec);
2244  mpfr_const_catalan(x.mp, rnd_mode);
2245  return x;
2246  }
2247 
2249  // Integer Related Functions
2250 
2251  inline const mpreal rint(const mpreal& v, mp_rnd_t rnd_mode) {
2252  mpreal x(v);
2253  mpfr_rint(x.mp, v.mp, rnd_mode);
2254  return x;
2255  }
2256 
2257  inline const mpreal ceil(const mpreal& v) {
2258  mpreal x(v);
2259  mpfr_ceil(x.mp, v.mp);
2260  return x;
2261 
2262  }
2263 
2264  inline const mpreal floor(const mpreal& v) {
2265  mpreal x(v);
2266  mpfr_floor(x.mp, v.mp);
2267  return x;
2268  }
2269 
2270  inline const mpreal round(const mpreal& v) {
2271  mpreal x(v);
2272  mpfr_round(x.mp, v.mp);
2273  return x;
2274  }
2275 
2276  inline const mpreal trunc(const mpreal& v) {
2277  mpreal x(v);
2278  mpfr_trunc(x.mp, v.mp);
2279  return x;
2280  }
2281 
2282  inline const mpreal rint_ceil(const mpreal& v, mp_rnd_t rnd_mode) {
2283  mpreal x(v);
2284  mpfr_rint_ceil(x.mp, v.mp, rnd_mode);
2285  return x;
2286  }
2287 
2288  inline const mpreal rint_floor(const mpreal& v, mp_rnd_t rnd_mode) {
2289  mpreal x(v);
2290  mpfr_rint_floor(x.mp, v.mp, rnd_mode);
2291  return x;
2292  }
2293 
2294  inline const mpreal rint_round(const mpreal& v, mp_rnd_t rnd_mode) {
2295  mpreal x(v);
2296  mpfr_rint_round(x.mp, v.mp, rnd_mode);
2297  return x;
2298  }
2299 
2300  inline const mpreal rint_trunc(const mpreal& v, mp_rnd_t rnd_mode) {
2301  mpreal x(v);
2302  mpfr_rint_trunc(x.mp, v.mp, rnd_mode);
2303  return x;
2304  }
2305 
2306  inline const mpreal frac(const mpreal& v, mp_rnd_t rnd_mode) {
2307  mpreal x(v);
2308  mpfr_frac(x.mp, v.mp, rnd_mode);
2309  return x;
2310  }
2311 
2313  // Miscellaneous Functions
2314 
2315  inline void swap(mpreal& a, mpreal& b) {
2316  mpfr_swap(a.mp, b.mp);
2317  }
2318 
2319 #ifndef max
2320 
2321  inline const mpreal max(const mpreal& x, const mpreal& y) {
2322  return (x > y ? x : y);
2323  }
2324 #endif
2325 
2326 #ifndef min
2327 
2328  inline const mpreal min(const mpreal& x, const mpreal& y) {
2329  return (x < y ? x : y);
2330  }
2331 #endif
2332 
2333  inline const mpreal nexttoward(const mpreal& x, const mpreal& y) {
2334  mpreal a(x);
2335  mpfr_nexttoward(a.mp, y.mp);
2336  return a;
2337  }
2338 
2339  inline const mpreal nextabove(const mpreal& x) {
2340  mpreal a(x);
2341  mpfr_nextabove(a.mp);
2342  return a;
2343  }
2344 
2345  inline const mpreal nextbelow(const mpreal& x) {
2346  mpreal a(x);
2347  mpfr_nextbelow(a.mp);
2348  return a;
2349  }
2350 
2351  inline const mpreal urandomb(gmp_randstate_t& state) {
2352  mpreal x;
2353  mpfr_urandomb(x.mp, state);
2354  return x;
2355  }
2356 
2357  /*inline const mpreal random2 (mp_size_t size, mp_exp_t exp)
2358  {
2359  mpreal x;
2360  mpfr_random2(x.mp,size,exp);
2361  return x;
2362  }*/
2363 
2365  // Set/Get global properties
2366 
2367  inline void mpreal::set_default_prec(mp_prec_t prec) {
2368  default_prec = prec;
2369  mpfr_set_default_prec(prec);
2370  }
2371 
2372  inline mp_prec_t mpreal::get_default_prec() {
2373  return mpfr_get_default_prec();
2374  }
2375 
2376  inline void mpreal::set_default_base(int base) {
2377  default_base = base;
2378  }
2379 
2380  inline int mpreal::get_default_base() {
2381  return default_base;
2382  }
2383 
2384  inline void mpreal::set_default_rnd(mp_rnd_t rnd_mode) {
2385  default_rnd = rnd_mode;
2386  mpfr_set_default_rounding_mode(rnd_mode);
2387  }
2388 
2389  inline mp_rnd_t mpreal::get_default_rnd() {
2390  return mpfr_get_default_rounding_mode();
2391  }
2392 
2393  inline void mpreal::set_double_bits(int dbits) {
2394  double_bits = dbits;
2395  }
2396 
2397  inline int mpreal::get_double_bits() {
2398  return double_bits;
2399  }
2400 
2401  inline bool mpreal::fits_in_bits(double x, int n) {
2402  int i;
2403  double t;
2404  return IsInf(x) || (std::modf(std::ldexp(std::frexp(x, &i), n), &t) == 0.0);
2405  }
2406 
2407  inline const mpreal pow(const mpreal& a, const mpreal& b, mp_rnd_t rnd_mode) {
2408  mpreal x(a);
2409  mpfr_pow(x.mp, x.mp, b.mp, rnd_mode);
2410  return x;
2411  }
2412 
2413  inline const mpreal pow(const mpreal& a, const mpz_t b, mp_rnd_t rnd_mode) {
2414  mpreal x(a);
2415  mpfr_pow_z(x.mp, x.mp, b, rnd_mode);
2416  return x;
2417  }
2418 
2419  inline const mpreal pow(const mpreal& a, const unsigned long int b, mp_rnd_t rnd_mode) {
2420  mpreal x(a);
2421  mpfr_pow_ui(x.mp, x.mp, b, rnd_mode);
2422  return x;
2423  }
2424 
2425  inline const mpreal pow(const mpreal& a, const unsigned int b, mp_rnd_t rnd_mode) {
2426  return pow(a, static_cast<unsigned long int> (b), rnd_mode);
2427  }
2428 
2429  inline const mpreal pow(const mpreal& a, const long int b, mp_rnd_t rnd_mode) {
2430  mpreal x(a);
2431  mpfr_pow_si(x.mp, x.mp, b, rnd_mode);
2432  return x;
2433  }
2434 
2435  inline const mpreal pow(const mpreal& a, const int b, mp_rnd_t rnd_mode) {
2436  return pow(a, static_cast<long int> (b), rnd_mode);
2437  }
2438 
2439  inline const mpreal pow(const mpreal& a, const long double b, mp_rnd_t rnd_mode) {
2440  return pow(a, mpreal(b), rnd_mode);
2441  }
2442 
2443  inline const mpreal pow(const mpreal& a, const double b, mp_rnd_t rnd_mode) {
2444  return pow(a, mpreal(b), rnd_mode);
2445  }
2446 
2447  inline const mpreal pow(const unsigned long int a, const mpreal& b, mp_rnd_t rnd_mode) {
2448  mpreal x(a);
2449  mpfr_ui_pow(x.mp, a, b.mp, rnd_mode);
2450  return x;
2451  }
2452 
2453  inline const mpreal pow(const unsigned int a, const mpreal& b, mp_rnd_t rnd_mode) {
2454  return pow(static_cast<unsigned long int> (a), b, rnd_mode);
2455  }
2456 
2457  inline const mpreal pow(const long int a, const mpreal& b, mp_rnd_t rnd_mode) {
2458  if (a >= 0) return pow(static_cast<unsigned long int> (a), b, rnd_mode);
2459  else return pow(mpreal(a), b, rnd_mode);
2460  }
2461 
2462  inline const mpreal pow(const int a, const mpreal& b, mp_rnd_t rnd_mode) {
2463  if (a >= 0) return pow(static_cast<unsigned long int> (a), b, rnd_mode);
2464  else return pow(mpreal(a), b, rnd_mode);
2465  }
2466 
2467  inline const mpreal pow(const long double a, const mpreal& b, mp_rnd_t rnd_mode) {
2468  return pow(mpreal(a), b, rnd_mode);
2469  }
2470 
2471  inline const mpreal pow(const double a, const mpreal& b, mp_rnd_t rnd_mode) {
2472  return pow(mpreal(a), b, rnd_mode);
2473  }
2474 
2475  // pow unsigned long int
2476 
2477  inline const mpreal pow(const unsigned long int a, const unsigned long int b, mp_rnd_t rnd_mode) {
2478  mpreal x(a);
2479  mpfr_ui_pow_ui(x.mp, a, b, rnd_mode);
2480  return x;
2481  }
2482 
2483  inline const mpreal pow(const unsigned long int a, const unsigned int b, mp_rnd_t rnd_mode) {
2484  return pow(a, static_cast<unsigned long int> (b), rnd_mode); //mpfr_ui_pow_ui
2485  }
2486 
2487  inline const mpreal pow(const unsigned long int a, const long int b, mp_rnd_t rnd_mode) {
2488  if (b > 0) return pow(a, static_cast<unsigned long int> (b), rnd_mode); //mpfr_ui_pow_ui
2489  else return pow(a, mpreal(b), rnd_mode); //mpfr_ui_pow
2490  }
2491 
2492  inline const mpreal pow(const unsigned long int a, const int b, mp_rnd_t rnd_mode) {
2493  if (b > 0) return pow(a, static_cast<unsigned long int> (b), rnd_mode); //mpfr_ui_pow_ui
2494  else return pow(a, mpreal(b), rnd_mode); //mpfr_ui_pow
2495  }
2496 
2497  inline const mpreal pow(const unsigned long int a, const long double b, mp_rnd_t rnd_mode) {
2498  return pow(a, mpreal(b), rnd_mode); //mpfr_ui_pow
2499  }
2500 
2501  inline const mpreal pow(const unsigned long int a, const double b, mp_rnd_t rnd_mode) {
2502  return pow(a, mpreal(b), rnd_mode); //mpfr_ui_pow
2503  }
2504 
2505  // pow unsigned int
2506 
2507  inline const mpreal pow(const unsigned int a, const unsigned long int b, mp_rnd_t rnd_mode) {
2508  return pow(static_cast<unsigned long int> (a), b, rnd_mode); //mpfr_ui_pow_ui
2509  }
2510 
2511  inline const mpreal pow(const unsigned int a, const unsigned int b, mp_rnd_t rnd_mode) {
2512  return pow(static_cast<unsigned long int> (a), static_cast<unsigned long int> (b), rnd_mode); //mpfr_ui_pow_ui
2513  }
2514 
2515  inline const mpreal pow(const unsigned int a, const long int b, mp_rnd_t rnd_mode) {
2516  if (b > 0) return pow(static_cast<unsigned long int> (a), static_cast<unsigned long int> (b), rnd_mode); //mpfr_ui_pow_ui
2517  else return pow(static_cast<unsigned long int> (a), mpreal(b), rnd_mode); //mpfr_ui_pow
2518  }
2519 
2520  inline const mpreal pow(const unsigned int a, const int b, mp_rnd_t rnd_mode) {
2521  if (b > 0) return pow(static_cast<unsigned long int> (a), static_cast<unsigned long int> (b), rnd_mode); //mpfr_ui_pow_ui
2522  else return pow(static_cast<unsigned long int> (a), mpreal(b), rnd_mode); //mpfr_ui_pow
2523  }
2524 
2525  inline const mpreal pow(const unsigned int a, const long double b, mp_rnd_t rnd_mode) {
2526  return pow(static_cast<unsigned long int> (a), mpreal(b), rnd_mode); //mpfr_ui_pow
2527  }
2528 
2529  inline const mpreal pow(const unsigned int a, const double b, mp_rnd_t rnd_mode) {
2530  return pow(static_cast<unsigned long int> (a), mpreal(b), rnd_mode); //mpfr_ui_pow
2531  }
2532 
2533  // pow long int
2534 
2535  inline const mpreal pow(const long int a, const unsigned long int b, mp_rnd_t rnd_mode) {
2536  if (a > 0) return pow(static_cast<unsigned long int> (a), b, rnd_mode); //mpfr_ui_pow_ui
2537  else return pow(mpreal(a), b, rnd_mode); //mpfr_pow_ui
2538  }
2539 
2540  inline const mpreal pow(const long int a, const unsigned int b, mp_rnd_t rnd_mode) {
2541  if (a > 0) return pow(static_cast<unsigned long int> (a), static_cast<unsigned long int> (b), rnd_mode); //mpfr_ui_pow_ui
2542  else return pow(mpreal(a), static_cast<unsigned long int> (b), rnd_mode); //mpfr_pow_ui
2543  }
2544 
2545  inline const mpreal pow(const long int a, const long int b, mp_rnd_t rnd_mode) {
2546  if (a > 0) {
2547  if (b > 0) return pow(static_cast<unsigned long int> (a), static_cast<unsigned long int> (b), rnd_mode); //mpfr_ui_pow_ui
2548  else return pow(static_cast<unsigned long int> (a), mpreal(b), rnd_mode); //mpfr_ui_pow
2549  } else {
2550  return pow(mpreal(a), b, rnd_mode); // mpfr_pow_si
2551  }
2552  }
2553 
2554  inline const mpreal pow(const long int a, const int b, mp_rnd_t rnd_mode) {
2555  if (a > 0) {
2556  if (b > 0) return pow(static_cast<unsigned long int> (a), static_cast<unsigned long int> (b), rnd_mode); //mpfr_ui_pow_ui
2557  else return pow(static_cast<unsigned long int> (a), mpreal(b), rnd_mode); //mpfr_ui_pow
2558  } else {
2559  return pow(mpreal(a), static_cast<long int> (b), rnd_mode); // mpfr_pow_si
2560  }
2561  }
2562 
2563  inline const mpreal pow(const long int a, const long double b, mp_rnd_t rnd_mode) {
2564  if (a >= 0) return pow(static_cast<unsigned long int> (a), mpreal(b), rnd_mode); //mpfr_ui_pow
2565  else return pow(mpreal(a), mpreal(b), rnd_mode); //mpfr_pow
2566  }
2567 
2568  inline const mpreal pow(const long int a, const double b, mp_rnd_t rnd_mode) {
2569  if (a >= 0) return pow(static_cast<unsigned long int> (a), mpreal(b), rnd_mode); //mpfr_ui_pow
2570  else return pow(mpreal(a), mpreal(b), rnd_mode); //mpfr_pow
2571  }
2572 
2573  // pow int
2574 
2575  inline const mpreal pow(const int a, const unsigned long int b, mp_rnd_t rnd_mode) {
2576  if (a > 0) return pow(static_cast<unsigned long int> (a), b, rnd_mode); //mpfr_ui_pow_ui
2577  else return pow(mpreal(a), b, rnd_mode); //mpfr_pow_ui
2578  }
2579 
2580  inline const mpreal pow(const int a, const unsigned int b, mp_rnd_t rnd_mode) {
2581  if (a > 0) return pow(static_cast<unsigned long int> (a), static_cast<unsigned long int> (b), rnd_mode); //mpfr_ui_pow_ui
2582  else return pow(mpreal(a), static_cast<unsigned long int> (b), rnd_mode); //mpfr_pow_ui
2583  }
2584 
2585  inline const mpreal pow(const int a, const long int b, mp_rnd_t rnd_mode) {
2586  if (a > 0) {
2587  if (b > 0) return pow(static_cast<unsigned long int> (a), static_cast<unsigned long int> (b), rnd_mode); //mpfr_ui_pow_ui
2588  else return pow(static_cast<unsigned long int> (a), mpreal(b), rnd_mode); //mpfr_ui_pow
2589  } else {
2590  return pow(mpreal(a), b, rnd_mode); // mpfr_pow_si
2591  }
2592  }
2593 
2594  inline const mpreal pow(const int a, const int b, mp_rnd_t rnd_mode) {
2595  if (a > 0) {
2596  if (b > 0) return pow(static_cast<unsigned long int> (a), static_cast<unsigned long int> (b), rnd_mode); //mpfr_ui_pow_ui
2597  else return pow(static_cast<unsigned long int> (a), mpreal(b), rnd_mode); //mpfr_ui_pow
2598  } else {
2599  return pow(mpreal(a), static_cast<long int> (b), rnd_mode); // mpfr_pow_si
2600  }
2601  }
2602 
2603  inline const mpreal pow(const int a, const long double b, mp_rnd_t rnd_mode) {
2604  if (a >= 0) return pow(static_cast<unsigned long int> (a), mpreal(b), rnd_mode); //mpfr_ui_pow
2605  else return pow(mpreal(a), mpreal(b), rnd_mode); //mpfr_pow
2606  }
2607 
2608  inline const mpreal pow(const int a, const double b, mp_rnd_t rnd_mode) {
2609  if (a >= 0) return pow(static_cast<unsigned long int> (a), mpreal(b), rnd_mode); //mpfr_ui_pow
2610  else return pow(mpreal(a), mpreal(b), rnd_mode); //mpfr_pow
2611  }
2612 
2613  // pow long double
2614 
2615  inline const mpreal pow(const long double a, const long double b, mp_rnd_t rnd_mode) {
2616  return pow(mpreal(a), mpreal(b), rnd_mode);
2617  }
2618 
2619  inline const mpreal pow(const long double a, const unsigned long int b, mp_rnd_t rnd_mode) {
2620  return pow(mpreal(a), b, rnd_mode); //mpfr_pow_ui
2621  }
2622 
2623  inline const mpreal pow(const long double a, const unsigned int b, mp_rnd_t rnd_mode) {
2624  return pow(mpreal(a), static_cast<unsigned long int> (b), rnd_mode); //mpfr_pow_ui
2625  }
2626 
2627  inline const mpreal pow(const long double a, const long int b, mp_rnd_t rnd_mode) {
2628  return pow(mpreal(a), b, rnd_mode); // mpfr_pow_si
2629  }
2630 
2631  inline const mpreal pow(const long double a, const int b, mp_rnd_t rnd_mode) {
2632  return pow(mpreal(a), static_cast<long int> (b), rnd_mode); // mpfr_pow_si
2633  }
2634 
2635  inline const mpreal pow(const double a, const double b, mp_rnd_t rnd_mode) {
2636  return pow(mpreal(a), mpreal(b), rnd_mode);
2637  }
2638 
2639  inline const mpreal pow(const double a, const unsigned long int b, mp_rnd_t rnd_mode) {
2640  return pow(mpreal(a), b, rnd_mode); // mpfr_pow_ui
2641  }
2642 
2643  inline const mpreal pow(const double a, const unsigned int b, mp_rnd_t rnd_mode) {
2644  return pow(mpreal(a), static_cast<unsigned long int> (b), rnd_mode); // mpfr_pow_ui
2645  }
2646 
2647  inline const mpreal pow(const double a, const long int b, mp_rnd_t rnd_mode) {
2648  return pow(mpreal(a), b, rnd_mode); // mpfr_pow_si
2649  }
2650 
2651  inline const mpreal pow(const double a, const int b, mp_rnd_t rnd_mode) {
2652  return pow(mpreal(a), static_cast<long int> (b), rnd_mode); // mpfr_pow_si
2653  }
2654 }
2655 
2656 // Explicit specialization of std::swap for mpreal numbers
2657 // Thus standard algorithms will use efficient version of swap (due to Koenig lookup)
2658 // Non-throwing swap C++ idiom: http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Non-throwing_swap
2659 namespace std {
2660 
2661  template <>
2662  inline void swap(mpfr::mpreal& x, mpfr::mpreal& y) {
2663  return mpfr::swap(x, y);
2664  }
2665 }
2666 
2667 #endif /* __MP_REAL_H__ */
Definition: mpreal.h:52
Definition: mpreal.h:383