File: mpfrxx.h

package info (click to toggle)
libgmp3 4.0.1-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,164 kB
  • ctags: 14,528
  • sloc: ansic: 61,605; asm: 36,633; sh: 7,541; perl: 2,072; cpp: 1,278; makefile: 869; lisp: 193; yacc: 152; lex: 79; fortran: 24
file content (882 lines) | stat: -rw-r--r-- 30,345 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
/* mpfrxx.h -- C++ class wrapper for MPFR.  -*- C++ -*-

Copyright 2001 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.

The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */

#ifndef __GMPFR_PLUSPLUS__
#define __GMPFR_PLUSPLUS__

#include <iostream>
#include <string>
#include <strstream>
#include <gmp.h>
#include <mpfr.h>
#include "gmpxx.h"


class __gmpfr_value { };

template <class T, class U>
void __gmp_set_expr(mpfr_ptr, const __gmp_expr<T, U> &);

const int mpfr_default_base = 10;


/**************** Macros for in-class declarations ****************/

#define __GMPFRR_DECLARE_COMPOUND_OPERATOR(fun)                             \
  template <class T, class U>                                               \
  __gmp_expr<__gmpfr_value, __gmpfr_value> & fun(const __gmp_expr<T, U> &);

#define __GMPFRN_DECLARE_COMPOUND_OPERATOR(fun) \
  __gmp_expr & fun(bool);                       \
  __gmp_expr & fun(signed char);                \
  __gmp_expr & fun(unsigned char);              \
  __gmp_expr & fun(signed int);                 \
  __gmp_expr & fun(unsigned int);               \
  __gmp_expr & fun(signed short int);           \
  __gmp_expr & fun(unsigned short int);         \
  __gmp_expr & fun(signed long int);            \
  __gmp_expr & fun(unsigned long int);          \
  __gmp_expr & fun(float);                      \
  __gmp_expr & fun(double);                     \
  __gmp_expr & fun(long double);

#define __GMPFR_DECLARE_COMPOUND_OPERATOR(fun) \
__GMPFRR_DECLARE_COMPOUND_OPERATOR(fun)        \
__GMPFRN_DECLARE_COMPOUND_OPERATOR(fun)

#define __GMPFR_DECLARE_COMPOUND_OPERATOR_UI(fun) \
  __gmp_expr & fun(unsigned long int);

#define __GMPFR_DECLARE_INCREMENT_OPERATOR(fun) \
  inline __gmp_expr & fun();                    \
  inline __gmp_expr fun(int);


/**************** mpfr_class -- wrapper for mpfr_t ****************/

template <>
class __gmp_expr<__gmpfr_value, __gmpfr_value>
{
private:
  mpfr_t mp;
public:
  // size information
  unsigned long int get_prec() const { return mpfr_get_prec(mp); }

  // constructors and destructor
  __gmp_expr() { mpfr_init(mp); }

  __gmp_expr(const __gmp_expr &f)
  {
    mpfr_init2(mp, f.get_prec());
    mpfr_set(mp, f.mp, __gmp_default_rounding_mode);
  }
  __gmp_expr(const __gmp_expr &f, unsigned long int prec,
	     char mode = __gmp_default_rounding_mode)
  { mpfr_init2(mp, prec); mpfr_set(mp, f.mp, mode); }
  template <class T, class U>
  __gmp_expr(const __gmp_expr<T, U> &expr)
  { mpfr_init2(mp, expr.get_prec()); __gmp_set_expr(mp, expr); }
  template <class T, class U>
  __gmp_expr(const __gmp_expr<T, U> &expr, unsigned long int prec)
  { mpfr_init2(mp, prec); __gmp_set_expr(mp, expr); }

  __gmp_expr(bool b)
  { mpfr_init(mp); mpfr_set_ui(mp, b, __gmp_default_rounding_mode); }
  __gmp_expr(bool b, unsigned long int prec,
	     char mode = __gmp_default_rounding_mode)
  { mpfr_init2(mp, prec); mpfr_set_ui(mp, b, mode); }

  __gmp_expr(signed char c)
  { mpfr_init(mp); mpfr_set_si(mp, c, __gmp_default_rounding_mode); }
  __gmp_expr(signed char c, unsigned long int prec,
	     char mode = __gmp_default_rounding_mode)
  { mpfr_init2(mp, prec); mpfr_set_si(mp, c, mode); }
  __gmp_expr(unsigned char c)
  { mpfr_init(mp); mpfr_set_ui(mp, c, __gmp_default_rounding_mode); }
  __gmp_expr(unsigned char c, unsigned long int prec,
	     char mode = __gmp_default_rounding_mode)
  { mpfr_init2(mp, prec); mpfr_set_ui(mp, c, mode); }

