Closed xuquan316 closed 10 months ago
example proc:
int main(int argc, char* argv[]) { using mpfr::mpreal; using std::cout; using std::endl;
// Required precision of computations in decimal digits
// Play with it to check different precisions
const int digits = 50;
// Setup default precision for all subsequent computations
// MPFR accepts precision in bits - so we do the conversion
mpreal::set_default_prec(mpfr::digits2bits(digits));
// Compute all the vital characteristics of mpreal (in current precision)
// Analogous to lamch from LAPACK
const mpreal one = 1.0;
const mpreal zero = 0.0;
const mpreal eps = std::numeric_limits<mpreal>::epsilon();
const int base = std::numeric_limits<mpreal>::radix;
const mpreal prec = eps * base;
const int bindigits = std::numeric_limits<mpreal>::digits(); // eqv. to mpfr::mpreal::get_default_prec();
const mpreal rnd = std::numeric_limits<mpreal>::round_error();
const mpreal maxval = std::numeric_limits<mpreal>::max();
const mpreal minval = std::numeric_limits<mpreal>::min();
const mpreal small = one / maxval;
const mpreal sfmin = (small > minval) ? small * (one + eps) : minval;
const mpreal round = std::numeric_limits<mpreal>::round_style();
const int min_exp = std::numeric_limits<mpreal>::min_exponent;
const mpreal underflow = std::numeric_limits<mpreal>::min();
const int max_exp = std::numeric_limits<mpreal>::max_exponent;
const mpreal overflow = std::numeric_limits<mpreal>::max();
// Additionally compute pi with required accuracy - just for fun :)
const mpreal pi = mpfr::const_pi();
cout.precision(digits); // Show all the digits
cout << "pi = "<< pi << endl;
cout << "eps = "<< eps << endl;
cout << "base = "<< base << endl;
cout << "prec = "<< prec << endl;
cout << "b.digits = "<< bindigits << endl;
cout << "rnd = "<< rnd << endl;
cout << "maxval = "<< maxval << endl;
cout << "minval = "<< minval << endl;
cout << "small = "<< small << endl;
cout << "sfmin = "<< sfmin << endl;
cout << "1/sfmin = "<< 1 / sfmin << endl;
cout << "round = "<< round << endl;
cout << "max_exp = "<< max_exp << endl;
cout << "min_exp = "<< min_exp << endl;
cout << "underflow = "<< underflow << endl;
cout << "overflow = "<< overflow << endl;
cout << pi.toString("%.5RNg")<<endl;
cout << pi.toString("%.5RNf")<<endl;
return 0;
} print: pi = 3.1415926535897932384626433832795028841971693993751 eps = 1.069105884036878258456214586860592751526078752042e-50 base = 2 prec = 2.138211768073756516912429173721185503052157504084e-50 b.digits = 167 rnd = 0.5 maxval = 2.0985787164673876924043581168838390706380979654733e+323228496 minval = 9.530259619551804292864679126930698081660784502237e-323228497 small = 4.7651298097759021464323395634653490408303922511185e-323228497 sfmin = 9.530259619551804292864679126930698081660784502237e-323228497 1/sfmin = 1.0492893582336938462021790584419195353190489827367e+323228496 round = 1 max_exp = 1073741823 min_exp = -1073741823 underflow = 9.530259619551804292864679126930698081660784502237e-323228497 overflow = 2.0985787164673876924043581168838390706380979654733e+323228496 3.1416 3.14159
code: set_default_prec(200); a = "3.1666666666666666666"; cout << a.toString("%.5RNf") <<endl; cout << a.toString("%.5RNg") <<endl; print: 3.16667 3.1667 mpreal version:3.7.0 mpfr version :4.2.1