forum.alglib.net

ALGLIB forum
It is currently Sun Dec 22, 2024 8:49 am

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: C++ compilers and precision of FP calculations
PostPosted: Fri Apr 01, 2011 3:32 am 
Offline

Joined: Mon Nov 22, 2010 11:19 am
Posts: 10
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?


Top
 Profile  
 
 Post subject: Re: C++ compilers and precision of FP calculations
PostPosted: Fri Apr 01, 2011 10:59 am 
Offline

Joined: Mon Nov 22, 2010 11:19 am
Posts: 10
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...


Top
 Profile  
 
 Post subject: Re: C++ compilers and precision of FP calculations
PostPosted: Mon Apr 04, 2011 6:14 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
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.


Top
 Profile  
 
 Post subject: Re: C++ compilers and precision of FP calculations
PostPosted: Mon Apr 04, 2011 9:13 am 
Offline

Joined: Mon Nov 22, 2010 11:19 am
Posts: 10
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 37 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group