  __gmp_expr(signed int i)
  { mpfr_init(mp); mpfr_set_si(mp, i, __gmp_default_rounding_mode); }
  __gmp_expr(signed int i, unsigned long int prec,
	     char mode = __gmp_default_rounding_mode)
  { mpfr_init2(mp, prec); mpfr_set_si(mp, i, mode); }
  __gmp_expr(unsigned int i)
  { mpfr_init(mp); mpfr_set_ui(mp, i, __gmp_default_rounding_mode); }
  __gmp_expr(unsigned int i, unsigned long int prec,
	     char mode = __gmp_default_rounding_mode)
  { mpfr_init2(mp, prec); mpfr_set_ui(mp, i, mode); }

  __gmp_expr(signed short int s)
  { mpfr_init(mp); mpfr_set_si(mp, s, __gmp_default_rounding_mode); }
  __gmp_expr(signed short int s, unsigned long int prec,
	     char mode = __gmp_default_rounding_mode)
  { mpfr_init2(mp, prec); mpfr_set_si(mp, s, mode); }
  __gmp_expr(unsigned short int s)
  { mpfr_init(mp); mpfr_set_ui(mp, s, __gmp_default_rounding_mode); }
  __gmp_expr(unsigned short int s, unsigned long int prec,
	     char mode = __gmp_default_rounding_mode)
  { mpfr_init2(mp, prec); mpfr_set_ui(mp, s, mode); }

  __gmp_expr(signed long int l)
  { mpfr_init(mp); mpfr_set_si(mp, l, __gmp_default_rounding_mode); }
  __gmp_expr(signed long int l, unsigned long int prec,
	     char mode = __gmp_default_rounding_mode)
  { mpfr_init2(mp, prec); mpfr_set_si(mp, l, mode); }
  __gmp_expr(unsigned long int l)
  { mpfr_init(mp); mpfr_set_ui(mp, l, __gmp_default_rounding_mode); }
  __gmp_expr(unsigned long int l, unsigned long int prec,
	     char mode = __gmp_default_rounding_mode)
  { mpfr_init2(mp, prec); mpfr_set_ui(mp, l, mode); }

  __gmp_expr(float f)
  { mpfr_init(mp); mpfr_set_d(mp, f, __gmp_default_rounding_mode); }
  __gmp_expr(float f, unsigned long int prec,
	     char mode = __gmp_default_rounding_mode)
  { mpfr_init2(mp, prec); mpfr_set_d(mp, f, mode); }
  __gmp_expr(double d)
  { mpfr_init(mp); mpfr_set_d(mp, d, __gmp_default_rounding_mode); }
  __gmp_expr(double d, unsigned long int prec,
	     char mode = __gmp_default_rounding_mode)
  { mpfr_init2(mp, prec); mpfr_set_d(mp, d, mode); }
  /*
  __gmp_expr(long double ld)
  { mpfr_init(mp); mpfr_set_d(mp, ld, __gmp_default_rounding_mode); }
  __gmp_expr(long double ld, unsigned long int prec,
	     char mode = __gmp_default_rounding_mode)
  { mpfr_init2(mp, prec); mpfr_set_d(mp, ld, mode); }
  */

  /*
  explicit __gmp_expr(const char *s)
  { mpfr_init_set_str(mp, s, mpfr_default_base); }
  __gmp_expr(const char *s, int base) { mpfr_init_set_str(mp, s, base); }
  explicit __gmp_expr(const std::string &s)
  { mpfr_init_set_str(mp, s.c_str(), mpfr_default_base); }
  __gmp_expr(const std::string &s, int base)
  { mpfr_init_set_str(mp, s.c_str(), base); }
  */

  explicit __gmp_expr(mpfr_srcptr f)
  {
    mpfr_init2(mp, mpfr_get_prec(f));
    mpfr_set(mp, f, __gmp_default_rounding_mode);
  }
  explicit __gmp_expr(mpfr_srcptr f, unsigned long int prec)
  {
    mpfr_init2(mp, prec);
    mpfr_set(mp, f, __gmp_default_rounding_mode);
  }

  ~__gmp_expr() { mpfr_clear(mp); }

