mpf_class to double
Marc Glisse
marc.glisse at inria.fr
Sat Sep 4 17:50:08 CEST 2010
On Sat, 4 Sep 2010, Paul Zimmermann wrote:
> 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];
You don't need to store them in an array, you can convert them on the fly,
always reusing the same mpf_class object, or you can let mpresult+=a[i] do
the conversion implicitly for you.
>> 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).
With the C++ interface, assuming you have a mpf_class f, you could write
f.get_mpf_t() and use the C functions on it, or directly use the C++
versions, which have the same name without the mpf_ prefix, i.e.
f.get_d(), f.set_prec(n), etc.
--
Marc Glisse
More information about the gmp-discuss
mailing list