Re: gamma() and lgamma() functions

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Peter Eisentraut <peter(at)eisentraut(dot)org>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: gamma() and lgamma() functions
Date: 2024-09-06 09:42:57
Message-ID: CAEZATCX5Bj6M=RACfyF973SiDim7iqk_JgEuOqkLQZccZ+=5Tg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, 4 Sept 2024 at 19:21, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> >> I'm not sure why you are doing the testing for special values (NaN etc.)
> >> yourself when the C library function already does it. For example, if I
> >> remove the isnan(arg1) check in your dlgamma(), then it still behaves
> >> the same in all tests.
>
> > It's useful to do that so that we don't need to assume that every
> > platform conforms to the POSIX standard, and because it simplifies the
> > later overflow checks. This is consistent with the approach taken in
> > other functions, such as dexp(), dsin(), dcos(), etc.
>
> dexp() and those other functions date from the late stone age, before
> it was safe to assume that libm's behavior matched the POSIX specs.
> Today I think we can assume that, at least till proven differently.
> There's not necessarily anything wrong with what you've coded, but
> I don't buy this argument for it.
>

OK, thinking about this some more, I think we should reserve overflow
errors for genuine overflows, which I take to mean cases where the
exact mathematical result should be finite, but is too large to be
represented in a double.

In particular, this means that zero and negative integer inputs are
not genuine overflows, but should return NaN or +/-Inf, as described
in the POSIX spec.

Doing that, and assuming that tgamma() and lgamma() behave according
to spec, leads to the attached, somewhat simpler patch.

Regards,
Dean

Attachment Content-Type Size
v3-gamma-and-lgamma.patch text/x-patch 8.0 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Richard Guo 2024-09-06 09:51:05 Re: On disable_cost
Previous Message Daniel Gustafsson 2024-09-06 09:42:20 Re: Typos in the code and README