  // assignment operators
  __gmp_expr & operator=(const __gmp_expr &f)
  { mpfr_set(mp, f.mp, __gmp_default_rounding_mode); return *this; }
  template <class T, class U>
  __gmp_expr<__gmpfr_value, __gmpfr_value> & operator=
  (const __gmp_expr<T, U> &expr)
  { __gmp_set_expr(mp, expr); return *this; }

  __gmp_expr & operator=(bool b)
  { mpfr_set_ui(mp, b, __gmp_default_rounding_mode); return *this; }

  __gmp_expr & operator=(signed char c)
  { mpfr_set_si(mp, c, __gmp_default_rounding_mode); return *this; }
  __gmp_expr & operator=(unsigned char c)
  { mpfr_set_ui(mp, c, __gmp_default_rounding_mode); return *this; }

  __gmp_expr & operator=(signed int i)
  { mpfr_set_si(mp, i, __gmp_default_rounding_mode); return *this; }
  __gmp_expr & operator=(unsigned int i)
  { mpfr_set_ui(mp, i, __gmp_default_rounding_mode); return *this; }

  __gmp_expr & operator=(signed short int s)
  { mpfr_set_si(mp, s, __gmp_default_rounding_mode); return *this; }
  __gmp_expr & operator=(unsigned short int s)
  { mpfr_set_ui(mp, s, __gmp_default_rounding_mode); return *this; }

  __gmp_expr & operator=(signed long int l)
  { mpfr_set_si(mp, l, __gmp_default_rounding_mode); return *this; }
  __gmp_expr & operator=(unsigned long int l)
  { mpfr_set_ui(mp, l, __gmp_default_rounding_mode); return *this; }

  __gmp_expr & operator=(float f)
  { mpfr_set_d(mp, f, __gmp_default_rounding_mode); return *this; }
  __gmp_expr & operator=(double d)
  { mpfr_set_d(mp, d, __gmp_default_rounding_mode); return *this; }
  /*
  __gmp_expr & operator=(long double ld)
  { mpfr_set_d(mp, ld, __gmp_default_rounding_mode); return *this; }
  */

  /*
  __gmp_expr & operator=(const char *s)
  { mpfr_set_str(mp, s, mpfr_default_base); return *this; }
  __gmp_expr & operator=(const std::string &s)
  { mpfr_set_str(mp, s.c_str(), mpfr_default_base); return *this; }

  // string input/output functions
  int set_str(const std::string &s, int base)
  { return mpfr_set_str(mp, s.c_str(), base); }
  */
  std::string get_str(mp_exp_t *expo, int base, size_t size,
		      mp_rnd_t rmode = __gmp_default_rounding_mode) const
  {
    __gmp_alloc_cstring temp(mpfr_get_str(0, expo, base, size, mp, rmode));
    return std::string(temp.str);
  }
  std::string get_str2(int base = 10,
		       mp_rnd_t rmode = __gmp_default_rounding_mode) const
  {
    std::ostrstream o;
    mp_exp_t expo;
    std::string temp(mpfr_get_str(0, &expo, base, 0, mp, rmode));

    if (temp[0] == '-')
      o << "-0." << temp.substr(1);
    else
      o << "0." << temp;

    if (base <= 10)
      o << "e" << expo << '\0';
    else
      o << "@" << expo << '\0';

    return o.str();
  }

  // conversion functions
  mpfr_srcptr get_mpfr_t() const { return mp; }
  mpfr_ptr get_mpfr_t() { return mp; }

  // signed long get_si() const { return mpfr_get_si(mp); }
  // unsigned long get_ui() const { return mpfr_get_ui(mp); }
  double get_d() const { return mpfr_get_d(mp); } // should be long double

  // compound assignments
  __GMPFR_DECLARE_COMPOUND_OPERATOR(operator+=)
  __GMPFR_DECLARE_COMPOUND_OPERATOR(operator-=)
  __GMPFR_DECLARE_COMPOUND_OPERATOR(operator*=)
  __GMPFR_DECLARE_COMPOUND_OPERATOR(operator/=)

  __GMPFR_DECLARE_COMPOUND_OPERATOR_UI(operator<<=)
  __GMPFR_DECLARE_COMPOUND_OPERATOR_UI(operator>>=)

  __GMPFR_DECLARE_INCREMENT_OPERATOR(operator++)
  __GMPFR_DECLARE_INCREMENT_OPERATOR(operator--)
};

