-
Notifications
You must be signed in to change notification settings - Fork 1
/
mpreal.h
3263 lines (2705 loc) · 117 KB
/
mpreal.h
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
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
MPFR C++: Multi-precision floating point number class for C++.
Based on MPFR library: http://mpfr.org
Project homepage: http://www.holoborodko.com/pavel/mpfr
Contact e-mail: pavel@holoborodko.com
Copyright (c) 2008-2020 Pavel Holoborodko
Contributors:
Dmitriy Gubanov, Konstantin Holoborodko, Brian Gladman,
Helmut Jarausch, Fokko Beekhof, Ulrich Mutze, Heinz van Saanen,
Pere Constans, Peter van Hoof, Gael Guennebaud, Tsai Chia Cheng,
Alexei Zubanov, Jauhien Piatlicki, Victor Berger, John Westwood,
Petr Aleksandrov, Orion Poplawski, Charles Karney, Arash Partow,
Rodney James, Jorge Leitao, Jerome Benoit, Michal Maly.
Licensing:
(A) MPFR C++ is under GNU General Public License ("GPL").
(B) Non-free licenses may also be purchased from the author, for users who
do not want their programs protected by the GPL.
The non-free licenses are for users that wish to use MPFR C++ in
their products but are unwilling to release their software
under the GPL (which would require them to release source code
and allow free redistribution).
Such users can purchase an unlimited-use license from the author.
Contact us for more details.
GNU General Public License ("GPL") copyright permissions statement:
**************************************************************************
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __MPREAL_H__
#define __MPREAL_H__
#include <string>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <cfloat>
#include <cmath>
#include <cstring>
#include <limits>
#include <complex>
#include <algorithm>
#include <stdint.h>
// Options
#define MPREAL_HAVE_MSVC_DEBUGVIEW // Enable Debugger Visualizer for "Debug" builds in MSVC.
#define MPREAL_HAVE_DYNAMIC_STD_NUMERIC_LIMITS // Enable extended std::numeric_limits<mpfr::mpreal> specialization.
// Meaning that "digits", "round_style" and similar members are defined as functions, not constants.
// See std::numeric_limits<mpfr::mpreal> at the end of the file for more information.
// Library version
#define MPREAL_VERSION_MAJOR 3
#define MPREAL_VERSION_MINOR 6
#define MPREAL_VERSION_PATCHLEVEL 8
#define MPREAL_VERSION_STRING "3.6.8"
// Detect compiler using signatures from http://predef.sourceforge.net/
#if defined(__GNUC__) && defined(__INTEL_COMPILER)
#define IsInf(x) isinf(x) // Intel ICC compiler on Linux
#elif defined(_MSC_VER) // Microsoft Visual C++
#define IsInf(x) (!_finite(x))
#else
#define IsInf(x) std::isinf(x) // GNU C/C++ (and/or other compilers), just hope for C99 conformance
#endif
// A Clang feature extension to determine compiler features.
#ifndef __has_feature
#define __has_feature(x) 0
#endif
// Detect support for r-value references (move semantic).
// Move semantic should be enabled with great care in multi-threading environments,
// especially if MPFR uses custom memory allocators.
// Everything should be thread-safe and support passing ownership over thread boundary.
#if (__has_feature(cxx_rvalue_references) || \
defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L || \
(defined(_MSC_VER) && _MSC_VER >= 1600) && !defined(MPREAL_DISABLE_MOVE_SEMANTIC))
#define MPREAL_HAVE_MOVE_SUPPORT
// Use fields in mpfr_t structure to check if it was initialized / set dummy initialization
#define mpfr_is_initialized(x) (0 != (x)->_mpfr_d)
#define mpfr_set_uninitialized(x) ((x)->_mpfr_d = 0 )
#endif
// Detect support for explicit converters.
#if (__has_feature(cxx_explicit_conversions) || \
(defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC_MINOR >= 5) || __cplusplus >= 201103L || \
(defined(_MSC_VER) && _MSC_VER >= 1800) || \
(defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1300))
#define MPREAL_HAVE_EXPLICIT_CONVERTERS
#endif
#define MPFR_USE_INTMAX_T // Enable 64-bit integer types - should be defined before mpfr.h
#if defined(MPREAL_HAVE_MSVC_DEBUGVIEW) && defined(_MSC_VER) && defined(_DEBUG)
#define MPREAL_MSVC_DEBUGVIEW_CODE DebugView = toString();
#define MPREAL_MSVC_DEBUGVIEW_DATA std::string DebugView;
#else
#define MPREAL_MSVC_DEBUGVIEW_CODE
#define MPREAL_MSVC_DEBUGVIEW_DATA
#endif
#include <mpfr.h>
#if (MPFR_VERSION < MPFR_VERSION_NUM(3,0,0))
#include <cstdlib> // Needed for random()
#endif
// Less important options
#define MPREAL_DOUBLE_BITS_OVERFLOW -1 // Triggers overflow exception during conversion to double if mpreal
// cannot fit in MPREAL_DOUBLE_BITS_OVERFLOW bits
// = -1 disables overflow checks (default)
// Fast replacement for mpfr_set_zero(x, +1):
// (a) uses low-level data members, might not be forward compatible
// (b) sign is not set, add (x)->_mpfr_sign = 1;
#define mpfr_set_zero_fast(x) ((x)->_mpfr_exp = __MPFR_EXP_ZERO)
#if defined(__GNUC__)
#define MPREAL_PERMISSIVE_EXPR __extension__
#else
#define MPREAL_PERMISSIVE_EXPR
#endif
namespace mpfr {
class mpreal {
private:
mpfr_t mp;
public:
// Get default rounding mode & precision
inline static mp_rnd_t get_default_rnd() { return (mp_rnd_t)(mpfr_get_default_rounding_mode()); }
inline static mp_prec_t get_default_prec() { return (mpfr_get_default_prec)(); }
// Constructors && type conversions
mpreal();
mpreal(const mpreal& u);
mpreal(const mpf_t u);
mpreal(const mpz_t u, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t mode = mpreal::get_default_rnd());
mpreal(const mpq_t u, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t mode = mpreal::get_default_rnd());
mpreal(const double u, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t mode = mpreal::get_default_rnd());
mpreal(const long double u, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t mode = mpreal::get_default_rnd());
mpreal(const unsigned long long int u, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t mode = mpreal::get_default_rnd());
mpreal(const long long int u, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t mode = mpreal::get_default_rnd());
mpreal(const unsigned long int u, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t mode = mpreal::get_default_rnd());
mpreal(const unsigned int u, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t mode = mpreal::get_default_rnd());
mpreal(const long int u, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t mode = mpreal::get_default_rnd());
mpreal(const int u, mp_prec_t prec = mpreal::get_default_prec(), mp_rnd_t mode = mpreal::get_default_rnd());
// Construct mpreal from mpfr_t structure.
// shared = true allows to avoid deep copy, so that mpreal and 'u' share the same data & pointers.
mpreal(const mpfr_t u, bool shared = false);
mpreal(const char* s, mp_prec_t prec = mpreal::get_default_prec(), int base = 10, mp_rnd_t mode = mpreal::get_default_rnd());
mpreal(const std::string& s, mp_prec_t prec = mpreal::get_default_prec(), int base = 10, mp_rnd_t mode = mpreal::get_default_rnd());
~mpreal();
#ifdef MPREAL_HAVE_MOVE_SUPPORT
mpreal& operator=(mpreal&& v);
mpreal(mpreal&& u);
#endif
// Operations
// =
// +, -, *, /, ++, --, <<, >>
// *=, +=, -=, /=,
// <, >, ==, <=, >=
// =
mpreal& operator=(const mpreal& v);
mpreal& operator=(const mpf_t v);
mpreal& operator=(const mpz_t v);
mpreal& operator=(const mpq_t v);
mpreal& operator=(const long double v);
mpreal& operator=(const double v);
mpreal& operator=(const unsigned long int v);
mpreal& operator=(const unsigned long long int v);
mpreal& operator=(const long long int v);
mpreal& operator=(const unsigned int v);
mpreal& operator=(const long int v);
mpreal& operator=(const int v);
mpreal& operator=(const char* s);
mpreal& operator=(const std::string& s);
template <typename real_t> mpreal& operator= (const std::complex<real_t>& z);
// +
mpreal& operator+=(const mpreal& v);
mpreal& operator+=(const mpf_t v);
mpreal& operator+=(const mpz_t v);
mpreal& operator+=(const mpq_t v);
mpreal& operator+=(const long double u);
mpreal& operator+=(const double u);
mpreal& operator+=(const unsigned long int u);
mpreal& operator+=(const unsigned int u);
mpreal& operator+=(const long int u);
mpreal& operator+=(const int u);
mpreal& operator+=(const long long int u);
mpreal& operator+=(const unsigned long long int u);
mpreal& operator-=(const long long int u);
mpreal& operator-=(const unsigned long long int u);
mpreal& operator*=(const long long int u);
mpreal& operator*=(const unsigned long long int u);
mpreal& operator/=(const long long int u);
mpreal& operator/=(const unsigned long long int u);
const mpreal operator+() const;
mpreal& operator++ ();
const mpreal operator++ (int);
// -
mpreal& operator-=(const mpreal& v);
mpreal& operator-=(const mpz_t v);
mpreal& operator-=(const mpq_t v);
mpreal& operator-=(const long double u);
mpreal& operator-=(const double u);
mpreal& operator-=(const unsigned long int u);
mpreal& operator-=(const unsigned int u);
mpreal& operator-=(const long int u);
mpreal& operator-=(const int u);
const mpreal operator-() const;
friend const mpreal operator-(const unsigned long int b, const mpreal& a);
friend const mpreal operator-(const unsigned int b, const mpreal& a);
friend const mpreal operator-(const long int b, const mpreal& a);
friend const mpreal operator-(const int b, const mpreal& a);
friend const mpreal operator-(const double b, const mpreal& a);
mpreal& operator-- ();
const mpreal operator-- (int);
// *
mpreal& operator*=(const mpreal& v);
mpreal& operator*=(const mpz_t v);
mpreal& operator*=(const mpq_t v);
mpreal& operator*=(const long double v);
mpreal& operator*=(const double v);
mpreal& operator*=(const unsigned long int v);
mpreal& operator*=(const unsigned int v);
mpreal& operator*=(const long int v);
mpreal& operator*=(const int v);
// /
mpreal& operator/=(const mpreal& v);
mpreal& operator/=(const mpz_t v);
mpreal& operator/=(const mpq_t v);
mpreal& operator/=(const long double v);
mpreal& operator/=(const double v);
mpreal& operator/=(const unsigned long int v);
mpreal& operator/=(const unsigned int v);
mpreal& operator/=(const long int v);
mpreal& operator/=(const int v);
friend const mpreal operator/(const unsigned long int b, const mpreal& a);
friend const mpreal operator/(const unsigned int b, const mpreal& a);
friend const mpreal operator/(const long int b, const mpreal& a);
friend const mpreal operator/(const int b, const mpreal& a);
friend const mpreal operator/(const double b, const mpreal& a);
//<<= Fast Multiplication by 2^u
mpreal& operator<<=(const unsigned long int u);
mpreal& operator<<=(const unsigned int u);
mpreal& operator<<=(const long int u);
mpreal& operator<<=(const int u);
//>>= Fast Division by 2^u
mpreal& operator>>=(const unsigned long int u);
mpreal& operator>>=(const unsigned int u);
mpreal& operator>>=(const long int u);
mpreal& operator>>=(const int u);
// Type Conversion operators
bool toBool ( ) const;
long toLong (mp_rnd_t mode = GMP_RNDZ) const;
unsigned long toULong (mp_rnd_t mode = GMP_RNDZ) const;
long long toLLong (mp_rnd_t mode = GMP_RNDZ) const;
unsigned long long toULLong (mp_rnd_t mode = GMP_RNDZ) const;
float toFloat (mp_rnd_t mode = GMP_RNDN) const;
double toDouble (mp_rnd_t mode = GMP_RNDN) const;
long double toLDouble (mp_rnd_t mode = GMP_RNDN) const;
#if defined (MPREAL_HAVE_EXPLICIT_CONVERTERS)
explicit operator bool () const { return toBool(); }
explicit operator signed char () const { return (signed char)toLong(); }
explicit operator unsigned char () const { return (unsigned char)toULong(); }
explicit operator short () const { return (short)toLong(); }
explicit operator unsigned short () const { return (unsigned short)toULong();}
explicit operator int () const { return (int)toLong(); }
explicit operator unsigned int () const { return (unsigned int)toULong(); }
explicit operator long () const { return toLong(); }
explicit operator unsigned long () const { return toULong(); }
explicit operator long long () const { return toLLong(); }
explicit operator unsigned long long () const { return toULLong(); }
explicit operator float () const { return toFloat(); }
explicit operator double () const { return toDouble(); }
explicit operator long double () const { return toLDouble(); }
#endif
// Get raw pointers so that mpreal can be directly used in raw mpfr_* functions
::mpfr_ptr mpfr_ptr();
::mpfr_srcptr mpfr_ptr() const;
::mpfr_srcptr mpfr_srcptr() const;
// Convert mpreal to string with n significant digits in base b
// n = -1 -> convert with the maximum available digits
std::string toString(int n = -1, int b = 10, mp_rnd_t mode = mpreal::get_default_rnd()) const;
#if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
std::string toString(const std::string& format) const;
#endif
std::ostream& output(std::ostream& os) const;
// Math Functions
friend const mpreal sqr (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal sqrt(const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal sqrt(const unsigned long int v, mp_rnd_t rnd_mode);
friend const mpreal cbrt(const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal root(const mpreal& v, unsigned long int k, mp_rnd_t rnd_mode);
friend const mpreal pow (const mpreal& a, const mpreal& b, mp_rnd_t rnd_mode);
friend const mpreal pow (const mpreal& a, const mpz_t b, mp_rnd_t rnd_mode);
friend const mpreal pow (const mpreal& a, const unsigned long int b, mp_rnd_t rnd_mode);
friend const mpreal pow (const mpreal& a, const long int b, mp_rnd_t rnd_mode);
friend const mpreal pow (const unsigned long int a, const mpreal& b, mp_rnd_t rnd_mode);
friend const mpreal pow (const unsigned long int a, const unsigned long int b, mp_rnd_t rnd_mode);
friend const mpreal fabs(const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal abs(const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal dim(const mpreal& a, const mpreal& b, mp_rnd_t rnd_mode);
friend inline const mpreal mul_2ui(const mpreal& v, unsigned long int k, mp_rnd_t rnd_mode);
friend inline const mpreal mul_2si(const mpreal& v, long int k, mp_rnd_t rnd_mode);
friend inline const mpreal div_2ui(const mpreal& v, unsigned long int k, mp_rnd_t rnd_mode);
friend inline const mpreal div_2si(const mpreal& v, long int k, mp_rnd_t rnd_mode);
friend int cmpabs(const mpreal& a,const mpreal& b);
friend const mpreal log (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal log2 (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal logb (const mpreal& v, mp_rnd_t rnd_mode);
friend mp_exp_t ilogb(const mpreal& v);
friend const mpreal log10(const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal exp (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal exp2 (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal exp10(const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal log1p(const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal expm1(const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal nextpow2(const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal cos(const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal sin(const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal tan(const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal sec(const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal csc(const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal cot(const mpreal& v, mp_rnd_t rnd_mode);
friend int sin_cos(mpreal& s, mpreal& c, const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal acos (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal asin (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal atan (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal atan2 (const mpreal& y, const mpreal& x, mp_rnd_t rnd_mode);
friend const mpreal acot (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal asec (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal acsc (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal cosh (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal sinh (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal tanh (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal sech (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal csch (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal coth (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal acosh (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal asinh (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal atanh (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal acoth (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal asech (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal acsch (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal hypot (const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode);
friend const mpreal fac_ui (unsigned long int v, mp_prec_t prec, mp_rnd_t rnd_mode);
friend const mpreal eint (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal gamma (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal tgamma (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal lngamma (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal lgamma (const mpreal& v, int *signp, mp_rnd_t rnd_mode);
friend const mpreal zeta (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal erf (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal erfc (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal besselj0 (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal besselj1 (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal besseljn (long n, const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal bessely0 (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal bessely1 (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal besselyn (long n, const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal fma (const mpreal& v1, const mpreal& v2, const mpreal& v3, mp_rnd_t rnd_mode);
friend const mpreal fms (const mpreal& v1, const mpreal& v2, const mpreal& v3, mp_rnd_t rnd_mode);
friend const mpreal agm (const mpreal& v1, const mpreal& v2, mp_rnd_t rnd_mode);
friend const mpreal sum (const mpreal tab[], const unsigned long int n, int& status, mp_rnd_t rnd_mode);
friend int sgn (const mpreal& v);
// MPFR 2.4.0 Specifics
#if (MPFR_VERSION >= MPFR_VERSION_NUM(2,4,0))
friend int sinh_cosh (mpreal& s, mpreal& c, const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal li2 (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal fmod (const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode);
friend const mpreal rec_sqrt (const mpreal& v, mp_rnd_t rnd_mode);
// MATLAB's semantic equivalents
friend const mpreal rem (const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode); // Remainder after division
friend const mpreal mod (const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode); // Modulus after division
#endif
#if (MPFR_VERSION >= MPFR_VERSION_NUM(3,0,0))
friend const mpreal digamma (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal ai (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal urandom (gmp_randstate_t& state, mp_rnd_t rnd_mode); // use gmp_randinit_default() to init state, gmp_randclear() to clear
#endif
#if (MPFR_VERSION >= MPFR_VERSION_NUM(3,1,0))
friend const mpreal grandom (gmp_randstate_t& state, mp_rnd_t rnd_mode); // use gmp_randinit_default() to init state, gmp_randclear() to clear
friend const mpreal grandom (unsigned int seed);
#endif
// Uniformly distributed random number generation in [0,1] using
// Mersenne-Twister algorithm by default.
// Use parameter to setup seed, e.g.: random((unsigned)time(NULL))
// Check urandom() for more precise control.
friend const mpreal random(unsigned int seed);
// Splits mpreal value into fractional and integer parts.
// Returns fractional part and stores integer part in n.
friend const mpreal modf(const mpreal& v, mpreal& n);
// Constants
// don't forget to call mpfr_free_cache() for every thread where you are using const-functions
friend const mpreal const_log2 (mp_prec_t prec, mp_rnd_t rnd_mode);
friend const mpreal const_pi (mp_prec_t prec, mp_rnd_t rnd_mode);
friend const mpreal const_euler (mp_prec_t prec, mp_rnd_t rnd_mode);
friend const mpreal const_catalan (mp_prec_t prec, mp_rnd_t rnd_mode);
// returns +inf iff sign>=0 otherwise -inf
friend const mpreal const_infinity(int sign, mp_prec_t prec);
// Output/ Input
friend std::ostream& operator<<(std::ostream& os, const mpreal& v);
friend std::istream& operator>>(std::istream& is, mpreal& v);
// Integer Related Functions
friend const mpreal rint (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal ceil (const mpreal& v);
friend const mpreal floor(const mpreal& v);
friend const mpreal round(const mpreal& v);
friend long lround(const mpreal& v);
friend long long llround(const mpreal& v);
friend const mpreal trunc(const mpreal& v);
friend const mpreal rint_ceil (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal rint_floor (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal rint_round (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal rint_trunc (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal frac (const mpreal& v, mp_rnd_t rnd_mode);
friend const mpreal remainder (const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode);
friend const mpreal remquo (const mpreal& x, const mpreal& y, int* q, mp_rnd_t rnd_mode);
// Miscellaneous Functions
friend const mpreal nexttoward (const mpreal& x, const mpreal& y);
friend const mpreal nextabove (const mpreal& x);
friend const mpreal nextbelow (const mpreal& x);
// use gmp_randinit_default() to init state, gmp_randclear() to clear
friend const mpreal urandomb (gmp_randstate_t& state);
// MPFR < 2.4.2 Specifics
#if (MPFR_VERSION <= MPFR_VERSION_NUM(2,4,2))
friend const mpreal random2 (mp_size_t size, mp_exp_t exp);
#endif
// Instance Checkers
friend bool isnan (const mpreal& v);
friend bool isinf (const mpreal& v);
friend bool isfinite (const mpreal& v);
friend bool isnum (const mpreal& v);
friend bool iszero (const mpreal& v);
friend bool isint (const mpreal& v);
#if (MPFR_VERSION >= MPFR_VERSION_NUM(3,0,0))
friend bool isregular(const mpreal& v);
#endif
// Set/Get instance properties
inline mp_prec_t get_prec() const;
inline void set_prec(mp_prec_t prec, mp_rnd_t rnd_mode = get_default_rnd()); // Change precision with rounding mode
// Aliases for get_prec(), set_prec() - needed for compatibility with std::complex<mpreal> interface
inline mpreal& setPrecision(int Precision, mp_rnd_t RoundingMode = get_default_rnd());
inline int getPrecision() const;
// Set mpreal to +/- inf, NaN, +/-0
mpreal& setInf (int Sign = +1);
mpreal& setNan ();
mpreal& setZero (int Sign = +1);
mpreal& setSign (int Sign, mp_rnd_t RoundingMode = get_default_rnd());
//Exponent
mp_exp_t get_exp() const;
int set_exp(mp_exp_t e);
int check_range (int t, mp_rnd_t rnd_mode = get_default_rnd());
int subnormalize (int t, mp_rnd_t rnd_mode = get_default_rnd());
// Inexact conversion from float
inline bool fits_in_bits(double x, int n);
// Set/Get global properties
static void set_default_prec(mp_prec_t prec);
static void set_default_rnd(mp_rnd_t rnd_mode);
static mp_exp_t get_emin (void);
static mp_exp_t get_emax (void);
static mp_exp_t get_emin_min (void);
static mp_exp_t get_emin_max (void);
static mp_exp_t get_emax_min (void);
static mp_exp_t get_emax_max (void);
static int set_emin (mp_exp_t exp);
static int set_emax (mp_exp_t exp);
// Efficient swapping of two mpreal values - needed for std algorithms
friend void swap(mpreal& x, mpreal& y);
friend const mpreal fmax(const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode);
friend const mpreal fmin(const mpreal& x, const mpreal& y, mp_rnd_t rnd_mode);
private:
// Human friendly Debug Preview in Visual Studio.
// Put one of these lines:
//
// mpfr::mpreal=<DebugView> ; Show value only
// mpfr::mpreal=<DebugView>, <mp[0]._mpfr_prec,u>bits ; Show value & precision
//
// at the beginning of
// [Visual Studio Installation Folder]\Common7\Packages\Debugger\autoexp.dat
MPREAL_MSVC_DEBUGVIEW_DATA
// "Smart" resources deallocation. Checks if instance initialized before deletion.
void clear(::mpfr_ptr);
};
//////////////////////////////////////////////////////////////////////////
// Exceptions
class conversion_overflow : public std::exception {
public:
std::string why() { return "inexact conversion from floating point"; }
};
//////////////////////////////////////////////////////////////////////////
// Constructors & converters
// Default constructor: creates mp number and initializes it to 0.
inline mpreal::mpreal()
{
mpfr_init2(mpfr_ptr(), mpreal::get_default_prec());
mpfr_set_zero_fast(mpfr_ptr());
MPREAL_MSVC_DEBUGVIEW_CODE;
}
inline mpreal::mpreal(const mpreal& u)
{
mpfr_init2(mpfr_ptr(),mpfr_get_prec(u.mpfr_srcptr()));
mpfr_set (mpfr_ptr(),u.mpfr_srcptr(),mpreal::get_default_rnd());
MPREAL_MSVC_DEBUGVIEW_CODE;
}
#ifdef MPREAL_HAVE_MOVE_SUPPORT
inline mpreal::mpreal(mpreal&& other)
{
mpfr_set_uninitialized(mpfr_ptr()); // make sure "other" holds null-pointer (in uninitialized state)
mpfr_swap(mpfr_ptr(), other.mpfr_ptr());
MPREAL_MSVC_DEBUGVIEW_CODE;
}
inline mpreal& mpreal::operator=(mpreal&& other)
{
if (this != &other)
{
mpfr_swap(mpfr_ptr(), other.mpfr_ptr()); // destructor for "other" will be called just afterwards
MPREAL_MSVC_DEBUGVIEW_CODE;
}
return *this;
}
#endif
inline mpreal::mpreal(const mpfr_t u, bool shared)
{
if(shared)
{
std::memcpy(mpfr_ptr(), u, sizeof(mpfr_t));
}
else
{
mpfr_init2(mpfr_ptr(), mpfr_get_prec(u));
mpfr_set (mpfr_ptr(), u, mpreal::get_default_rnd());
}
MPREAL_MSVC_DEBUGVIEW_CODE;
}
inline mpreal::mpreal(const mpf_t u)
{
mpfr_init2(mpfr_ptr(),(mp_prec_t) mpf_get_prec(u)); // (gmp: mp_bitcnt_t) unsigned long -> long (mpfr: mp_prec_t)
mpfr_set_f(mpfr_ptr(),u,mpreal::get_default_rnd());
MPREAL_MSVC_DEBUGVIEW_CODE;
}
inline mpreal::mpreal(const mpz_t u, mp_prec_t prec, mp_rnd_t mode)
{
mpfr_init2(mpfr_ptr(), prec);
mpfr_set_z(mpfr_ptr(), u, mode);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
inline mpreal::mpreal(const mpq_t u, mp_prec_t prec, mp_rnd_t mode)
{
mpfr_init2(mpfr_ptr(), prec);
mpfr_set_q(mpfr_ptr(), u, mode);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
inline mpreal::mpreal(const double u, mp_prec_t prec, mp_rnd_t mode)
{
mpfr_init2(mpfr_ptr(), prec);
#if (MPREAL_DOUBLE_BITS_OVERFLOW > -1)
if(fits_in_bits(u, MPREAL_DOUBLE_BITS_OVERFLOW))
{
mpfr_set_d(mpfr_ptr(), u, mode);
}else
throw conversion_overflow();
#else
mpfr_set_d(mpfr_ptr(), u, mode);
#endif
MPREAL_MSVC_DEBUGVIEW_CODE;
}
inline mpreal::mpreal(const long double u, mp_prec_t prec, mp_rnd_t mode)
{
mpfr_init2 (mpfr_ptr(), prec);
mpfr_set_ld(mpfr_ptr(), u, mode);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
inline mpreal::mpreal(const unsigned long long int u, mp_prec_t prec, mp_rnd_t mode)
{
mpfr_init2 (mpfr_ptr(), prec);
mpfr_set_uj(mpfr_ptr(), u, mode);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
inline mpreal::mpreal(const long long int u, mp_prec_t prec, mp_rnd_t mode)
{
mpfr_init2 (mpfr_ptr(), prec);
mpfr_set_sj(mpfr_ptr(), u, mode);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
inline mpreal::mpreal(const unsigned long int u, mp_prec_t prec, mp_rnd_t mode)
{
mpfr_init2 (mpfr_ptr(), prec);
mpfr_set_ui(mpfr_ptr(), u, mode);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
inline mpreal::mpreal(const unsigned int u, mp_prec_t prec, mp_rnd_t mode)
{
mpfr_init2 (mpfr_ptr(), prec);
mpfr_set_ui(mpfr_ptr(), u, mode);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
inline mpreal::mpreal(const long int u, mp_prec_t prec, mp_rnd_t mode)
{
mpfr_init2 (mpfr_ptr(), prec);
mpfr_set_si(mpfr_ptr(), u, mode);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
inline mpreal::mpreal(const int u, mp_prec_t prec, mp_rnd_t mode)
{
mpfr_init2 (mpfr_ptr(), prec);
mpfr_set_si(mpfr_ptr(), u, mode);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
inline mpreal::mpreal(const char* s, mp_prec_t prec, int base, mp_rnd_t mode)
{
mpfr_init2 (mpfr_ptr(), prec);
mpfr_set_str(mpfr_ptr(), s, base, mode);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
inline mpreal::mpreal(const std::string& s, mp_prec_t prec, int base, mp_rnd_t mode)
{
mpfr_init2 (mpfr_ptr(), prec);
mpfr_set_str(mpfr_ptr(), s.c_str(), base, mode);
MPREAL_MSVC_DEBUGVIEW_CODE;
}
inline void mpreal::clear(::mpfr_ptr x)
{
#ifdef MPREAL_HAVE_MOVE_SUPPORT
if(mpfr_is_initialized(x))
#endif
mpfr_clear(x);
}
inline mpreal::~mpreal()
{
clear(mpfr_ptr());
}
// internal namespace needed for template magic
namespace internal{
// Use SFINAE to restrict arithmetic operations instantiation only for numeric types
// This is needed for smooth integration with libraries based on expression templates, like Eigen.
// TODO: Do the same for boolean operators.
template <typename ArgumentType> struct result_type {};
template <> struct result_type<mpreal> {typedef mpreal type;};
template <> struct result_type<mpz_t> {typedef mpreal type;};
template <> struct result_type<mpq_t> {typedef mpreal type;};
template <> struct result_type<long double> {typedef mpreal type;};
template <> struct result_type<double> {typedef mpreal type;};
template <> struct result_type<unsigned long int> {typedef mpreal type;};
template <> struct result_type<unsigned int> {typedef mpreal type;};
template <> struct result_type<long int> {typedef mpreal type;};
template <> struct result_type<int> {typedef mpreal type;};
template <> struct result_type<long long> {typedef mpreal type;};
template <> struct result_type<unsigned long long> {typedef mpreal type;};
}
// + Addition
template <typename Rhs>
inline const typename internal::result_type<Rhs>::type
operator+(const mpreal& lhs, const Rhs& rhs){ return mpreal(lhs) += rhs; }
template <typename Lhs>
inline const typename internal::result_type<Lhs>::type
operator+(const Lhs& lhs, const mpreal& rhs){ return mpreal(rhs) += lhs; }
// - Subtraction
template <typename Rhs>
inline const typename internal::result_type<Rhs>::type
operator-(const mpreal& lhs, const Rhs& rhs){ return mpreal(lhs) -= rhs; }
template <typename Lhs>
inline const typename internal::result_type<Lhs>::type
operator-(const Lhs& lhs, const mpreal& rhs){ return mpreal(lhs) -= rhs; }
// * Multiplication
template <typename Rhs>
inline const typename internal::result_type<Rhs>::type
operator*(const mpreal& lhs, const Rhs& rhs){ return mpreal(lhs) *= rhs; }
template <typename Lhs>
inline const typename internal::result_type<Lhs>::type
operator*(const Lhs& lhs, const mpreal& rhs){ return mpreal(rhs) *= lhs; }
// / Division
template <typename Rhs>
inline const typename internal::result_type<Rhs>::type
operator/(const mpreal& lhs, const Rhs& rhs){ return mpreal(lhs) /= rhs; }
template <typename Lhs>
inline const typename internal::result_type<Lhs>::type
operator/(const Lhs& lhs, const mpreal& rhs){ return mpreal(lhs) /= rhs; }
//////////////////////////////////////////////////////////////////////////
// sqrt
const mpreal sqrt(const unsigned int v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal sqrt(const long int v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal sqrt(const int v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal sqrt(const long double v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal sqrt(const double v, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
// abs
inline const mpreal abs(const mpreal& x, mp_rnd_t r = mpreal::get_default_rnd());
//////////////////////////////////////////////////////////////////////////
// pow
const mpreal pow(const mpreal& a, const unsigned int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const mpreal& a, const int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const mpreal& a, const long double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const mpreal& a, const double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const unsigned int a, const mpreal& b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const long int a, const mpreal& b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const int a, const mpreal& b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const long double a, const mpreal& b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const double a, const mpreal& b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const unsigned long int a, const unsigned int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const unsigned long int a, const long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const unsigned long int a, const int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const unsigned long int a, const long double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const unsigned long int a, const double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const unsigned int a, const unsigned long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const unsigned int a, const unsigned int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const unsigned int a, const long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const unsigned int a, const int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const unsigned int a, const long double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const unsigned int a, const double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const long int a, const unsigned long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const long int a, const unsigned int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const long int a, const long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const long int a, const int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const long int a, const long double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const long int a, const double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const int a, const unsigned long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const int a, const unsigned int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const int a, const long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const int a, const int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const int a, const long double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const int a, const double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const long double a, const long double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const long double a, const unsigned long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const long double a, const unsigned int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const long double a, const long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const long double a, const int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const double a, const double b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const double a, const unsigned long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const double a, const unsigned int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const double a, const long int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
const mpreal pow(const double a, const int b, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
inline const mpreal mul_2ui(const mpreal& v, unsigned long int k, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
inline const mpreal mul_2si(const mpreal& v, long int k, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
inline const mpreal div_2ui(const mpreal& v, unsigned long int k, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
inline const mpreal div_2si(const mpreal& v, long int k, mp_rnd_t rnd_mode = mpreal::get_default_rnd());
//////////////////////////////////////////////////////////////////////////
// Estimate machine epsilon for the given precision
// Returns smallest eps such that 1.0 + eps != 1.0
inline mpreal machine_epsilon(mp_prec_t prec = mpreal::get_default_prec());
// Returns smallest eps such that x + eps != x (relative machine epsilon)
inline mpreal machine_epsilon(const mpreal& x);
// Gives max & min values for the required precision,
// minval is 'safe' meaning 1 / minval does not overflow
// maxval is 'safe' meaning 1 / maxval does not underflow
inline mpreal minval(mp_prec_t prec = mpreal::get_default_prec());
inline mpreal maxval(mp_prec_t prec = mpreal::get_default_prec());
// 'Dirty' equality check 1: |a-b| < min{|a|,|b|} * eps
inline bool isEqualFuzzy(const mpreal& a, const mpreal& b, const mpreal& eps);
// 'Dirty' equality check 2: |a-b| < min{|a|,|b|} * eps( min{|a|,|b|} )
inline bool isEqualFuzzy(const mpreal& a, const mpreal& b);
// 'Bitwise' equality check
// maxUlps - a and b can be apart by maxUlps binary numbers.
inline bool isEqualUlps(const mpreal& a, const mpreal& b, int maxUlps);
//////////////////////////////////////////////////////////////////////////
// Convert precision in 'bits' to decimal digits and vice versa.
// bits = ceil(digits*log[2](10))
// digits = floor(bits*log[10](2))
inline mp_prec_t digits2bits(int d);
inline int bits2digits(mp_prec_t b);
//////////////////////////////////////////////////////////////////////////
// min, max
const mpreal (max)(const mpreal& x, const mpreal& y);
const mpreal (min)(const mpreal& x, const mpreal& y);
//////////////////////////////////////////////////////////////////////////
// Implementation
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// Operators - Assignment
inline mpreal& mpreal::operator=(const mpreal& v)
{
if (this != &v)
{
mp_prec_t tp = mpfr_get_prec( mpfr_srcptr());
mp_prec_t vp = mpfr_get_prec(v.mpfr_srcptr());
if(tp != vp){
clear(mpfr_ptr());
mpfr_init2(mpfr_ptr(), vp);
}
mpfr_set(mpfr_ptr(), v.mpfr_srcptr(), mpreal::get_default_rnd());
MPREAL_MSVC_DEBUGVIEW_CODE;
}
return *this;
}
inline mpreal& mpreal::operator=(const mpf_t v)
{
mpfr_set_f(mpfr_ptr(), v, mpreal::get_default_rnd());
MPREAL_MSVC_DEBUGVIEW_CODE;
return *this;
}
inline mpreal& mpreal::operator=(const mpz_t v)
{
mpfr_set_z(mpfr_ptr(), v, mpreal::get_default_rnd());
MPREAL_MSVC_DEBUGVIEW_CODE;
return *this;
}
inline mpreal& mpreal::operator=(const mpq_t v)
{
mpfr_set_q(mpfr_ptr(), v, mpreal::get_default_rnd());
MPREAL_MSVC_DEBUGVIEW_CODE;
return *this;
}
inline mpreal& mpreal::operator=(const long double v)
{
mpfr_set_ld(mpfr_ptr(), v, mpreal::get_default_rnd());
MPREAL_MSVC_DEBUGVIEW_CODE;
return *this;
}
inline mpreal& mpreal::operator=(const double v)
{
#if (MPREAL_DOUBLE_BITS_OVERFLOW > -1)
if(fits_in_bits(v, MPREAL_DOUBLE_BITS_OVERFLOW))
{
mpfr_set_d(mpfr_ptr(),v,mpreal::get_default_rnd());
}else
throw conversion_overflow();
#else
mpfr_set_d(mpfr_ptr(),v,mpreal::get_default_rnd());
#endif
MPREAL_MSVC_DEBUGVIEW_CODE;
return *this;
}
inline mpreal& mpreal::operator=(const unsigned long int v)
{
mpfr_set_ui(mpfr_ptr(), v, mpreal::get_default_rnd());
MPREAL_MSVC_DEBUGVIEW_CODE;
return *this;
}
inline mpreal& mpreal::operator=(const unsigned int v)