Re: dividing money by money - Mailing list pgsql-bugs

From Andy Balholm
Subject Re: dividing money by money
Date
Msg-id C3F71DDF-4D71-4CA5-9EF6-718DF6D9745D@balholm.com
Whole thread Raw
In response to Re: dividing money by money  ("Kevin Grittner" <Kevin.Grittner@wicourts.gov>)
Responses Re: dividing money by money  ("Kevin Grittner" <Kevin.Grittner@wicourts.gov>)
List pgsql-bugs
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
);

pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: BUG #5400: Columns count mismatch in RULE with subquery
Next
From: "Kevin Grittner"
Date:
Subject: Re: dividing money by money