typedef __gmp_expr<__gmpfr_value, __gmpfr_value> mpfr_class;


inline std::ostream & operator<<(std::ostream &o, const mpfr_class &f)
{
  mp_exp_t expo;
  int base = 10;
  __gmp_alloc_cstring temp(mpfr_get_str(0, &expo, base, 0, f.get_mpfr_t(),
					__gmp_default_rounding_mode));

  // cancel terminating zeros
  for (size_t i=strlen(temp.str)-1; temp.str[i]=='0' && i>0; --i)
    temp.str[i] = '\0';

  if (*temp.str == '-')
    o << "-0." << temp.str+1;
  else
    o << "0." << temp.str;

  if (base <= 10)
    o << "e" << expo;
  else
    o << "@" << expo;

  return o;
}

template <class T>
inline std::ostream & operator<<
(std::ostream &o, const __gmp_expr<__gmpfr_value, T> &expr)
{
  mpfr_class temp(expr);
  return o << temp;
}

inline std::istream & operator>>(std::istream &i, mpfr_class &f)
{
  mpf_t temp;
  mpf_init2(temp, f.get_prec());
  i >> temp;
  mpfr_set_f(f.get_mpfr_t(), temp, __gmp_default_rounding_mode);
  mpf_clear(temp);
  return i;
}


/**************** Classes for type conversion ****************/

class __gmpfr_temp
{
private:
  mpfr_srcptr mp;
  bool is_temp;
  mpfr_t temp;

  __gmpfr_temp();
  __gmpfr_temp(const __gmpfr_temp &);
  void operator=(const __gmpfr_temp &);
public:
  __gmpfr_temp(const mpfr_class &f) : mp(f.get_mpfr_t()), is_temp(false) { }
  __gmpfr_temp(const mpfr_class &f, unsigned long int)
    : mp(f.get_mpfr_t()), is_temp(false) { }
  template <class T, class U>
  __gmpfr_temp(const __gmp_expr<T, U> &expr)
  {
    mpfr_init2(temp, expr.get_prec());
    __gmp_set_expr(temp, expr);
    mp = temp;
    is_temp = true;
  }
  template <class T, class U>
  __gmpfr_temp(const __gmp_expr<T, U> &expr, unsigned long int prec)
  {
    mpfr_init2(temp, prec);
    __gmp_set_expr(temp, expr);
    mp = temp;
    is_temp = true;
  }
  ~__gmpfr_temp() { if (is_temp) mpfr_clear(temp); }

  mpfr_srcptr get_mp() const { return mp; }
};


template <>
struct __gmp_resolve_expr<__gmpz_value, __gmpfr_value>
{
  typedef __gmpfr_value value_type;
  typedef __gmpfr_temp temp_type;
};

template <>
struct __gmp_resolve_expr<__gmpfr_value, __gmpz_value>
{
  typedef __gmpfr_value value_type;
  typedef __gmpfr_temp temp_type;
};

template <>
struct __gmp_resolve_expr<__gmpq_value, __gmpfr_value>
{
  typedef __gmpfr_value value_type;
  typedef __gmpfr_temp temp_type;
};

template <>
struct __gmp_resolve_expr<__gmpfr_value, __gmpq_value>
{
  typedef __gmpfr_value value_type;
  typedef __gmpfr_temp temp_type;
};

template <>
struct __gmp_resolve_expr<__gmpf_value, __gmpfr_value>
{
  typedef __gmpfr_value value_type;
  typedef __gmpfr_temp temp_type;
};

template <>
struct __gmp_resolve_expr<__gmpfr_value, __gmpf_value>
{
  typedef __gmpfr_value value_type;
  typedef __gmpfr_temp temp_type;
};

template <>
struct __gmp_resolve_expr<__gmpfr_value, __gmpfr_value>
{
  typedef __gmpfr_value value_type;
  typedef __gmpfr_temp temp_type;
};


