Re: gamma() and lgamma() functions

Dmitry Koval <d.koval@postgrespro.ru>

From: Dmitry Koval <d.koval@postgrespro.ru>
To: pgsql-hackers@lists.postgresql.org
Date: 2024-11-14T16:28:27Z
Lists: pgsql-hackers
Hi!

I think having gamma() and lgamma() functions in PostgreSQL would be 
useful for some users, this is asked [1].

I have a question regarding the current implementation of gamma() 
function. Code block:

+	if (errno == ERANGE && arg1 != 0)
+	{
+		if (result != 0.0)
+			float_overflow_error();
+		else
+			float_underflow_error();
+	}
+	else if (isinf(result) && arg1 != 0 && !isinf(arg1))
+		float_overflow_error();
+	else if (result == 0.0)
+		float_underflow_error();

Why in some cases (if arg1 is close to 0, but not 0) an error 
(float_overflow_error) will be returned, but in the case of "arg1 = 0" 
the value 'Infinity' will be returned?
For example:

 >SELECT gamma(float8 '1e-320');
ERROR:  value out of range: overflow

 >SELECT gamma(float8 '0');
   gamma
----------
  Infinity
(1 row)

Perhaps it would be logical if the behavior in these cases was the same 
(either ERROR or 'Infinity')?

Links:
[1] 
https://stackoverflow.com/questions/58884066/how-can-i-run-the-equivalent-of-excels-gammaln-function-in-postgres

-- 
With best regards,
Dmitry Koval

Postgres Professional: http://postgrespro.com