mpf_class to double
Andreas Puettmann
andreas.puettmann at gmx.de
Sat Sep 4 09:21:01 CEST 2010
Hi everyone!
I am absolutely new to GMP, so please excuse me if my question is a
pushover.
I have a scientific simulation code which as a whole works fine (using
only C standard types, i.e. doubles). However, there is a small fragment
of it which somehow seems to produce "random" results. As this part
consists basically of a large sum (or difference) of doubles of
arbitrary magnitude, one can assume that this behavior is due to loss of
significance here. To substantiate this suspicion, my thought was to do
this summation with mpf_class data types at very high precision.
Reduced to its core, the code fragment looks similar to this:
void calc_sum (double *a, double *result, int l)
{
*result = 0.0;
for(int i=0; i<l; i++)
*result += a[i];
}
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.
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?
Remember, I really do not want to convert the whole code to mpf types as
I am pretty sure that problems only originate from this fragment.
Any hints would be much appreciated!
Thanks in advance
Andreas
More information about the gmp-discuss
mailing list