/*
template <class T>
inline void __gmp_set_expr(mpz_ptr z, const mpfr_class &f)
{
  mpz_set_fr(z, f.get_mpfr_t());
}

template <class T>
inline void __gmp_set_expr
(mpz_ptr z, const __gmp_expr<__gmpfr_value, T> &expr)
{
  mpfr_class temp(expr);
  mpz_set_fr(z, temp.get_mpfr_t());
}

template <class T>
inline void __gmp_set_expr(mpq_ptr q, const mpfr_class &f)
{
  mpq_set_fr(q, f.get_mpfr_t());
}

template <class T>
inline void __gmp_set_expr
(mpq_ptr q, const __gmp_expr<__gmpfr_value, T> &expr)
{
  mpfr_class temp(expr);
  mpq_set_fr(q, temp.get_mpfr_t());
}

template <class T>
inline void __gmp_set_expr(mpf_ptr f, const mpfr_class &g)
{
  mpf_set_fr(f, g.get_mpfr_t());
}

template <class T>
inline void __gmp_set_expr
(mpf_ptr f, const __gmp_expr<__gmpfr_value, T> &expr)
{
  mpfr_class temp(expr);
  mpf_set_fr(f, temp.get_mpfr_t());
}
*/

template <class T>
inline void __gmp_set_expr(mpfr_ptr f, const mpz_class &z)
{
  mpfr_set_z(f, z.get_mpz_t(), __gmp_default_rounding_mode);
}

template <class T>
inline void __gmp_set_expr(mpfr_ptr f, const mpz_classref &z)
{
  mpfr_set_z(f, z.get_mpz_t(), __gmp_default_rounding_mode);
}

template <class T>
inline void __gmp_set_expr
(mpfr_ptr f, const __gmp_expr<__gmpz_value, T> &expr)
{
  mpz_class temp(expr);
  mpfr_set_z(f, temp.get_mpz_t(), __gmp_default_rounding_mode);
}

template <class T>
inline void __gmp_set_expr(mpfr_ptr f, const mpq_class &q)
{
  mpfr_set_q(f, q.get_mpq_t(), __gmp_default_rounding_mode);
}

template <class T>
inline void __gmp_set_expr
(mpfr_ptr f, const __gmp_expr<__gmpq_value, T> &expr)
{
  mpq_class temp(expr);
  mpfr_set_q(f, temp.get_mpq_t(), __gmp_default_rounding_mode);
}

template <class T>
inline void __gmp_set_expr(mpfr_ptr f, const mpf_class &g)
{
  mpfr_set_f(f, g.get_mpf_t(), __gmp_default_rounding_mode);
}

template <class T>
inline void __gmp_set_expr
(mpfr_ptr f, const __gmp_expr<__gmpf_value, T> &expr)
{
  mpq_class temp(expr);
  mpfr_set_f(f, temp.get_mpf_t(), __gmp_default_rounding_mode);
}

template <>
inline void __gmp_set_expr(mpfr_ptr f, const mpfr_class &g)
{
  mpfr_set(f, g.get_mpfr_t(), __gmp_default_rounding_mode);
}

template <class T>
inline void __gmp_set_expr
(mpfr_ptr f, const __gmp_expr<__gmpfr_value, T> &expr)
{
  expr.eval(f, mpfr_get_prec(f));
}


/**************** Specializations of __gmp_expr ****************/

// unary expressions

template <class Op>
class __gmp_expr<__gmpfr_value, __gmp_unary_expr<mpfr_class, Op> >
{
private:
  __gmp_unary_expr<mpfr_class, Op> expr;
public:
  __gmp_expr(const mpfr_class &val) : expr(val) { }
  void eval(mpfr_ptr f, unsigned long int) const
  { Op::eval(f, expr.val.get_mpfr_t(), __gmp_default_rounding_mode); }
  unsigned long int get_prec() const
  { return mpfr_get_prec(expr.val.get_mpfr_t()); }
};

template <class T, class U, class Op>
class __gmp_expr<__gmpfr_value, __gmp_unary_expr<__gmp_expr<T, U>, Op> >
{
private:
  __gmp_unary_expr<__gmp_expr<T, U>, Op> expr;
public:
  __gmp_expr(const __gmp_expr<T, U> &val) : expr(val) { }
  void eval(mpfr_ptr f, unsigned long int prec) const
  {
    mpfr_class temp(expr.val, prec);
    Op::eval(f, temp.get_mpfr_t(), __gmp_default_rounding_mode);
  }
  unsigned long int get_prec() const { return expr.val.get_prec(); }
};


// binary expressions

