Search code examples
c++c++17gmp

How to assign a variable to the variables decalred with <gmp.h>


I recently used a library <gmp.h> and I am having difficulty assigning a value to the variable mpf_t x I declared. I used mpf_init2 (x,256) to initialize it and when I do x=1.234029187340918239082372984509283475029845702 I get

highprecision.cc:7:7: error: incompatible types in assignment of ‘double’ to ‘mpf_t’ {aka ‘__mpf_struct [1]’}
    7 |     x=1.234029187340918239082372984509283475029845702;
      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

this error. Any idea why? I am using C++17 GCC btw. The documentary is also very confusing. Sorry.


Solution

  • mpf_set_d(x, 1.234029187340918239082372984509283475029845702);
    

    Maybe reading the documentation would help?

    BTW if you think you are going to get 256 digits of precision in a double literal you are mistaken. 1.234029187340918239082372984509283475029845702 is still going to have the regular precision of a double on your system (around 17 to 19 digits usually).