factorial of negative numbers

From: Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: factorial of negative numbers
Date: 2020-06-15 07:11:16
Message-ID: 6ce1df0e-86a3-e544-743a-f357ff663f68@2ndquadrant.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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 numbers:

SELECT factorial(-4);
factorial
-----------
1

While there are some advanced mathematical constructions that define
factorials for negative numbers, they certainly produce different
answers than this.

Curiously, before the reimplementation of factorial using numeric
(04a4821adef38155b7920ba9eb83c4c3c29156f8), it returned 0 for negative
numbers, which is also not correct by any theory I could find.

I propose to change this to error out for negative numbers.

See attached patches for test and code changes.

[0]:
https://www.postgresql.org/message-id/flat/38ca86db-42ab-9b48-2902-337a0d6b8311%402ndquadrant.com

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

Attachment Content-Type Size
0001-doc-Document-factorial-function.patch text/plain 1.2 KB
0002-Expand-tests-for-factorial.patch text/plain 2.9 KB
0003-Disallow-factorial-of-negative-numbers.patch text/plain 1.4 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Kyotaro Horiguchi 2020-06-15 07:31:42 Re: Review for GetWALAvailability()
Previous Message Tatsuo Ishii 2020-06-15 07:00:16 Re: Transactions involving multiple postgres foreign servers, take 2