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
NumericVar fact;
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)
{
res = make_result(&const_one);
(1 row)
SELECT -4!;
- ?column?
-----------
- 1
-(1 row)
-
+ERROR: factorial of a negative number is undefined
SELECT factorial(-4);
- factorial
------------
- 1
-(1 row)
-
+ERROR: factorial of a negative number is undefined