Wrong division ?

Torbjörn Granlund tg at gmplib.org
Fri Apr 1 12:46:53 UTC 2016


Di Maria Giovanni <calimero22 at yahoo.it> writes:

  GMP gives:
  0.000.......4546540093519711704355108480114153379661....
  
*Your program* gives that.

  SAGE, Wolfram Alpha, PARI/GP and BC give:
  0.000.......4546540093519711704355108480114153355262....
  
  Is it normal?

  Here is the code:
  
  #include <stdio.h>
  #include <gmp.h>
  int main(void) {
     mpz_t x,y;
     mpf_t xx,yy,divisione;
     mpz_init(x);
     mpz_init(y);
     mpf_init(xx);
     mpf_init(yy);
     mpf_init2(divisione,50000);
     mpz_fac_ui(x,50);
     mpz_fac_ui(y,120);
     mpf_set_z(xx,x);
     mpf_set_z(yy,y);
     mpf_div(divisione,xx,yy);
     /*-----Print-----*/
     printf("\n\n\n\n\n");
     gmp_printf("%4.1000Ff\n",divisione);
     mpz_clear(x);
     mpz_clear(y);
     mpf_clear(xx);
     mpf_clear(yy);
     mpf_clear(divisione);
     return 0;
  }
  
Note that mpf uses floating point representation also for integral
values.  Neither 50! and 120! are likely to be stored without rounding
as floats.

-- 
Torbjörn
Please encrypt, key id 0xC8601622


More information about the gmp-discuss mailing list