From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on ip-172-31-74-118.ec2.internal X-Spam-Level: * X-Spam-Status: No, score=1.8 required=3.0 tests=BAYES_50,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Received: by 2002:ac8:322b:: with SMTP id x40mr20181213qta.55.1593840652706; Fri, 03 Jul 2020 22:30:52 -0700 (PDT) X-Received: by 2002:aca:e188:: with SMTP id y130mr12237804oig.110.1593840652458; Fri, 03 Jul 2020 22:30:52 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!news.gegeweb.eu!gegeweb.org!usenet-fr.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Fri, 3 Jul 2020 22:30:52 -0700 (PDT) Complaints-To: groups-abuse@google.com Injection-Info: google-groups.googlegroups.com; posting-host=93.41.2.250; posting-account=9fwclgkAAAD6oQ5usUYhee1l39geVY99 NNTP-Posting-Host: 93.41.2.250 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Question about best practices with numerical functions From: mockturtle Injection-Date: Sat, 04 Jul 2020 05:30:52 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Xref: reader01.eternal-september.org comp.lang.ada:59333 List-Id: Dear.all,=20 I have a question about the best way to manage a potential loss of precisio= n in a numerical function. This is a doubt that came to my mind while writ= ing a piece of software; now I solved the specific problem, but the curiosi= ty 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)=3Dy). This function c= annot be expressed with elementary functions and the algorithm I found basi= cally solves the equation in an iterative way. Of course, if you fix the m= aximum 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. =20 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 i= n many different programs. If the function was specific for a single progr= am, 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 a= ny value of interest; nevertheless, the curiosity remains...) I can see few line of actions that would make sense [1] Raise an exception. =20 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 d= ecimal digits) =20 [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 :=3D Lambert(Y); you would say X :=3D 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 a= nd, moreover, it supposes there is some logging facilities or that the sta= ndard error is available for logging... Remember that the function should b= e general, to be used in any program. [5] ???=20 Any suggestions? Thank you in advance Riccardo