Wrong division ?

Pedro Gimeno gmpdiscuss at formauri.es
Fri Apr 1 14:23:49 UTC 2016


Torbjörn Granlund wrote, On 2016-04-01 14:46:
> Note that mpf uses floating point representation also for integral
> values.  Neither 50! and 120! are likely to be stored without rounding
> as floats.

Indeed. I modified the OP's program like this:

#include <stdio.h>
#include <gmp.h>
int main(void) {
   mpq_t xx;
   mpf_t result;
   mpq_init(xx);
   mpz_fac_ui(mpq_numref(xx),50);
   mpz_fac_ui(mpq_denref(xx),120);
   mpf_init2(result,50000);
   mpf_set_q(result, xx);

   /*-----Print-----*/
   printf("\n\n\n\n\n");
   gmp_printf("%4.1000Ff\n",result);
   mpq_clear(xx);
   mpf_clear(result);
   return 0;
}

and the result matches the stated output. Unfortunately, %Qf is not yet implemented; I've been wanting it for a long time. The output of many exact calculations is a fraction, and having to use an intermediate mpf to store the result in order to display it is a waste of memory and time.


More information about the gmp-discuss mailing list