Permission is granted to copy, distribute and / or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
MPFRCPP 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 2 of the License, or (at your option) any later version.
MPFRCPP 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 MPFRCPP; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
MPFRCPP is object oriented interface to MPFR library. MPFR provides arbitrary precision arithmetic on floating-point numbers. MPFRCPP overloads operators, functions and represents floating point numbers as class objects to make using of MPFR in OO-programs easier.
MPFRCPP was not supported by any organization. Author looks for official support from laboratories and universities which could help project without limiting its free distribution.
To use MPFRCPP you should download and install
MPFRCPP sources should be copied to compiler include path (/usr/include/ or /usr/local/include/, for example) in «mpfrcpp» directory. Then you can use MPFRCPP by including its header
#include </mpfrcpp/mpfrcpp.hpp>
Now compatibility was tested with MPFR 2.2.0, GMP 4.2.1, g++ 3.3.5. There is no warranty that MPFRCPP compatible with newer or older version of libraries.
Before use certain function or operator, check its definition at the sources and consult MPFR manual.
C++ interface was not aimed to make operations faster. More important tasks for developing were achieving compatibility with modern compilers and making simple front-end to MPFR C library. While programming complex solutions, try to compare C and C++ realizations of algorithms. C++ realizations should be compatible with C++ templates, more universal but slower.
Important item of MPFRCPP is that it works with objects of one base class. It differs from object interface of GMP, ARPREC and other libraries which uses special internal temporary type to prevent creating objects after each operation in expression.
For example, in MPFRCPP
x = (y+z)*x
will create object containing y+z
, then other one containing (y+z)*x
and then finally do assignment. GMP and ARPREC C++ interfaces will create temporary type objects and it should be a little faster.
But using of temporary types connected with some problems: in many cases programmer needs to perform conversions from temporary type to normal type. It makes writing mathematical codes with type-independent templates impossible.
Thanks to MPFR and GMP base, MPFRCPP is very fast, it is faster than many other arbitrary precision libraries, even MPFRCPP changed speed of calculations on code beauty.
MPFRCPP was tested on compatibility with complex<T>
and valarray<T>
[T = MPFR]
. We haven't tested all realizations of Standard C++ Library, but MPFRCPP should be compatible with g++. If your compiler can't do something, please, send me compiler error output and library headers.
This section applies to MPFR user manual.
Table describing MPFRCPP interface is given below. General functions/methods in the MPFRCPP namespace are given as MPFR::foo()
; methods for certain class objects are given as x.method()
. MPFRCPP functions were given as usage designations, not as strict prototypes: qualifiers were ignored (prototypes will be listed at manual for the first public release).
Feature, function, type, macros of MPFR | MPFRCPP implementation: usage and link to source | Notes |
---|---|---|
1. Types | ||
mpfr_t |
MPFR <mpfrcpp/mpfrcpp.hpp> |
|
mp_prec_t |
mp_prec_t |
|
mp_rnd_t |
std::float_round_style <limits> |
MPFR internal type mp_rnd_t does not conform to ISO/IEC 14882:1998, section 18.2.1.3, «Type float_round_style ». Internal functions frs2mprnd() mprnd2frs() should perform conversion. While designing MPFRCPP functions, use only std::float_round_style as defined at <limits> . |
2. Initialization Functions | ||
void mpfr_init2 (mpfr_t, mp_prec_t) |
MPFR([mp_prec_t prec=MPFR::default_prec()], [std::float_round_style rnd=MPFR::default_round_mode()]) <mpfrcpp/constructors.hpp> |
Sets number to 0 if MPFR::set_initd_to_0() , to NaN else |
void mpfr_clear (mpfr_t) |
~MPFR() <mpfrcpp/constructors.hpp> |
MPFR destructor |
void mpfr_set_default_prec (mp_prec_t) |
MPFR::default_prec(mp_prec_t) <mpfrcpp/mpfrcpp.hpp> |
Works with DEFAULT_PREC variable without calling MPFR function |
mp_prec_t mpfr_get_default_prec () |
mp_prec_t MPFR::default_prec() <mpfrcpp/mpfrcpp.hpp> |
Works with DEFAULT_PREC variable without calling MPFR function |
void mpfr_set_prec (mpfr_t, mp_prec_t) |
void x.prec(mp_prec_t) <mpfrcpp/mpfrcpp.hpp> |
|
mp_prec_t (mpfr_t) |
mp_prec_t x.prec() <mpfrcpp/mpfrcpp.hpp> |
|
3. Assignment Functions | ||
int mpfr_set (mpfr_t, mpfr_t, mp_rnd_t) |
void MPFR::operator= ({…}) <assignment_conversion.cpp> |
void MPFR::operator= can increase precision of left operand to precision of right operand, base of left operand will be changed to the base of right operand |
int mpfr_set_uj (mpfr_t, uintmax_t, mp_rnd_t) |
Will be ignored in MPFRCPP | MPFRCPP does not works with uintmax_t and intmax_t |
int mpfr_set_{…}_2exp (mpfr_t, {…}, mp_exp_t, mp_rnd_t) |
Friend of class MPFR . Deprecated |
|
int mpfr_set_str (mpfr_t, char *, int, mp_rnd_t) |
MPFR(const std::string, int, [mp_prec_t prec=MPFR::default_prec()], [std::float_round_style rnd=MPFR::default_round_mode()]) <mpfrcpp/constructors.hpp> |
|
void MPFR::operator= (std::string) <assignment_conversion.cpp> |
||
void MPFR::operator= (char *) <assignment_conversion.cpp> |
||
int mpfr_strtofr (mpfr_t, char *, char **, int, mp_rnd_t) |
Is needed? | |
void mpfr_set_inf (mpfr_t, int) |
x.inf(int) <mpfrcpp/mpfrcpp.hpp> |
|
void mpfr_set_nan (mpfr_t) |
x.nan() <mpfrcpp/mpfrcpp.hpp> |
|
void mpfr_swap (mpfr_t, mpfr_t) |
Friend of class MPFR . Deprecated |
|
- | void MPFR::operator= (mpz_class) <mpfrcpp/mpfrcpp.hpp> |
|
4. Combined Initialization and Assignment Functions<mpfrcpp/constructors.hpp> |
||
int mpfr_init_set (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR(MPFR) |
|
int mpfr_init_set_{…} (mpfr_t, {…}, mp_rnd_t) |
MPFR({…}, [mp_prec_t prec=MPFR::default_prec()], [std::float_round_style rnd=MPFR::default_round_mode()]) |
|
- | MPFR::MPFR(const mpz_class, [mp_prec_t prec=MPFR::default_prec(), std::float_round_style rnd=MPFR::default_round_mode()]) |
|
5. Conversion Functions<assignment_conversion.cpp> |
||
{…} mpfr_get_{…} (mpfr_t, mp_rnd_t) |
MPFR::operator {…} () |
Number to convert must fits() in conversion target type |
int mpfr_fits_{…} |
x.fits<T>() |
T = short , int , long , unsigned short , unsigned int , unsigned long , float , double , long double .Based on ISO/IEC 14882:1998, section 18.2.1, «Numeric limits», uses comparison with connected fields of numeric_limits<{…}> . |
- | MPFR::operator mpz_class () |
|
6. Basic Arithmetic Functions<mpfrcpp/basic_arithmetic.cpp> |
||
int mpfr_add (mpfr_t, mpfr_t, mpfr_t, mp_rnd_t) |
MPFR operator+ (MPFR, MPFR) |
|
int mpfr_sub (mpfr_t, mpfr_t, mpfr_t, mp_rnd_t) |
MPFR operator- (MPFR, MPFR) |
|
int mpfr_mul (mpfr_t, mpfr_t, mpfr_t, mp_rnd_t) |
MPFR operator* (MPFR, MPFR) |
|
int mpfr_div (mpfr_t, mpfr_t, mpfr_t, mp_rnd_t) |
MPFR operator/ (MPFR, MPFR) |
|
int mpfr_sqr (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR sqr (const MPFR& x) .Deprecated |
|
int mpfr_sqrt (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR sqrt (MPFR) |
|
int mpfr_cbrt (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR cbrt (MPFR) |
|
int mpfr_root (mpfr_t, mpfr_t, unsigned long int, mp_rnd_t) |
MPFR cbrt (MPFR, unsigned long int) |
|
int mpfr_pow (mpfr_t, mpfr_t, mpfr_t, mp_rnd_t) |
MPFR pow (MPFR, MPFR) |
|
int mpfr_pow_{…} (mpfr_t, {…}, mpfr_t, mp_rnd_t) |
MPFR pow (MPFR, {…}) |
|
int mpfr_{…}_pow (mpfr_t, {…}, mpfr_t, mp_rnd_t) |
MPFR pow ({…}, MPFR) |
|
int mpfr_neg (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR MPFR::operator- () |
|
int mpfr_abs (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR abs (MPFR) |
|
int mpfr_mul_2{…} (mpfr_t, mpfr_t, {…}, mp_rnd_t) |
Friend of class MPFR . Deprecated |
|
int mpfr_div_2{…} (mpfr_t, mpfr_t, {…}, mp_rnd_t) |
||
- | MPFR MPFR::operator-- (int) |
|
MPFR MPFR::operator++ (int) |
||
MPFR MPFR::operator-- |
||
MPFR MPFR::operator++ |
||
operator+= (MPFR, {…}) |
||
operator-= (MPFR, {…}) |
||
operator*= (MPFR, {…}) |
||
operator/= (MPFR, {…}) |
||
7. Comparison Functions<mpfrcpp/comparison.cpp> |
||
int mpfr_cmp (mpfr_t, mpfr_t) |
Friend of class MPFR . Deprecated |
|
int mpfr_cmp_{…} (mpfr_t, {…}) |
||
int mpfr_cmp_{…})_2exp (mpfr_t, {…}, mp_exp_t) |
||
int mpfr_cmpabs (mpfr_t, mpfr_t) |
||
int mpfr_nan_p (mpfr_t) |
bool is_nan (MPFR) |
|
int mpfr_inf_p (mpfr_t) |
bool is_inf (MPFR) |
|
int mpfr_number_p (mpfr_t) |
bool is_number (MPFR) |
|
int mpfr_zero_p (mpfr_t) |
bool is_zero (MPFR) |
|
int mpfr_unordered_p (mpfr_t, mpfr_t) |
bool is_unordered (MPFR, MPFR) |
|
int mpfr_sgn (mpfr_t) |
int sgn (MPFR) |
|
int mpfr_greater_p (mpfr_t, mpfr_t) |
bool operator> (MPFR, MPFR) |
|
int mpfr_greaterequal_p (mpfr_t, mpfr_t) |
bool operator>= (MPFR, MPFR) |
|
int mpfr_less_p (mpfr_t, mpfr_t) |
bool operator< (MPFR, MPFR) |
|
int mpfr_lessequal_p (mpfr_t, mpfr_t) |
bool operator<= (MPFR, MPFR) |
|
int mpfr_equal_p (mpfr_t op1, mpfr_t op2) |
bool operator== (MPFR, MPFR) |
|
8. Special Functions<mpfrcpp/special.cpp> |
||
int mpfr_log (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR log (MPFR) |
|
int mpfr_log2 (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR log2 (MPFR) |
|
int mpfr_log10 (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR log10 (MPFR) |
|
int mpfr_exp (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR exp (MPFR) |
|
int mpfr_exp2 (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR exp2 (MPFR) |
|
int mpfr_exp10 (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR exp10 (MPFR) |
|
int mpfr_cos (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR cos (MPFR) |
|
int mpfr_sin (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR sin (MPFR) |
|
int mpfr_tan (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR tan (MPFR) |
|
int mpfr_sec (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR sec (MPFR) |
|
int mpfr_csc (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR csc (MPFR) |
|
int mpfr_cot (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR cot (MPFR) |
|
int mpfr_sin_cos (mpfr_t, mpfr_t, mpfr_t, mp_rnd_t) |
Friend of class MPFR . Deprecated |
|
int mpfr_acos (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR acos (MPFR) |
|
int mpfr_asin (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR asin (MPFR) |
|
int mpfr_atan (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR atan (MPFR) |
|
int mpfr_atan2 (mpfr_t, mpfr_t, mpfr_t, mp_rnd_t) |
MPFR atan2 (MPFR, MPFR) |
|
int mpfr_cosh (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR cosh (MPFR) |
|
int mpfr_sinh (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR sinh (MPFR) |
|
int mpfr_tanh (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR tanh (MPFR) |
|
int mpfr_sech (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR sech (MPFR) |
|
int mpfr_csch (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR csch (MPFR) |
|
int mpfr_coth (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR coth (MPFR) |
|
int mpfr_acosh (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR acosh (MPFR) |
|
int mpfr_asinh (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR asinh (MPFR) |
|
int mpfr_atanh (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR atanh (MPFR) |
|
int mpfr_fac_ui (mpfr_t, unsigned long int, mp_rnd_t) |
MPFR factorial (unsigned long int) |
Maybe, will be moved to MPFR:: namespace as well as other functions taking only built-in type arguments and returning MPFR . |
int mpfr_log1p (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR log1p (MPFR) |
|
int mpfr_expm1 (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR expm1 (MPFR) |
|
int mpfr_eint (mpfr_t y, mpfr_t x, mp_rnd_t) |
MPFR eint (MPFR) |
|
int mpfr_gamma (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR gamma (MPFR) |
|
- | MPFR beta (MPFR, MPFR) |
Now it just calls gamma(x) * gamma(y) / gamma(x+y) . |
int mpfr_lngamma (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR lngamma (MPFR) |
|
int mpfr_zeta (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR zeta (MPFR) |
|
int mpfr_erf (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR erf (MPFR) |
|
int mpfr_erfc (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR erfc (MPFR) |
|
int mpfr_fma (mpfr_t, mpfr_t, mpfr_t, mpfr_t, mp_rnd_t) |
MPFR erfc (MPFR) |
|
int mpfr_agm (mpfr_t, mpfr_t, mpfr_t, mp_rnd_t) |
MPFR erfc (MPFR, MPFR) |
|
int mpfr_hypot (mpfr_t, mpfr_t, mpfr_t, mp_rnd_t) |
MPFR hypot (MPFR, MPFR) |
|
int mpfr_const_log2 (mpfr_t, mp_rnd_t) |
MPFR const_log2 ([mp_prec_t prec=MPFR::default_prec()], [std::float_round_style rnd=MPFR::default_round_mode()], [bool clear_cache = false]) |
If clear_cache , function will call MPFR::cache::free() |
int mpfr_const_pi (mpfr_t, mp_rnd_t) |
MPFR const_pi ([mp_prec_t prec=MPFR::default_prec()], [std::float_round_style rnd=MPFR::default_round_mode()], [bool clear_cache = false]) |
|
int mpfr_const_euler (mpfr_t, mp_rnd_t) |
MPFR const_euler ([mp_prec_t prec=MPFR::default_prec()], [std::float_round_style rnd=MPFR::default_round_mode()], [bool clear_cache = false]) |
|
int mpfr_const_catalan (mpfr_t, mp_rnd_t) |
MPFR const_catalan ([mp_prec_t prec=MPFR::default_prec()], [std::float_round_style rnd=MPFR::default_round_mode()], [bool clear_cache = false]) |
|
void mpfr_free_cache (void) |
MPFR::cache::free() <mpfrcpp/mpfrcpp.hpp> |
Could be called immediately after the calculating constant (see MPFRCPP const_{…}() ) |
int mpfr_sum (mpfr_t, mpfr_ptr&[], unsigned&long, mp_rnd_t) |
Will be ignored in MPFRCPP | Could be realized by valarray class (<valarray> ) from the Standard C++ Library as well as other operations with arrays. |
9. Input and Output Functions<mpfrcpp/stream.cpp> |
||
size_t mpfr_out_str (FILE *, int, size_t, mpfr_t, mp_rnd_t) |
std::basic_ostream& operator<< (std::basic_ostream, MPFR&) |
Using x.base() while forming output string |
size_t mpfr_inp_str (mpfr_t, FILE *, int, mp_rnd_t) |
int operator>> (std::basic_istream&, MPFR&) |
Base will be detected automatically. int -return is return of called mpfr_set_str() . |
10. Integer Related Functions<mpfrcpp/integer_related.cpp> |
||
int mpfr_rint (mpfr_t, mpfr_t, mp_rnd_t) |
Is needed? | |
int mpfr_ceil (mpfr_t, mpfr_t) |
||
int mpfr_floor (mpfr_t, mpfr_t) |
||
int mpfr_round (mpfr_t, mpfr_t) |
||
int mpfr_trunc (mpfr_t, mpfr_t) |
||
int mpfr_rint_ceil (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR ceil (MPFR) |
|
int mpfr_rint_floor (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR floor (MPFR) |
|
int mpfr_rint_round (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR round (MPFR) |
|
int mpfr_rint_trunc (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR trunc (MPFR) |
|
int mpfr_frac (mpfr_t, mpfr_t, mp_rnd_t) |
MPFR frac (MPFR) |
|
int mpfr_integer_p (mpfr_t) |
bool is_integer (MPFR) |
|
11. Miscellaneous Functions | ||
void mpfr_nexttoward (mpfr_t, mpfr_t) |
Is needed? | |
void mpfr_nextabove (mpfr_t) |
||
void mpfr_nextbelow (mpfr_t) |
||
int mpfr_min (mpfr_t, mpfr_t, mpfr_t, mp_rnd_t) |
Will be ignored in MPFRCPP | Should be implemented by min /max from the Standard Template Library (<algorithm> ) |
int mpfr_max (mpfr_t, mpfr_t, mpfr_t, mp_rnd_t) |
||
int mpfr_urandomb (mpfr_t, gmp_randstate_t) |
MPFR urandomb (gmp_randstate_t, [mp_prec_t prec=MPFR::default_prec()], [std::float_round_style rnd=MPFR::default_round_mode()]) <mpfrcpp/special.cpp> |
|
void mpfr_random (mpfr_t) |
Will be ignored in MPFRCPP | |
void mpfr_random2 (mpfr_t, mp_size_t, mp_exp_t) |
MPFR random2 (mp_size_t, mp_exp_t, [mp_prec_t prec=MPFR::default_prec()], [std::float_round_style rnd=MPFR::default_round_mode()]) <mpfrcpp/special.cpp> |
|
mp_exp_t mpfr_get_exp (mpfr_t) |
mp_exp_t x.exp() <mpfrcpp/mpfrcpp.hpp> |
|
int mpfr_set_exp (mpfr_t, mp_exp_t) |
void x.exp(mp_exp_t) <mpfrcpp/mpfrcpp.hpp> |
|
const char * mpfr_get_version (void) |
Will be ignored in MPFRCPP | |
MPFR_VERSION |
||
MPFR_VERSION_MAJOR |
||
MPFR_VERSION_MINOR |
||
MPFR_VERSION_PATCHLEVEL |
||
MPFR_VERSION_STRING |
||
12. Rounding Modes<mpfrcpp/mpfrcpp.hpp> |
||
void mpfr_set_default_rounding_mode (mp_rnd_t) |
void MPFR::default_round_mode (std::float_round_style) |
Works with DEFAULT_ROUND_MODE variable without calling MPFR function |
mp_rnd_t mpfr_get_default_rounding_mode () |
std::float_round_style MPFR::default_round_mode() |
Works with DEFAULT_ROUND_MODE variable without calling MPFR function |
int mpfr_prec_round (mpfr_t, mp_prec_t, mp_rnd_t) |
x.round (mp_prec_t, [std::float_round_style rnd=MPFR::default_round_mode()]) |
|
int mpfr_round_prec (mpfr_t, mp_rnd_t, mp_prec_t) |
Will be ignored in MPFRCPP | |
const char * mpfr_print_rnd_mode (mp_rnd_t) |
||
13. Exceptions<mpfrcpp/mpfrcpp.hpp> |
||
mp_exp_t mpfr_get_emin () |
mp_exp_t emin() |
|
mp_exp_t mpfr_get_emax () |
mp_exp_t emax() |
|
int mpfr_set_emin (mp_exp_t) |
int emin(mp_exp_t) |
|
int mpfr_set_emax (mp_exp_t) |
int emax(mp_exp_t) |
|
mp_exp_t mpfr_get_emin_min () |
mp_exp_t emin_min() |
|
mp_exp_t mpfr_get_emin_max () |
mp_exp_t emin_max() |
|
mp_exp_t mpfr_get_emax_min () |
mp_exp_t emax_min() |
|
mp_exp_t mpfr_get_emax_max () |
mp_exp_t emax_max() |
|
int mpfr_check_range (mpfr_t, int, mp_rnd_t) |
int x.range() |
Ternary. Mean of the return is described at MPFR manual |
int mpfr_subnormalize (mpfr_t, int, mp_rnd_t) |
void subnormalize(int) |
|
void mpfr_clear_underflow () |
MPFR::flags::underflow(false) |
|
void mpfr_set_underflow () |
MPFR::flags::underflow(true) |
|
int mpfr_underflow_p () |
bool MPFR::flags::underflow() |
|
void mpfr_clear_overflow () |
MPFR::flags::overflow(false) |
|
void mpfr_set_overflow () |
MPFR::flags::overflow(true) |
|
int mpfr_overflow_p () |
bool MPFR::flags::overflow() |
|
void mpfr_clear_nanflag () |
MPFR::flags::nan(false) |
|
void mpfr_set_nanflag () |
MPFR::flags::nan(true) |
|
int mpfr_nanflag_p () |
bool MPFR::flags::nan() |
|
void mpfr_clear_inexflag () |
MPFR::flags::inexact(false) |
|
void mpfr_set_inexflag () |
MPFR::flags::inexact(true) |
|
int mpfr_inexflag_p () |
bool MPFR::flags::inexact() |
|
void mpfr_clear_erangeflag () |
MPFR::flags::erange(false) |
|
void mpfr_set_erangeflag () |
MPFR::flags::erange(true) |
|
int mpfr_erangeflag_p () |
bool MPFR::flags::erange() |
|
void mpfr_clear_flags () |
void MPFR::flags::clear() |
|
14. MPFR advanced Functions | ||
* | Will be ignored in MPFRCPP | |
15. Compatibility with MPF | ||
* | Will be ignored in MPFRCPP | |
16. MPFR custom interface | ||
* | Will be ignored in MPFRCPP | |
17. MPFR internals | ||
* | Will be ignored in MPFRCPP |
Some not implemented functions could be added to MPFR class friends. Please, inform MPFRCPP maintainer about functions you want to see in MPFR friends.
uintmax_t
and intmax_t
.MPFR stores rounding mode for class object. The rules of rounding are:
void round_mode (std::float_round_style)
method. Available values are
std::float_round_style round_mode()
method.x1
, x2
uses rounding mode of arguments if x1.round_mode() == x2.round_mode()
. If x1.round_mode() != x2.round_mode()
, default rounding mode MPFR::default_round_mode()
will be used. Default rounding mode could be changed by MPFR::default_round_mode (std::float_round_style rnd)
method.std::float_round_style round_mode()
argument (see prototypes).x = y
use rounding mode of x
if x
is MPFR object.Please, note that rounding mode will be applied to conversion. For example, if conversion from built-in floating point type to built-in integer type works like rounding to integer by floor()
function, conversion from MPFR
class object x
to integer needs you to control x.round_mode()
to get correct result.
Actually, there is MPFR floor (MPFR)
function. So, if
static_cast<int>(0.999)
results 0,
static_cast<int>(MPFR(0.999))
can result 1. That's why you need to use
static_cast<int>(floor(MPFR(0.999)))
Rules for precision of objects created by functions are the same as rules for rounding modes.
void prec (mp_prec_t)
method. Current precision could be checked by mp_prec_t prec()
method.x1
, x2
uses precision max (x1.prec(), x2.prec())
.x = y
use max (x.prec(), y.prec())
precision.mpf_t
and mpf_class
stores their precision. So, MPFRCPP can operate them like MPFR
class object.We call this «scalable precision».
This scheme seems to be too complex or too simple in certain cases, but, actually, real solutions don't need using of different parameters. If precision and rounding mode are very important for you, be accurate with managing anonymous objects and check parameters if they were not defined directly.
Precision for output is connected with stream.precision()
. Set stream.precision(0)
if output with actual number precision needed. It could differ from stream.precision(0)
for built-in floats:
std::cout.precision(0); std::cout << 123456789.0L << std::endl; // Output: 1e+08 std::cout << MPFR(123456789) << std::endl; // Output: 1.23456789(0)e8
Also you should be accurate with initializations and assignments from built-in float types. It can because errors connected with precision. Many decimal fractions could not be represented in ±2 base system. For example, 1.675
can be represented as
1.675000000000000044408920985006261616945266...
So, be accurate while initializing MPFR
from built-in floats. The better way is to initialize from strings:
MPFR::default_round_mode(std::round_to_nearest); MPFR::default_prec(128); std::cout << MPFR(1.675) << std::endl; // Oops... // Output: 1.675000000000000044408920985006261616945e0 std::cout << MPFR("1.675", 10) << std::endl; // Okay // Output: 1.674999999999999999999999999999999999998e0
Common strategy is to initialize MPFR
from strings or perform division instead of decimal fractions use:
std::cout << MPFR(1675)/MPFR(1000) << std::endl; // Output: 1.675000000000000000000000000000000000000e0
Note that it depends on MPFR
default rounding mode, as you can see. MPFR(1675)/MPFR(1000)
can result 1.675(0), MPFR(67)/MPFR(40)
or MPFR("1.675", 10)
can result 1.674(9)8
. This difference can't cause errors:
MPFR(1675)/MPFR(1000) - MPFR("1.675", 10) == 0; // results true MPFR(1675)/MPFR(1000) - MPFR(67)/MPFR(40) == 0; // results true
By default, MPFR sets initialized number to NaN. Some realizations of mathematical classes assume that non-assigned numbers should be zeros. For example, complex<T>
implementation in g++ headers:
template<typename _Tp> complex<_Tp>& complex<_Tp>::operator=(const _Tp& __t) { _M_real = __t; _M_imag = _Tp(); // Ouch! return *this; }
This means that given template with _Tp = MPFR
will result complex<MPFR> (Re) == complex<MPFR> (Re, NaN)
! Some g++ headers uses complex<_Tp> (_Tp Re)
constructor, that's why you can easily get (NaN, NaN) complex number after the trivial arithmetics (actually, T()
initialization of imaginary part could be finded in ISO/IEC 14882
).
Our constructor sets initialized numbers to zero if defined (see <mpfrcpp/config.cpp>
)
extern bool SET_INITD_TO_0 = true;
You can edit your compiler C++ headers for complex
, if needed, but it is not recommended.
Please, email any bug reports, questions and suggestions to bav.272304@gmail.com. Include information about your system and compiler and attach source code.
See Frequently Asked Questions about MPFR for solutions of common problems with MPFR.
operator?:
' in '((__n % 2) != 0) ? __x : 1
'»gcc says omething like this:
/usr/include/c++/3.3/bits/cmath.tcc: In function `_Tp std::__cmath_power(_Tp, unsigned int) [with _Tp = std::complex<MPFR>]': /usr/include/c++/3.3/cmath:477: instantiated from `_Tp std::__pow_helper(_Tp, int) [with _Tp = std::complex<MPFR>]' /usr/include/c++/3.3/complex:564: instantiated from `std::complex<_Tp> std::pow(const std::complex<_Tp>&, int) [with _Tp = MPFR]' test.cpp:112: instantiated from here /usr/include/c++/3.3/bits/cmath.tcc:42: error: no match for ternary 'operator?:' in '((__n % 2) != 0) ? __x : 1'
You should open your /usr/include/c++/*/bits/cmath.tcc, find there line
_Tp __y = __n % 2 ? __x : 1;
comment it and add after the commented line code like this:
// Actually, it does not differs from
// `_Tp __y = __n % 2 ? __x : 1' :
_Tp __y;
if (__n % 2) { __y = __x; }
else { __y = 1; }
It seems to be the error in g++ <cmath>
implementation (or <complex>
implementations). If you know more accurate way to correct it, tell me.
Other variants of Standard C++ Library also could contain bugs, but we don't recommend you to edit header files.
mpfr_cmp
, mpfr_cmp_ui
, mpfr_cmp_si
as friend functionsYou can find commented block at <mpfrcpp/mpfrcpp.hpp>
:
/*
friend int mpfr_cmp (mpfr_t, mpfr_t);
friend int mpfr_cmp_ui (mpfr_t, unsigned long int);
friend int mpfr_cmp_si (mpfr_t, long int);
*/
g++ can't compile it:
/usr/include/mpfrcpp/mpfrcpp.hpp:416: error: type specifier omitted for parameter /usr/include/mpfrcpp/mpfrcpp.hpp:416: error: parse error before numeric constant /usr/include/mpfrcpp/mpfrcpp.hpp:417: error: parse error before `,' token /usr/include/mpfrcpp/mpfrcpp.hpp:418: error: parse error before `,' token
It seems to be the bug in MPFR implementation of mpfr_cmp
, mpfr_cmp_ui
, mpfr_cmp_si
. Don't use these functions with current version of MPFRCPP.
Add «-lmpfr -lgmp
» to compiler flags or specify paths to MPFR and GMP libraries.
Check the versions of libraries. Only MPFR 2.x and GMP 4.x branches are supported. Update MPFR and GMP to latest versions.
If latest versions used, send me test code to let me check, is it a MPFRCPP or MPFR bug and is your code uses MPFRCPP correctly.
If you want to modify MPFRCPP, please, send me your code improvements. I plan to develop MPFRCPP in the future, but note that
complex<T>
and valarray<T>
. Extensions for complex<T>
and valarray<T>
could be included soon.MPFRCPP was written by Alexey Beshenov, Lipetsk, Russia. I am secondary school graduate in 2006 (with specialization on math, physics and computer science). My interests are numeric analysis and C++ programming.
I wish to thank people who helped me in work. Authors of MPFR are Guillaume Hanrot, Vincent Lefèvre, Patrick Pélissier and Paul Zimmermann. Dmitry Beshenov consulted me on some C++ items. Nathalie Revol written MPFR++ — other C++ interface to MPFR (now MPFR++ is not up-to-date). Yozo Hida is the developer of ARPREC package. Thanks!
ISO/IEC 14882
. ISO, IEC, ANSI. 1998.