Improvements for the code for the Lanczos method.
Dear all, I now implemented the improvements to the code for the Lanczos method as have been discussed during December 2006. You find them in CVS. Best wishes for the not-so-new-anymore year, Chris
Hi, Chris!
if (coeffs_12.size() != 12) { std::cerr << "Fatal: array of size 12 does not have size 12" << std::endl; exit(1); }
First of all, libraries should not be spamming std{out,err}. Secondly, exit(1) makes debugging unnecessary complicated (I really, _really_ hate libraries which exit() on error!). Last, you should have #include'ed the header <cstdlib> where exit is declared. I suggest to remove all that cruft and use exceptions instead. Best regards, Alexei --- ginac/numeric.cpp | 28 +++++++++++----------------- 1 files changed, 11 insertions(+), 17 deletions(-) diff --git a/ginac/numeric.cpp b/ginac/numeric.cpp index eed6624..55b3b53 100644 --- a/ginac/numeric.cpp +++ b/ginac/numeric.cpp @@ -1670,10 +1670,9 @@ lanczos_coeffs::lanczos_coeffs() coeffs_12.push_back("13.80379833961490898061357227729422691903"); coeffs_12.push_back("-0.0807817619724537563116612761921260762075"); coeffs_12.push_back("3.47974801622326717770813986587340515986E-5"); - if (coeffs_12.size() != 12) { - std::cerr << "Fatal: array of size 12 does not have size 12" << std::endl; - exit(1); - } + if (coeffs_12.size() != 12) + throw std::logic_error("Fatal: array of size 12 does not have size 12"); + std::vector<cln::cl_N> &coeffs_30 = coeffs[1]; coeffs_30.reserve(30); /* thirty coefficients follow. */ @@ -1707,10 +1706,9 @@ lanczos_coeffs::lanczos_coeffs() coeffs_30.push_back("8.5728436055212340846907439451102962820713733082683634385104363203776378266115E-12"); coeffs_30.push_back("-3.9175430218003196379961975369936752665267219444417121562332986822123821080906E-17"); coeffs_30.push_back("1.06841715008998384033789050831892757796251622802680860264598247667384268519263E-24"); - if (coeffs_30.size() != 30) { - std::cerr << "Fatal: array of size 30 does not have size 30" << std::endl; - exit(1); - } + if (coeffs_30.size() != 30) + throw std::logic_error("Fatal: array of size 30 does not have size 30"); + std::vector<cln::cl_N> &coeffs_60 = coeffs[2]; coeffs_60.reserve(60); /* sixty coefficients follow. */ @@ -1774,10 +1772,9 @@ lanczos_coeffs::lanczos_coeffs() coeffs_60.push_back("1.022249951013180267209479446016461291488484443236553319305574600271584296178678167457933405768832443689762998392188667506451117069946568E-43"); coeffs_60.push_back("-1.158776990252157075591666544736990249102708476419363164106801472497162421792350234416969073422311477683246469337273059290064112071625785E-47"); coeffs_60.push_back("4.27222387142756413870104074160770434521893587460314314301300261552300727494374933435001642531897059406263033431558827297492879960920275E-49"); - if (coeffs_60.size() != 60) { - std::cerr << "Fatal: array of size 60 does not have size 60" << std::endl; - exit(1); - } + if (coeffs_60.size() != 60) + throw std::logic_error("Fatal: array of size 60 does not have size 60"); + std::vector<cln::cl_N> &coeffs_120 = coeffs[3]; coeffs_120.reserve(120); /* 120 coefficients follow. */ @@ -1901,11 +1898,8 @@ lanczos_coeffs::lanczos_coeffs() coeffs_120.push_back("4.5681983751743456413033268196376305093509590040595182930261094908859252761697530924655649930852283295534503341542929581967081012867692190108698698006237799801339418962091877730207560007839789937153876806052229193448161273005984514504886230869730232561E-94"); coeffs_120.push_back("-1.5943139155457706045530478744891549581317663177038648406493256399589001327414318955746453934207742828511041930090849236963271943244329753764497401819704943705370596846318480510254313447057477914171472190541408193443142906466279172123681623644325254209E-95"); coeffs_120.push_back("2.7319125666863032595604997603472305262880292377469053594326527505796348018540179196191192420176181194669607935656210005192217186286873953583571180312679155204061051208771126804209623533044988888808754656646355388901404252058383561064953226611421609762E-97"); - if (coeffs_120.size() != 120) { - std::cerr << "Fatal: array of size 120 does not have size 120" - << std::endl; - exit(1); - } + if (coeffs_120.size() != 120) + throw std::runtime_error("Fatal: array of size 120 does not have size 120"); } -- 1.4.4.3 -- All science is either physics or stamp collecting.
Dear Alexei, On Thu, 18 Jan 2007, Sheplyakov Alexei wrote:
First of all, libraries should not be spamming std{out,err}.
Surely they should not be "spamming" it. However, this code is mainly present to guard the code integrity. I.e., preventing that someone is going to unwittingly delete some line, or some such thing. So it really should never be executed.
Secondly, exit(1) makes debugging unnecessary complicated (I really, _really_ hate libraries which exit() on error!).
Same as before, this code really, _really_, should never be executed in the first place.
Last, you should have #include'ed the header <cstdlib> where exit is declared.
Okay, I'll include cstdlib then.
I suggest to remove all that cruft and use exceptions instead.
I do not see what benefit exceptions bring to this situation. It could only force the compiler to insert error handling code all over the place where really none is needed. Noone should be catching this exit. Instead, if it is called, the code should be fixed. Best wishes, Chris
Hello! On Wed, Jan 24, 2007 at 12:50:21PM +0100, Chris Dams wrote:
First of all, libraries should not be spamming std{out,err}.
Surely they should not be "spamming" it. However, this code is mainly present to guard the code integrity.
Code integrity is not a good excuse for doing weird annoying things.
I.e., preventing that someone is going to unwittingly delete some line, or some such thing.
Yep, some badly designed software used to annoy people with silly questions ("Are you really sure you want to run this program?") and/or error messages("Fatal error: success"). I don't think GiNaC should do such things. (I admit that my patch does not make the error message better, but at least it allows user/application to ignore it).
So it really should never be executed.
In reality such "this should never happen" code does get executed, for instance: "Internal error: statement in file ./real/conv/cl_R_to_double.cc, line 55 has been reached!! Please send the authors of the program a description how you produced this error!"
I do not see what benefit exceptions bring to this situation.
Consider a program which uses GiNaC as an optional add-on, e.g., by dlopen()'ing it (or some wrapper library). For such a program libginac's initialization failure may not be a fatal error. If GiNaC signals such a failure with exit() the program has no chance to handle the error in a graceful manner. On the other hand, it is easy to catch an exception and take some sensible action (do the whole calculation numerically, or at least save the data before bailing out, etc). exit() from a library is EVIL.
It could only force the compiler to insert error handling code all over the place where really none is needed.
OK, let's get rid of this check at all.
Noone should be catching this exit.
I don't agree with you (see above).
Instead, if it is called, the code should be fixed.
Sure. But exit() is not really debugger-friendly anyway. Best regards, Alexei -- All science is either physics or stamp collecting.
participants (2)
-
Chris Dams
-
varg@theor.jinr.ru