Storing and retrieving mrgrynch at
Sat Jul 2 17:33:22 CEST 2005

Thanks for the information!  I am currently already using the C++ wrappers that come with GMP.  So far, I've wirtten the code to stream these text values to a file.  The question arises when I read them back...

Can I just read the value in and set an mpf_class to that string value, and be comfortable that the conversion will work?  The documentation for this is a bit confusing for me, as it says...

int mpf_set_str (mpf t rop, char *str, int base) [Function]
Set the value of rop from the string in str. The string is of the form `M at N' or...., if the base is 10 or less, alternatively `MeN'.
Assuming I am using base 10, then I believe what this means is I can just call this method with a number like "-3.17335744356735673e-14", and it will properly set the destination object to that value, assuming a suitable precision level of the destination structure. 
Am I correct in this?  If I am, then I think I've all I need to do what I need.

-------------- Original message -------------- 

> At 03:53 +0000 2005/07/02, mrgrynch at wrote: 
> >I have a need to preserve a bunch of GMP floating point values (from 
> >an array of mpf_class objects) for retrieval at a later time. The 
> >experiement I am running will take about a year, and I'd hate to 
> >lose it all if the power goes out, etc... 
> > 
> > I was thinking the easiest way to do this is with strings. Has 
> >anyone done this before? 
> One can write binary data to a file and read it back, but I gather if 
> the core parts that the GMP relies on would somehow change, it would 
> make the file binary data unreadable. So the safest way, though 
> slower, is to write it out as text, and read it back again. You are 
> then embarking, in its extension, on the path of making two programs, 
> a parser that can read this kind of data, and an expresser, or 
> "pretty-printer", that can write out it. Ultimately, if you develop a 
> computer language, you may need the use of tools such as the lexer 
> generator Flex and the parser generator Bison. 
> But start simply, expand at need. 
> >Reading through the documentation, I'm not entirely sure about the 
> >argument list to be passed to the functions which handle this, nor 
> >how they would work through the C++ class interface, which I am 
> >using. Would I have to extract a reference to the underlying C 
> >structure before converting to and from strings? 
> GMP has C read and write functions, and further, some C++ support. I 
> haven't used the latter, as I wrote my own C++ wrap. I can send it to 
> you by private mail, if you so like. 
> The major drawback of my C++ wrap is that I had my own version of the 
> GMP read function. So it means that if GMP changes that, I will have 
> to do some rewriting. But this part is there only to make a C++ nice 
> istream interface; you could use something more version-stable, if 
> you like. Otherwise, it is just a C++ interface on top of C, using 
> reference counts to avoid unneeded recopying. 
> -- 
> Hans Aberg 
-------------- next part --------------
An HTML attachment was scrubbed...

More information about the gmp-discuss mailing list