0

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?

kuskmen
  • 3,648
  • 4
  • 27
  • 54

1 Answers1

1

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.

vinc17
  • 2,829
  • 17
  • 23
  • Hm, strange, now I that I print it the way you did, it shows 120 to me also, previously I was checking the internals of mpfr_t, but I guess I misread them. – kuskmen May 27 '19 at 08:54