template <class Op>
class __gmp_expr
<__gmpfr_value, __gmp_binary_expr<mpfr_class, mpfr_class, Op> >
{
private:
  __gmp_binary_expr<mpfr_class, mpfr_class, Op> expr;
public:
  __gmp_expr(const mpfr_class &val1, const mpfr_class &val2)
    : expr(val1, val2) { }
  void eval(mpfr_ptr f, unsigned long int) const
  { Op::eval(f, expr.val1.get_mpfr_t(), expr.val2.get_mpfr_t(),
	     __gmp_default_rounding_mode); }
  unsigned long int get_prec() const
  {
    unsigned long int prec1 = expr.val1.get_prec(),
      prec2 = expr.val2.get_prec();
    return (prec1 > prec2) ? prec1 : prec2;
  }
};

template <class T, class Op>
class __gmp_expr<__gmpfr_value, __gmp_binary_expr<mpfr_class, T, Op> >
{
private:
  __gmp_binary_expr<mpfr_class, T, Op> expr;
public:
  __gmp_expr(const mpfr_class &val1, T val2) : expr(val1, val2) { }
  void eval(mpfr_ptr f, unsigned long int) const
  { Op::eval(f, expr.val1.get_mpfr_t(), expr.val2,
	     __gmp_default_rounding_mode); }
  unsigned long int get_prec() const
  {
    unsigned long int prec1 = expr.val1.get_prec(),
      prec2 = mpf_get_default_prec();
    return (prec1 > prec2) ? prec1 : prec2;
  }
};

template <class T, class Op>
class __gmp_expr<__gmpfr_value, __gmp_binary_expr<T, mpfr_class, Op> >
{
private:
  __gmp_binary_expr<T, mpfr_class, Op> expr;
public:
  __gmp_expr(T val1, const mpfr_class &val2) : expr(val1, val2) { }
  void eval(mpfr_ptr f, unsigned long int) const
  { Op::eval(f, expr.val1, expr.val2.get_mpfr_t(),
	     __gmp_default_rounding_mode); }
  unsigned long int get_prec() const
  {
    unsigned long int prec1 = mpf_get_default_prec(),
      prec2 = expr.val2.get_prec();
    return (prec1 > prec2) ? prec1 : prec2;
  }
};

template <class T, class U, class Op>
class __gmp_expr
<__gmpfr_value, __gmp_binary_expr<mpfr_class, __gmp_expr<T, U>, Op> >
{
private:
  __gmp_binary_expr<mpfr_class, __gmp_expr<T, U>, Op> expr;
public:
  __gmp_expr(const mpfr_class &val1, const __gmp_expr<T, U> &val2)
    : expr(val1, val2) { }
  void eval(mpfr_ptr f, unsigned long int prec) const
  {
    mpfr_class temp(expr.val2, prec);
    Op::eval(f, expr.val1.get_mpfr_t(), temp.get_mpfr_t(),
	     __gmp_default_rounding_mode);
  }
  unsigned long int get_prec() const
  {
    unsigned long int prec1 = expr.val1.get_prec(),
      prec2 = expr.val2.get_prec();
    return (prec1 > prec2) ? prec1 : prec2;
  }
};

template <class T, class U, class Op>
class __gmp_expr
<__gmpfr_value, __gmp_binary_expr<__gmp_expr<T, U>, mpfr_class, Op> >
{
private:
  __gmp_binary_expr<__gmp_expr<T, U>, mpfr_class, Op> expr;
public:
  __gmp_expr(const __gmp_expr<T, U> &val1, const mpfr_class &val2)
    : expr(val1, val2) { }
  void eval(mpfr_ptr f, unsigned long int prec) const
  {
    mpfr_class temp(expr.val1, prec);
    Op::eval(f, temp.get_mpfr_t(), expr.val2.get_mpfr_t(),
	     __gmp_default_rounding_mode);
  }
  unsigned long int get_prec() const
  {
    unsigned long int prec1 = expr.val1.get_prec(),
      prec2 = expr.val2.get_prec();
    return (prec1 > prec2) ? prec1 : prec2;
  }
};

template <class T, class U, class V, class Op>
class __gmp_expr<__gmpfr_value, __gmp_binary_expr<__gmp_expr<T, U>, V, Op> >
{
private:
  __gmp_binary_expr<__gmp_expr<T, U>, V, Op> expr;
public:
  __gmp_expr(const __gmp_expr<T, U> &val1, V val2) : expr(val1, val2) { }
  void eval(mpfr_ptr f, unsigned long int prec) const
  {
    mpfr_class temp(expr.val1, prec);
    Op::eval(f, temp.get_mpfr_t(), expr.val2, __gmp_default_rounding_mode);
  }
  unsigned long int get_prec() const
  {
    unsigned long int prec1 = expr.val1.get_prec(),
      prec2 = mpf_get_default_prec();
    return (prec1 > prec2) ? prec1 : prec2;
  }
};

