comp.lang.ada
 help / color / mirror / Atom feed
From: mockturtle <framefritti@gmail.com>
Subject: Question about best practices with numerical functions
Date: Fri, 3 Jul 2020 22:30:52 -0700 (PDT)	[thread overview]
Message-ID: <fa2c6030-8bed-44c6-b5ed-0e24495a5085o@googlegroups.com> (raw)

Dear.all, 
I have a question about the best way to manage a potential loss of precision in a numerical function.  This is a doubt that came to my mind while writing a piece of software; now I solved the specific problem, but the curiosity remains.

Let me explain.

Recently I needed to write an implementation of the Lambert W function (is the function that given y finds x such that x*exp(x)=y).  This function cannot be expressed with elementary functions and the algorithm I found basically solves the equation in an iterative way.  Of course, if you fix the maximum number of iterations, it can happen that the convergence is not fast enough and you obtain a result that is potentially less precise than what you would expect.  

I was wondering how to manage such a non convergence case. Please note that I am supposing that I am writing a "general" function that could be used in many different programs.  If the function was specific for a single program, then I would choose the line of action (e.g., ignore, log a warning or raise an exception) depending on the needs of the specific program.

(Incidentally, it turned out that the implementation converges nicely for any value of interest; nevertheless, the curiosity remains...)

I can see few line of actions that would make sense

   [1] Raise an exception.  

Maybe this is a bit too drastic since there are cases where a moderate loss of precision does not matter (this was my case, i just needed one or two decimal digits)     

   [2] Let the function have an optional "precision" parameter and raise an exception if the precision goes below that

   [3] Let the function return a record with a field Value with the actual result and a field Error with the estimated precision.

This would make the code a bit heavier since instead of calling

X := Lambert(Y);

you would say

X := Lambert(Y).Value;

Not really a huge deal, however...

   [4] Print a warning message to standard error or some logging system and go on.

This sounds like the worst option to me.  The message could be overlooked and, moreover, it supposes there is some logging facilities or that  the standard error is available for logging... Remember that the function should be general, to be used in any program.

   [5] ??? 

Any suggestions?

Thank you in advance

Riccardo

             reply	other threads:[~2020-07-04  5:30 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-04  5:30 mockturtle [this message]
2020-07-04  7:50 ` Question about best practices with numerical functions Dmitry A. Kazakov
2020-07-04 10:45 ` Nasser M. Abbasi
2020-07-06 17:02 ` Shark8
2020-07-06 22:23   ` Dmitry A. Kazakov
2020-07-07  4:52     ` mockturtle
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox