Re: factorial of negative numbers

2020-06-18 Thread Juan José Santamaría Flecha
On Thu, Jun 18, 2020 at 1:57 PM Peter Eisentraut < peter.eisentr...@2ndquadrant.com> wrote: > On 2020-06-18 09:43, Juan José Santamaría Flecha wrote: > > The gamma function from math.h returns a NaN for negative integer > > values, the postgres factorial function returns a numeric, which allows >

Re: factorial of negative numbers

2020-06-18 Thread Peter Eisentraut
On 2020-06-18 09:43, Juan José Santamaría Flecha wrote: On Thu, Jun 18, 2020 at 9:13 AM Peter Eisentraut > 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

Re: factorial of negative numbers

2020-06-18 Thread Juan José Santamaría Flecha
On Thu, Jun 18, 2020 at 9:13 AM Peter Eisentraut < peter.eisentr...@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 m

Re: factorial of negative numbers

2020-06-18 Thread Peter Eisentraut
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 -- Peter Eisentraut http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Re: factorial of negative numbers

2020-06-16 Thread Dean Rasheed
On Tue, 16 Jun 2020 at 12:18, Peter Eisentraut wrote: > > On 2020-06-16 11:49, Dean Rasheed wrote: > > With [1], we could return 'Infinity', which would be more correct from > > a mathematical point of view, and might be preferable to erroring-out > > in some contexts. > > But the limit of the gam

Re: factorial of negative numbers

2020-06-16 Thread Peter Eisentraut
On 2020-06-16 11:49, Dean Rasheed wrote: With [1], we could return 'Infinity', which would be more correct from a mathematical point of view, and might be preferable to erroring-out in some contexts. But the limit of the gamma function is either negative or positive infinity, depending on from

Re: factorial of negative numbers

2020-06-16 Thread Juan José Santamaría Flecha
On Tue, Jun 16, 2020 at 11:50 AM Dean Rasheed wrote: > On Tue, 16 Jun 2020 at 10:09, Juan José Santamaría Flecha > wrote: > > > > It is defined as NaN (or undefined), which is not in the realm of > integer numbers. You might get a clear idea of the logic from [1], where > they also make a case f

Re: factorial of negative numbers

2020-06-16 Thread Dean Rasheed
On Tue, 16 Jun 2020 at 10:09, Juan José Santamaría Flecha wrote: > > It is defined as NaN (or undefined), which is not in the realm of integer > numbers. You might get a clear idea of the logic from [1], where they also > make a case for the error being ERRCODE_DIVISION_BY_ZERO. > > [1] http://m

Re: factorial of negative numbers

2020-06-16 Thread Dean Rasheed
On Tue, 16 Jun 2020 at 09:55, Bruce Momjian wrote: > > On Tue, Jun 16, 2020 at 08:31:21AM +0100, Dean Rasheed wrote: > > > > Most common implementations do regard factorial as undefined for > > anything other than positive integers, as well as following the > > convention that factorial(0) = 1. So

Re: factorial of negative numbers

2020-06-16 Thread Juan José Santamaría Flecha
On Tue, Jun 16, 2020 at 10:55 AM Bruce Momjian wrote: > On Tue, Jun 16, 2020 at 08:31:21AM +0100, Dean Rasheed wrote: > > > > Most common implementations do regard factorial as undefined for > > anything other than positive integers, as well as following the > > convention that factorial(0) = 1.

Re: factorial of negative numbers

2020-06-16 Thread Bruce Momjian
On Tue, Jun 16, 2020 at 08:31:21AM +0100, Dean Rasheed wrote: > On Tue, 16 Jun 2020 at 06:00, Ashutosh Bapat > wrote: > > > > Divison by zero is really undefined, 12345678 * 12345678 (just some > > numbers) is out of range of say int4, but factorial of a negative number > > has some meaning and

Re: factorial of negative numbers

2020-06-16 Thread Dean Rasheed
On Tue, 16 Jun 2020 at 06:00, Ashutosh Bapat wrote: > > Divison by zero is really undefined, 12345678 * 12345678 (just some numbers) > is out of range of say int4, but factorial of a negative number has some > meaning and is defined but PostgreSQL does not support it. > Actually, I think undefi

Re: factorial of negative numbers

2020-06-15 Thread Ashutosh Bapat
On Tue, 16 Jun 2020 at 08:48, Peter Eisentraut < peter.eisentr...@2ndquadrant.com> wrote: > On 2020-06-15 13:15, Ashutosh Bapat wrote: > > Here are some comments > > I see below in the .out but there's not corresponding SQL in .sql. > > +SELECT factorial(-4); > > + factorial > > +--- > > +

Re: factorial of negative numbers

2020-06-15 Thread Peter Eisentraut
On 2020-06-15 13:15, Ashutosh Bapat wrote: Here are some comments I see below in the .out but there's not corresponding SQL in .sql. +SELECT factorial(-4); + factorial +--- + 1 +(1 row) + Should we also add -4! to cover both function as well as the operator? I will add that. I

Re: factorial of negative numbers

2020-06-15 Thread Tom Lane
... oh, one slightly more important nit-pick: per the catalogs and code, the function is factorial(bigint): Schema | Name| Result data type | Argument data types | Type +---+--+-+-- pg_catalog | factorial | numeric |

Re: factorial of negative numbers

2020-06-15 Thread Tom Lane
Peter Eisentraut writes: > Adjacent to the discussion in [0] I wanted to document the factorial() > function and expand the tests for that slightly with some edge cases. > ... > I propose to change this to error out for negative numbers. +1 for all of this, with a couple trivial nitpicks about t

Re: factorial of negative numbers

2020-06-15 Thread Ashutosh Bapat
On Mon, Jun 15, 2020 at 12:41 PM Peter Eisentraut wrote: > > Adjacent to the discussion in [0] I wanted to document the factorial() > function and expand the tests for that slightly with some edge cases. > > I noticed that the current implementation returns 1 for the factorial of > all negative nu