template <class T, class U, class V, class Op>
class __gmp_expr<__gmpfr_value, __gmp_binary_expr<T, __gmp_expr<U, V>, Op> >
{
private:
  __gmp_binary_expr<T, __gmp_expr<U, V>, Op> expr;
public:
  __gmp_expr(T val1, const __gmp_expr<U, V> &val2) : expr(val1, val2) { }
  void eval(mpfr_ptr f, unsigned long int prec) const
  {
    mpfr_class temp(expr.val2, prec);
    Op::eval(f, expr.val1, temp.get_mpfr_t(), __gmp_default_rounding_mode);
  }
  unsigned long int get_prec() const
  {
    unsigned long int prec1 = mpf_get_default_prec(),
      prec2 = expr.val2.get_prec();
    return (prec1 > prec2) ? prec1 : prec2;
  }
};

template <class T, class U, class V, class W, class Op>
class __gmp_expr
<__gmpfr_value, __gmp_binary_expr<__gmp_expr<T, U>, __gmp_expr<V, W>, Op> >
{
private:
  __gmp_binary_expr<__gmp_expr<T, U>, __gmp_expr<V, W>, Op> expr;
public:
  __gmp_expr(const __gmp_expr<T, U> &val1, const __gmp_expr<V, W> &val2)
    : expr(val1, val2) { }
  void eval(mpfr_ptr f, unsigned long int prec) const
  {
    mpfr_class temp1(expr.val1, prec), temp2(expr.val2, prec);
    Op::eval(f, temp1.get_mpfr_t(), temp2.get_mpfr_t(),
	     __gmp_default_rounding_mode);
  }
  unsigned long int get_prec() const
  {
    unsigned long int prec1 = expr.val1.get_prec(),
      prec2 = expr.val2.get_prec();
    return (prec1 > prec2) ? prec1 : prec2;
  }
};


/**************** Macros for defining functions ****************/

#define __GMPFRR_DEFINE_COMPOUND_OPERATOR(fun, eval_fun)              \
                                                                      \
template <class T, class U>                                           \
inline mpfr_class & mpfr_class::fun(const __gmp_expr<T, U> &expr)     \
{                                                                     \
  __gmpfr_temp temp(expr, get_prec());                                \
  eval_fun::eval(mp, mp, temp.get_mp(), __gmp_default_rounding_mode); \
  return *this;                                                       \
}

#define __GMPFRN_DEFINE_COMPOUND_OPERATOR(fun, eval_fun)           \
                                                                   \
inline mpfr_class & mpfr_class::fun(bool b)                        \
{                                                                  \
  eval_fun::eval(mp, mp, (unsigned long int) b,                    \
                 __gmp_default_rounding_mode);                     \
  return *this;                                                    \
}                                                                  \
                                                                   \
inline mpfr_class & mpfr_class::fun(signed char c)                 \
{                                                                  \
  eval_fun::eval(mp, mp, (signed long int) c,                      \
                 __gmp_default_rounding_mode);                     \
  return *this;                                                    \
}                                                                  \
                                                                   \
inline mpfr_class & mpfr_class::fun(unsigned char c)               \
{                                                                  \
  eval_fun::eval(mp, mp, (unsigned long int) c,                    \
                 __gmp_default_rounding_mode);                     \
  return *this;                                                    \
}                                                                  \
                                                                   \
inline mpfr_class & mpfr_class::fun(signed int i)                  \
{                                                                  \
  eval_fun::eval(mp, mp, (signed long int) i,                      \
                 __gmp_default_rounding_mode);                     \
  return *this;                                                    \
}                                                                  \
                                                                   \
inline mpfr_class & mpfr_class::fun(unsigned int i)                \
{                                                                  \
  eval_fun::eval(mp, mp, (unsigned long int) i,                    \
                 __gmp_default_rounding_mode);                     \
  return *this;                                                    \
}                                                                  \
                                                                   \
inline mpfr_class & mpfr_class::fun(signed short int s)            \
{                                                                  \
  eval_fun::eval(mp, mp, (signed long int) s,                      \
                 __gmp_default_rounding_mode);                     \
  return *this;                                                    \
}                                                                  \
                                                                   \
