forum.alglib.net
http://forum.alglib.net/

C++ compilers and precision of FP calculations
http://forum.alglib.net/viewtopic.php?f=2&t=331
Page 1 of 1

Author:  jambulat [ Fri Apr 01, 2011 3:32 am ]
Post subject:  C++ compilers and precision of FP calculations

Hello!
I've occurred some interesting problem when the output precision of the numerical algorithm isn't dependent on its epsilon parameter. Please, look at my simple example where I'm trying to calculate derivative of x^3/3 function at x=1:
Code:
#include <math.h>
#include "stdio.h"

double f(double x)
{
   return x*x*x/3;
}

double diff_f(double x)
{
   const double eps = 1e-45;
   double dx = 1e-4;
   double fp_prev, fp_curr;

   fp_curr = (f(x+dx) - f(x))/dx;

   do{
      dx /= 2;
      fp_prev = fp_curr;
      fp_curr = (f(x+dx) - f(x))/dx;   
   } while(abs(fp_curr-fp_prev) > eps);

   return fp_curr;
}

int main()
{
   double x = 1;
   printf("f'(%.2f)-1=%e\n",x,diff_f(x)-1);
   printf("Press enter...");
   getchar();
   return 0;
}

On the output program prints calculated value f'(x=1) minus its real value (1). And here one can assure that the output will be the same for
eps = 1e-15; and eps = 1e-25:
Code:
f'(1.00)-1=4.749745e-008


But if we use long double instead double, difference for eps=1e-14 will be ~1e-317. So it would be nice to have a possibility to change capacity of double to long double but without changing whole cpp project, it may be big enough, like ALGLIB for example. Is there some solution for this?

Author:  jambulat [ Fri Apr 01, 2011 10:59 am ]
Post subject:  Re: C++ compilers and precision of FP calculations

I changed in whole alglib-3.1.0 double to long double, and in my project too. They are even compiled and linked OK after all. But there is some run-time error when binary is run. Here is archive with modified alglib and program using it that demonstrates run-time error:
http://zalil.ru/30780055/1e7d0478.4d965 ... double.zip
Compiled for ia32 with Intel C++ compiler with key /Qlong_double

It would be nice if we could make ALGLIB work with long double accuracy...

Author:  Sergey.Bochkanov [ Mon Apr 04, 2011 6:14 am ]
Post subject:  Re: C++ compilers and precision of FP calculations

About your original post first - introduction of long double haven't increased your precision from 1.0E-16 to 1e-317. It just become something about 1E-20. The fact that it seemed to work better in one particular case is just a lucky coincidence.

Second, amongst all platforms only Intel has extended 80-bit floating point numbers. ARM, SPARC and others do not have long doubles. So it is not good idea to use long double, especially when we know that even under Windows size of this type depends on particular OS (32/64), compiler (GSS/MSVC/ICC) and its switches.

Author:  jambulat [ Mon Apr 04, 2011 9:13 am ]
Post subject:  Re: C++ compilers and precision of FP calculations

Quote:
About your original post first - introduction of long double haven't increased your precision from 1.0E-16 to 1e-317. It just become something about 1E-20. The fact that it seemed to work better in one particular case is just a lucky coincidence.


You are right... It's because the printf() function can't handle correct long double arguments. And if we change that code as follows:
Code:
printf("f'(%.2f)-1=%e\n",(double)x,(double)(diff_f(x)-1));

we obtain the same difference delta=8.74e-8 as in double precision case. I think that means that even with using Intel c++ complier with \Qlong_double key, arithmetical operations are performed with double precision.

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/