mpf_class to double

Paul Zimmermann Paul.Zimmermann at loria.fr
Sat Sep 4 14:48:59 CEST 2010


       Andreas,

> And what I would like to do is something like this (in some sort of 
> pseudo code):
> 
> void calc_sum (double *a, double *result, int l)
> {
>      // convert input values to high precision types... in whatever way...
>      mpf_class mpa[l];
>      mpf_class mpresult = 0.0;
> 
>      for(int i=0; i<l; i++)
>          mpa[i]= a[i];
> 
> 
>      // evaluate sum with mpf types
>      for(int i=0; i<l; i++)
>          mpresult += mpa[i];
> 
> 
>      // convert result back to double
>      *result = mpf_to_double( mpresult );
> }
> 
> 
> Right now this fails mainly because I don't know how to do the 
> back-conversion from mpf to double.

see the mpf_get_d() function.

> But I would also like to know if this approach is intelligent at all. Or 
> would there be a nicer/better way to do this?

this approach seems good to me. Don't forget to set the precision of the mpf
numbers to a large value (I don't know how to do this with the C++ interface).

Paul Zimmermann


More information about the gmp-discuss mailing list