Search code examples
c++gmp

How to change precision of mpf_class after default inicialization?


I need to encapsulate mpf_class from MPIR library into my class A:

class A
{           
    mpf_class a;    // default constructor - default precision
public:
    A(){                
        a = mpf_class(0,my_precision); // initialize a with zero, but it remains with default precision 
        mpf_class b = mpf_class(0,my_precision); // initialize local b with zero with my_precision
    }
};

but in the constructor I cannot initialize a with my_precision, because operator= does not change the precision of destination.

The only way I founded is to change default precision to my_precision before create object of A, like

mpf_set_default_prec(my_precision);
A my_class;

which works, but it is a very bad solution. So, how to create field a with the wanted precision?


Solution

  • Just use the member initialization list to initialize a:

    A() : a(0, my_precision)
    { }
    

    This avoids the default construction of a.