2

I'm beginner with mpfr, I wrote this program to make a specific calculation and get it like you see,but I don't know why this error appear, this is the most important my code:

mpfr_t  myfct(int n)

{
   /......./
        return mpfr_get(result,MPFR_RNDN);

}

int main(void)
{

    mpfr_t U;
    mpfr_set_default_prec (53); 

    mpfr_set_emin (-1073);
    mpfr_set_emax (1024);

    n=10;
    mpfr_init2(U,24);
    mpfr_get(U,my_fct(n),MPFR_RNDN);
    mpfr_printf ("result: %.40Rg\n", U);

    mpfr_clear(U);

    return 0;
}

What's the matter ??

wolfgunner
  • 119
  • 2
  • 13

2 Answers2

1

Like GMP types, mpfr_t is an array of size 1 (the element is a structure, but you don't really need to know that). If you decide to write a function that returns a MPFR number, you have 2 possibilities (among others):

  1. The caller allocates and inits the mpfr_t with some given precision. In this case, the mpfr_t should be put as an argument of the function, and this is a pointer to this array that is actually passed (according to the rules of the C language). Basically, this can be seen as passing the variable by reference. MPFR functions use this method.
  2. The caller doesn't allocate anything. The prototype of the function should be such that the function returns a pointer mpfr_ptr to the structure. So, this function will allocate the mpfr_t typically with malloc (so that the memory is not freed when the function returns), then init the structure with mpfr_init2. The precision may be chosen by this function or passed as an argument.

Note: your program have various typos, but I suppose that this is not the point of this question.

vinc17
  • 2,829
  • 17
  • 23
0

mpfr_t, just like any other array type, cannot be used as a return value type; see #1592.

To return a mpfr_t value, please follow the convention that all MPFR functions use, and return it through a function parameter.

There is an another error in your code: mpfr_get() should return int (as per the convention; haven`t actually found such function in MPFR docs), not a mpfr_t, see above.

hidefromkgb
  • 5,834
  • 1
  • 13
  • 44