MPFR is missing a "mpfr_ui_div_ui"

% Remco Bloemen % 2009-11-24, last updated 2014-03-03

Strangely, MPFR is lacking a mpfr_ui_div_ui function, that is, a function that calculates the fraction of two small unsigned integers to very high accuracy. Until now I’ve been calculating it like this:

mpfr_set_ui(invprime, prime[i], GMP_RNDN);
mpfr_ui_div(invprime, 1, invprime, GMP_RNDN);

which is very slow! The reason is probably that the mpfr_ui_div is calculated using all bits in invprime, even though most of them are zero. A much faster method is to prepare a two-bit one constant like this:

mpfr_t one;
mpfr_init2(one, 2);
mpfr_set_ui(one, 1, GMP_RNDN);

and then do this:

mpfr_div_ui(invprime, one, primes[i], GMP_RNDN);

Remco Bloemen
Math & Engineering
https://2π.com