On Thu, 14 Nov 2024 at 16:28, Dmitry Koval <d.ko...@postgrespro.ru> wrote: > > I think having gamma() and lgamma() functions in PostgreSQL would be > useful for some users, this is asked [1]. >
Thanks for looking. > >SELECT gamma(float8 '1e-320'); > ERROR: value out of range: overflow > > >SELECT gamma(float8 '0'); > gamma > ---------- > Infinity > (1 row) > That's correct since gamma(1e-320) is roughly 1e320, which overflows the double precision type, but it's not actually infinite, whereas gamma(0) is infinite. > Perhaps it would be logical if the behavior in these cases was the same > (either ERROR or 'Infinity')? > In general, I think that having gamma() throw overflow errors is useful for spotting real problems in user code. In my experience, it's uncommon to pass negative or even close-to-zero inputs to gamma(), but it is common to fail to appreciate just how quickly the result grows for positive inputs (it overflows for inputs just over 171.6), so it seems better to be made aware of such problems (which might be solved by using lgamma() instead). So I think throwing overflow errors is the most useful thing to do from a practical point of view, and if we do that for too-large inputs, it makes sense to do the same for too-close-to-zero inputs. OTOH, gamma(+/-0) = +/-Infinity is right from a mathematical point of view, and consistent with the POSIX spec, and it's also consistent with the functions cot() and cotd(). Regards, Dean