mpf_t and mpf_get_str

Carlos Carreras carreras at die.upm.es
Mon Nov 19 09:40:59 CET 2007


Hi,

In the following code two mpf_t are initialized with two decimal strings
that
are different in the last digit. Both strings produce the same mantissa
and
exponent values in the mpf_t's (due to the base conversion, I assume).
However,
the strange thing is that both mpf_t (with identical binary mantissa and
exponent as shown through printf) then produce different decimal output
strings. Am I doing something wrong? Can anyone explain? Thanks.

(I've tried other ways to 'build' the mpf_t, like initializing from the
equivalent binary string, or initializing from the binary mantissa and
shifting
it with mpf_div_2exp() to get the binary exponent, and the final mpf_t
always
produces the second of the two output decimal strings below.)

int main() {
  mpf_set_default_prec(256);

  mpf_t n;
  char c_n[] =
"0.000000001875000026575627008461288879635329944698826788978426034113560035351407471672013";
  mpf_init_set_str(n, c_n, 10);
  mp_exp_t n_exp;
  char *n_mant10 = mpf_get_str(NULL, &n_exp, 10, 0, n);
  printf("\nn_mant10 = %s\nn_exp10  = %d\n\n", n_mant10, n_exp);
  free(n_mant10);
  char *n_mant2 = mpf_get_str(NULL, &n_exp, 2, 0, n);
  printf("n_mant2  = %s\nn_exp2   = %d\n\n", n_mant2, n_exp);
  free(n_mant2);
  mpf_clear(n);

  mpf_t m;
  char c_m[] =
"0.000000001875000026575627008461288879635329944698826788978426034113560035351407471672011";
  mpf_init_set_str(m, c_m, 10);
  mp_exp_t m_exp;
  char *m_mant10 = mpf_get_str(NULL, &m_exp, 10, 0, m);
  printf("m_mant10 = %s\nm_exp10  = %d\n\n", m_mant10, m_exp);
  free(m_mant10);
  char *m_mant2 = mpf_get_str(NULL, &m_exp, 2, 0, m);
  printf("m_mant2  = %s\nm_exp2   = %d\n\n", m_mant2, m_exp);
  free(m_mant2);
  mpf_clear(m);

  return 0;
}

Carlos



More information about the gmp-discuss mailing list