1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-12 05:01:15 +03:00

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

to note:

1) arttype is numeric. I thought this was the best way of allowing
arbitarily large factorials, even though factorial(2^63) is a large
number. Happy to change to integers if this is overkill.
2) since we're accepting numeric arguments, the patch tests for floats.
If a numeric is passed with non-zero decimal portion, an error is raised
since (from memory) they are undefined.

Gavin Sherry
This commit is contained in:
Bruce Momjian
2003-12-01 21:52:38 +00:00
parent af03663878
commit 04a4821ade
9 changed files with 74 additions and 100 deletions

View File

@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/int.c,v 1.58 2003/11/29 19:51:58 pgsql Exp $
* $PostgreSQL: pgsql/src/backend/utils/adt/int.c,v 1.59 2003/12/01 21:52:37 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -26,7 +26,7 @@
* intpl, intmi, int4mul, intdiv
*
* Arithmetic operators:
* intmod, int4fac
* intmod
*/
#include "postgres.h"
@@ -849,40 +849,6 @@ int42mod(PG_FUNCTION_ARGS)
PG_RETURN_INT32(arg1 % arg2);
}
/* int[24]fac()
* Factorial
*/
Datum
int4fac(PG_FUNCTION_ARGS)
{
int32 arg1 = PG_GETARG_INT32(0);
int32 result;
if (arg1 == 0)
result = 1;
else if (arg1 < 0)
result = 0;
else
for (result = 1; arg1 > 0; --arg1)
result *= arg1;
PG_RETURN_INT32(result);
}
Datum
int2fac(PG_FUNCTION_ARGS)
{
int16 arg1 = PG_GETARG_INT16(0);
int32 result;
if (arg1 == 0)
result = 1;
else if (arg1 < 0)
result = 0;
else
for (result = 1; arg1 > 0; --arg1)
result *= arg1;
PG_RETURN_INT32(result);
}
/* int[24]abs()
* Absolute value

View File

@@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/int8.c,v 1.49 2003/11/29 19:51:58 pgsql Exp $
* $PostgreSQL: pgsql/src/backend/utils/adt/int8.c,v 1.50 2003/12/01 21:52:37 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -561,26 +561,6 @@ int8mod(PG_FUNCTION_ARGS)
PG_RETURN_INT64(result);
}
/* int8fac()
* Factorial
*/
Datum
int8fac(PG_FUNCTION_ARGS)
{
int64 arg1 = PG_GETARG_INT64(0);
int64 result;
int64 i;
if (arg1 == 0)
result = 1;
else if (arg1 < 1)
result = 0;
else
for (i = arg1, result = 1; i > 0; --i)
result *= i;
PG_RETURN_INT64(result);
}
Datum
int8inc(PG_FUNCTION_ARGS)

View File

@@ -14,7 +14,7 @@
* Copyright (c) 1998-2003, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.68 2003/11/29 19:51:59 pgsql Exp $
* $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.69 2003/12/01 21:52:37 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -1288,6 +1288,55 @@ numeric_larger(PG_FUNCTION_ARGS)
* ----------------------------------------------------------------------
*/
/*
* numeric_fac()
* Computer factorial
*/
Datum
numeric_fac(PG_FUNCTION_ARGS)
{
int64 num = PG_GETARG_INT64(0);
NumericVar count;
NumericVar fact;
NumericVar zerovar;
NumericVar result;
Numeric res;
if(num < 1) {
res = make_result(&const_one);
PG_RETURN_NUMERIC(res);
}
init_var(&fact);
init_var(&count);
init_var(&result);
init_var(&zerovar);
zero_var(&zerovar);
int8_to_numericvar((int64)num, &result);
set_var_from_var(&const_one, &count);
for(num = num - 1; num > 0; num--) {
set_var_from_var(&result,&count);
int8_to_numericvar((int64)num,&fact);
mul_var(&count, &fact, &result, count.dscale + fact.dscale);
}
res = make_result(&count);
free_var(&count);
free_var(&fact);
free_var(&result);
free_var(&zerovar);
PG_RETURN_NUMERIC(res);
}
/*
* numeric_sqrt() -