inline mpfr_class & mpfr_class::fun(unsigned short int s)          \
{                                                                  \
  eval_fun::eval(mp, mp, (unsigned long int) s,                    \
                 __gmp_default_rounding_mode);                     \
  return *this;                                                    \
}                                                                  \
                                                                   \
inline mpfr_class & mpfr_class::fun(signed long int l)             \
{                                                                  \
  eval_fun::eval(mp, mp, l, __gmp_default_rounding_mode);          \
  return *this;                                                    \
}                                                                  \
                                                                   \
inline mpfr_class & mpfr_class::fun(unsigned long int l)           \
{                                                                  \
  eval_fun::eval(mp, mp, l, __gmp_default_rounding_mode);          \
  return *this;                                                    \
}                                                                  \
                                                                   \
inline mpfr_class & mpfr_class::fun(float f)                       \
{                                                                  \
  eval_fun::eval(mp, mp, (double) f, __gmp_default_rounding_mode); \
  return *this;                                                    \
}                                                                  \
                                                                   \
inline mpfr_class & mpfr_class::fun(double d)                      \
{                                                                  \
  eval_fun::eval(mp, mp, d, __gmp_default_rounding_mode);          \
  return *this;                                                    \
}                                                                  \
                                                                   \
/*                                                                 \
inline mpfr_class & mpfr_class::fun(long double ld)                \
{                                                                  \
  eval_fun::eval(mp, mp, ld, __gmp_default_rounding_mode);         \
  return *this;                                                    \
} */

#define __GMPFR_DEFINE_COMPOUND_OPERATOR(fun, eval_fun) \
__GMPFRR_DEFINE_COMPOUND_OPERATOR(fun, eval_fun)        \
__GMPFRN_DEFINE_COMPOUND_OPERATOR(fun, eval_fun)

#define __GMPFR_DEFINE_COMPOUND_OPERATOR_UI(fun, eval_fun) \
                                                           \
inline mpfr_class & mpfr_class::fun(unsigned long int l)   \
{                                                          \
  eval_fun::eval(mp, mp, l, __gmp_default_rounding_mode);  \
  return *this;                                            \
}

#define __GMPFR_DEFINE_INCREMENT_OPERATOR(fun, eval_fun) \
                                                         \
inline mpfr_class & mpfr_class::fun()                    \
{                                                        \
  eval_fun::eval(mp, mp, __gmp_default_rounding_mode);   \
  return *this;                                          \
}                                                        \
                                                         \
inline mpfr_class mpfr_class::fun(int)                   \
{                                                        \
  mpfr_class temp(*this);                                \
  eval_fun::eval(mp, mp, __gmp_default_rounding_mode);   \
  return temp;                                           \
}


/**************** Arithmetic operators and functions ****************/

__GMPFR_DEFINE_COMPOUND_OPERATOR(operator+=, __gmp_binary_plus)
__GMPFR_DEFINE_COMPOUND_OPERATOR(operator-=, __gmp_binary_minus)
__GMPFR_DEFINE_COMPOUND_OPERATOR(operator*=, __gmp_binary_multiplies)
__GMPFR_DEFINE_COMPOUND_OPERATOR(operator/=, __gmp_binary_divides)

__GMPFR_DEFINE_COMPOUND_OPERATOR_UI(operator<<=, __gmp_binary_lshift)
__GMPFR_DEFINE_COMPOUND_OPERATOR_UI(operator>>=, __gmp_binary_rshift)

__GMPFR_DEFINE_INCREMENT_OPERATOR(operator++, __gmp_unary_increment)
__GMPFR_DEFINE_INCREMENT_OPERATOR(operator--, __gmp_unary_decrement)


/**************** #undef all private macros ****************/

#undef __GMPFRR_DECLARE_COMPOUND_OPERATOR
#undef __GMPFRN_DECLARE_COMPOUND_OPERATOR
#undef __GMPFR_DECLARE_COMPOUND_OPERATOR
#undef __GMPFR_DECLARE_COMPOUND_OPERATOR_UI
#undef __GMPFR_DECLARE_INCREMENT_OPERATOR

#undef __GMPFRR_DEFINE_COMPOUND_OPERATOR
#undef __GMPFRN_DEFINE_COMPOUND_OPERATOR
#undef __GMPFR_DEFINE_COMPOUND_OPERATOR
#undef __GMPFR_DEFINE_COMPOUND_OPERATOR_UI
#undef __GMPFR_DEFINE_INCREMENT_OPERATOR


#endif /* __GMPFR_PLUSPLUS__ */