Commit 0a40563e authored by Peter Eisentraut's avatar Peter Eisentraut

Disallow factorial of negative numbers

The previous implementation returned 1 for all negative numbers, which
is not sensible under any definition.

Discussion: https://www.postgresql.org/message-id/flat/6ce1df0e-86a3-e544-743a-f357ff663f68%402ndquadrant.com
parent 9d402c73
...@@ -2946,6 +2946,10 @@ numeric_fac(PG_FUNCTION_ARGS) ...@@ -2946,6 +2946,10 @@ numeric_fac(PG_FUNCTION_ARGS)
NumericVar fact; NumericVar fact;
NumericVar result; NumericVar result;
if (num < 0)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("factorial of a negative number is undefined")));
if (num <= 1) if (num <= 1)
{ {
res = make_result(&const_one); res = make_result(&const_one);
......
...@@ -2345,14 +2345,6 @@ SELECT 0!; ...@@ -2345,14 +2345,6 @@ SELECT 0!;
(1 row) (1 row)
SELECT -4!; SELECT -4!;
?column? ERROR: factorial of a negative number is undefined
----------
1
(1 row)
SELECT factorial(-4); SELECT factorial(-4);
factorial ERROR: factorial of a negative number is undefined
-----------
1
(1 row)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment