Return-Path: X-Original-To: apmail-commons-dev-archive@www.apache.org Delivered-To: apmail-commons-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9F1E5D4EA for ; Thu, 23 Aug 2012 19:09:02 +0000 (UTC) Received: (qmail 16913 invoked by uid 500); 23 Aug 2012 19:09:02 -0000 Delivered-To: apmail-commons-dev-archive@commons.apache.org Received: (qmail 16827 invoked by uid 500); 23 Aug 2012 19:09:02 -0000 Mailing-List: contact dev-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Developers List" Delivered-To: mailing list dev@commons.apache.org Received: (qmail 16818 invoked by uid 99); 23 Aug 2012 19:09:02 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 23 Aug 2012 19:09:02 +0000 X-ASF-Spam-Status: No, hits=-2.3 required=5.0 tests=RCVD_IN_DNSWL_MED,SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of SRS0=tD/E=GX=m4x.org=sebastien.brisard@bounces.m4x.org designates 129.104.30.34 as permitted sender) Received: from [129.104.30.34] (HELO mx1.polytechnique.org) (129.104.30.34) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 23 Aug 2012 19:08:54 +0000 Received: from mail-ob0-f171.google.com (mail-ob0-f171.google.com [209.85.214.171]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by ssl.polytechnique.org (Postfix) with ESMTPSA id 9534E14078166 for ; Thu, 23 Aug 2012 21:08:32 +0200 (CEST) Received: by obqv19 with SMTP id v19so769988obq.30 for ; Thu, 23 Aug 2012 12:08:31 -0700 (PDT) MIME-Version: 1.0 Received: by 10.50.87.165 with SMTP id az5mr2743367igb.60.1345748911018; Thu, 23 Aug 2012 12:08:31 -0700 (PDT) Received: by 10.64.15.201 with HTTP; Thu, 23 Aug 2012 12:08:30 -0700 (PDT) In-Reply-To: <50367DE8.20909@gmail.com> References: <5035DEE0.6000705@free.fr> <20120823102450.GG20488@dusk.harfang.homelinux.org> <20120823113754.GH20488@dusk.harfang.homelinux.org> <50362413.8070205@free.fr> <50367DE8.20909@gmail.com> Date: Thu, 23 Aug 2012 21:08:30 +0200 Message-ID: Subject: Re: [math] UnexpectedNegativeIntegerException From: =?ISO-8859-1?Q?S=E9bastien_Brisard?= To: Commons Developers List Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-AV-Checked: ClamAV using ClamSMTP at svoboda.polytechnique.org (Thu Aug 23 21:08:33 2012 +0200 (CEST)) X-Org-Mail: sebastien.brisard.1997@polytechnique.org X-Old-Spam-Flag: No, tests=bogofilter, spamicity=0.064693, queueID=0209114078167 Hi, thanks for all these answers. 2012/8/23 Phil Steitz : > On 8/23/12 5:37 AM, Luc Maisonobe wrote: >> Le 23/08/2012 13:37, Gilles Sadowski a =E9crit : >>> On Thu, Aug 23, 2012 at 12:35:18PM +0200, S=E9bastien Brisard wrote: >>>> Hi Gilles, >>>> >>>> 2012/8/23 Gilles Sadowski : >>>>> On Thu, Aug 23, 2012 at 10:05:10AM +0200, S=E9bastien Brisard wrote: >>>>>> Hi Luc, >>>>>> >>>>>> 2012/8/23 Luc Maisonobe : >>>>>>> Le 23/08/2012 05:16, S=E9bastien Brisard a =E9crit : >>>>>>>> Hi, >>>>>>>> in MATH-849, I have proposed an implementation of Gamma(x) >>>>>>>> (previously, class Gamma had only logGamma(x)). Gamma(x) is not >>>>>>>> defined for x negative integer. In such instances, I would like to >>>>>>>> throw an exception instead of returning Double.NaN. However, creat= ing >>>>>>>> a new exception in o.a.c.m.exception seems exagerated, since it's = very >>>>>>>> unlikely that this exception should be used elsewhere (or maybe). >>>>>>>> Should I define a nested exception instead [1]? >>>>>>>> >>>>>>>> What do you think of the name "UnexpectedNegativeIntegerException"= ? It >>>>>>>> does not really match the pattern of already defined exceptions, b= ut I >>>>>>>> can't find a better name. >>>>>>> Don't we already have NotPositiveException? >>>>>>> >>>>>>> Luc >>>>>>> >>>>>> We do, but Gamma is defined for all negative values, except integer = ones... >>>>> I think that in some circumstances, it might be useful to not throw >>>>> exceptions... >>>>> FastMath's "log" returns NaN for negative input. >>>>> >>>> then I guess that logGamma(x) should also return NaN if x <=3D 0? >>> Anyways, that's what it does currently. >>> >>>> I have to say I do not really like this option. >>> So did you intend to change that? >>> >>>> My life would >>>> sometimes be much easier if NaNs didn't exist... the good old days of >>>> the "floating-point error". > > We have different memories - I am old enough to remember wasting > real money due to jobs failing on "floating point checks" when I > would have preferred to have computations complete and return NaN > (which had not been invented yet). > >>> IIRC NaN could be useful for example in an optimization algorithm; exce= rpt >>> from Kahan: >>> --- >>> [...] NaNs is an opportunity ( not obligation ) for software ( especial= ly >>> when searching ) to follow an unexceptional path ( no need for exotic >>> control structures ) to a point where an exceptional event can be appra= ised >>> after the event, when additional evidence may have accrued. [...] >>> --- >>> >>> I do not say that Commons Math should prefer NaN over throwing exceptio= ns. >> If the choice is allowed would really prefer NaN for such cases. > > +1 >> >>> Maybe that it depends on how high-level an application is (i.e. if ther= e are >>> already calls to methods that could throw exceptions, then an algorithm= that >>> would not use try/catch to protect itself would fail anyway). >>> If we want Commons Math to allow taking advantage of NaNs, it would pro= bably >>> need to be updated so that a lot of precondition checks ought to be rem= oved >>> (but this will likely lead to reduced robustness in some applications t= hat >>> do not do their own checks...). >> This would clearly be cumbersome for users. >> Since we have changed our exception hierarchy, we don't have a single >> root anymore, so users simply cannot catch all exception we throw at >> once, they have to check every single type, and make sure they are >> thrown by themselves without any help from compiler. >> >> Just adding new exceptions is too much, we have already gone too far >> this way. > > +1 and the fact that all exceptions are unchecked makes it even > harder for client apps as the compiler will not help / force them. > There is a reason that NaNs exist. It is much cheaper to return a > NaN than to raise (and force the client to handle) an exception. > This is not precise and probably can't be made so, but I have always > looked at things more or less like this: > > 0) IAE (which I see no need to specialize as elaborately as we have > done in [math]) is for clear violation of the documented API > contract. The actual parameters "don't make sense" in the context > of the API. > 1) NaN can be returned as the result of a computation which, when > started with legitimate arguments, does not result in a > representable value. > > The problem is that contracts can often be written so that instances > of 1) are turned into instances of 0). Gamma(-) is a great > example. The singularities at negative integers could be viewed as > making negative integer arguments "illegal" or "nonsense" from the > API standpoint, or legitimate arguments for which no well-defined, > representable value can be returned. Personally, I would prefer to > get NaN back from this function and just point out the existence of > the singularities in the javadoc. > > Phil > > > > >> >> best regards, >> Luc >> >> >>> In the absence of strong arguments, the choice should probably match th= e >>> usage of current Commons Math's users. [This is an issue somewhat simil= ar to >>> the one that led to stop using checked exceptions.] >>> >>> >>> Best regards, >>> Gilles >>> >>> P.S. How do you test that the input is a negative integer? >>> >>> --------------------------------------------------------------------- >>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org >>> For additional commands, e-mail: dev-help@commons.apache.org >>> >>> >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org >> For additional commands, e-mail: dev-help@commons.apache.org >> >> > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org > For additional commands, e-mail: dev-help@commons.apache.org > So, NaN it is! And I'll keep the present behavior forGamma.logGamma(x): return NaN for x <=3D 0. S=E9bastien --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org For additional commands, e-mail: dev-help@commons.apache.org