How to serialize the GMP mpf type?

血红的双手。 提交于 2019-12-12 07:58:01

问题


It seems that GMP provides only string serialization of the mpf (floating point) type:

mpf_get_str(), mpf_class::get_str()

The mpz (integer) type has an additional interface for raw bytes: mpz_out_raw()

http://gmplib.org/manual/Function-Index.html

Am I missing something? Does anyone know of another library that can serialize GMP floats? Does anyone know of another bignum lib that offers robust serialization?

Edit: I'd be happy with serializing MPFR's mpfr_t, as well, which similarly only seems to offer string output: http://www.mpfr.org/mpfr-current/mpfr.html#Function-Index


回答1:


This was a long time ago, but I wound up doing something like this:

int mpf_out_raw (FILE *f, mpf_t X) {
   int expt; mpz_t Z; size_t nz;
   expt = X->_mp_exp;
   fwrite(&expt, sizeof(int), 1, f);
   nz = X->_mp_size;
   Z->_mp_alloc = nz; 
   Z->_mp_size  = nz; 
   Z->_mp_d     = X->_mp_d;
   return (mpz_out_raw(f, Z) + sizeof(int));
}

void mpf_inp_raw  (FILE *f, mpf_t X) { 
   int expt; mpz_t Z; size_t nz;
   mpz_init (Z);
   fread(&expt, sizeof(int), 1, f);
   mpz_inp_raw  (Z, f);
   mpf_set_z    (X, Z); 
   X->_mp_exp   = expt;
   mpz_clear (Z);
}



回答2:


You can do input and output of the bytes in GMP floats. The manual page on I/O of Floats lists the following functions:

size_t mpf_out_str (FILE *stream, int base, size_t n_digits, mpf_t op)
size_t mpf_inp_str (mpf_t rop, FILE *stream, int base)

The confusing part about the manual is that it lists the I/O routines for the different types in different sections.

Edit: I was totally wrong! These functions are doing string conversion, not outputting the raw bytes.



来源:https://stackoverflow.com/questions/3318979/how-to-serialize-the-gmp-mpf-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!