Re: factorial of negative numbers

Peter Eisentraut <peter.eisentraut@2ndquadrant.com>

From: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
To: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
Cc: Dean Rasheed <dean.a.rasheed@gmail.com>, Bruce Momjian <bruce@momjian.us>, Ashutosh Bapat <ashutosh.bapat@2ndquadrant.com>, pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2020-06-18T11:57:49Z
Lists: pgsql-hackers
On 2020-06-18 09:43, Juan José Santamaría Flecha wrote:
> 
> On Thu, Jun 18, 2020 at 9:13 AM Peter Eisentraut 
> <peter.eisentraut@2ndquadrant.com 
> <mailto:peter.eisentraut@2ndquadrant.com>> wrote:
> 
>     On 2020-06-16 14:17, Dean Rasheed wrote:
>      > I think you're probably right though. Raising an out-of-range error
>      > seems like the best option.
> 
>     committed as proposed then
> 
> 
> The gamma function from math.h returns a NaN for negative integer 
> values, the postgres factorial function returns a numeric, which allows 
> NaN. Raising an out-of-range error seems only reasonable for an integer 
> output.

But this is not the gamma function.  The gamma function is undefined at 
zero, but factorial(0) returns 1.  So this is similar but not the same.

Moreover, functions such as log() also error out on unsupportable input 
values, so it's consistent with the spec.

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services



Commits

  1. Disallow factorial of negative numbers

  2. Expand tests for factorial

  3. doc: Document factorial function

  4. Attached is a patch implementing factorial(), returning numeric. Points