On Mar 31, 2010, at 7:07 AM, Kevin Grittner wrote:
> (I was going to mark the TODO as an easy one.)=20=20
I thought it would be pretty simple, too, so I decided to go ahead and writ=
e and test it as an external module.=20
I think the function definition could be pasted directly into an appropriat=
e place in src/backend/utils/adt/cash.c, if someone wants to add it to the =
main code base. The SQL to load it would need to be modified somewhat to fi=
t into postgres.bki.=20
Here is the C source:
#include <postgres.h>
#include <fmgr.h>
#include <utils/cash.h>
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(cash_div_cash);
/* cash_div_cash()
* Divide cash by cash, returning float8.
*/
Datum
cash_div_cash(PG_FUNCTION_ARGS)
{
Cash dividend =3D PG_GETARG_CASH(0);
Cash divisor =3D PG_GETARG_CASH(1);
float8 quotient;
=20=20=20=20
if (divisor =3D=3D 0)
ereport(ERROR,
(errcode(ERRCODE_DIVISION_BY_ZERO),
errmsg("division by zero")));
=20=20=20=20
quotient =3D (float8)dividend / (float8)divisor;
PG_RETURN_FLOAT8(quotient);
}
---------------------------------------------------------------------------=
----------
And here is the SQL to load it (assuming it has been compiled as a dynamica=
lly loadable module named divide_money and placed in the library directory =
on the server):
CREATE FUNCTION cash_div_cash(money, money) RETURNS double precision
LANGUAGE c IMMUTABLE
AS '$libdir/divide_money', 'cash_div_cash';
=20=20=20=20
CREATE OPERATOR / (
PROCEDURE =3D cash_div_cash,
LEFTARG =3D money,
RIGHTARG =3D money
);