converting a mpq to a double

Patrick Pelissier Patrick.Pelissier at
Fri Jun 10 14:42:31 CEST 2005

> is there any kind of guarantee that the generated double is close
> to the rational number op.
> double mpq_get_d (mpq_t op)  	
> Convert op to a double.

 Use MPFR as an intermediary:

 double my_mpq_get_d (mpq_t op) {
  mpfr_t q;
  double ret;
  mpfr_init2 (q, 53);
  mpfr_set_q (q, op, GMP_RNDN); /* Or other RNDN modes */
  ret = mpfr_get_d (q, GMP_RNDN); /* exact */
  mpfr_clear (q);
  return ret;

  Patrick Pelissier

More information about the gmp-discuss mailing list