mpf_set_d

Patrick Pelissier Patrick.Pelissier at loria.fr
Mon Nov 24 10:08:51 CET 2003


On Sat, Nov 22, 2003 at 11:42:04 -0800, Hee Jung Lee wrote:
> thx ilya, i just read your article & found it very interesting.
> thank you for writing it.
> 
> i have an understanding of the fact that floating point
> arithmatic is not exact.  i just expected mpf_set_d to at least
> TRY to set my mpf_t number to be as "exact" as possible to
> the precision that i set the mpf_t number to be.  in my
> sample code, i set the default precision to be 400 bits.
> that's a lot of significant digits.  & what i ended up w/
> is an mpf_t number that's "exact" up to 16 digits, like double
> precision.  so.. my question has to do w/ how mpf_set_d
> works...

 I don't know if you are interesting but this sample code using mpfr, does what you want:

#include <stdio.h>
#include <stdlib.h>
#include "gmp.h"
#include "mpfr.h"
int main()
{
  double d;
  mpfr_t m;
  mpfr_set_default_prec(400);
  d = 0.897959184;
  mpfr_init(m);
  mpfr_set_str(m, "0.897959184", 10, GMP_RNDN);
  printf("\nd=%.16f\nm=", d);
  mpfr_out_str(stdout, 10, 0, m, GMP_RNDN);
  putchar('\n');
  mpfr_clear(m);
}
 
 I don't think using mpf[r]_set_d is a good idea.

Sincerely,
 Patrick Pelissier


More information about the gmp-discuss mailing list