Search code examples
c++mpfrboost-multiprecision

How am I suppose to use mpfr::mpfr_fac_ui function?


I tried finding an example of how to use mpfr::mpfr_fac_ui over the internet but I was unable to, so I decided to ask here.

I have my own iterative factorial

boost::multiprecision::mpfr_float factorial(int start, int end)
{
    boost::multiprecision::mpfr_float fact = 1;

    for (; start <= end; ++start)
        fact *= start;

    return fact;
}

but I want to try built-in factorial.

I don't know what I am doing wrong because when I am testing it like so

mpfr_t test;
mpfr_init2(test, 1000);

std::cout << mpfr_fac_ui(test, 5, MPFR_RNDN) << std::endl;
std::cout << factorial(1, 5) << std::endl;

mpfr_fac_ui does not return any errors (returns 0) and test is 0 while it should be 120.

Am I doing something wrong or I am missing something?


Solution

  • In C, I get 120 as expected with:

    #include <stdio.h>
    #include <mpfr.h>
    
    int main (void)
    {
      mpfr_t test;
      mpfr_init2 (test, 1000);
      mpfr_fac_ui (test, 5, MPFR_RNDN);
      mpfr_printf ("%Rg\n", test);
      mpfr_clear (test);
      return 0;
    }
    

    In your program, you do not show how you print the value of test. All what you do is to print the return value of mpfr_fac_ui, which is 0.