Thread: Check that numeric is zero

Check that numeric is zero

From
Gabriel Furstenheim Milerud
Date:
Hi,
I'm writing a Postgres native extension and I would like to check that a numeric is zero.

My problem is that all exported methods like numeric_eq or numeric_sign require me to have a numeric to start with, and const_zero is not exported in numeric.c.

Any idea how to check it?

Thanks
Gabriel Fürstenheim 

Re: Check that numeric is zero

From
Andrew Gierth
Date:
>>>>> "Gabriel" == Gabriel Furstenheim Milerud <furstenheim@gmail.com> writes:

 Gabriel> Hi,

 Gabriel> I'm writing a Postgres native extension and I would like to
 Gabriel> check that a numeric is zero.

 Gabriel> My problem is that all exported methods like numeric_eq or
 Gabriel> numeric_sign require me to have a numeric to start with, and
 Gabriel> const_zero is not exported in numeric.c.

Currently the easiest and most portable way to get a numeric constant is
to call int4_numeric or int8_numeric via DirectFunctionCall; if you
don't care about versions older than pg14 there's also int64_to_numeric
which can be called directly from C.

    Datum zero_num = DirectFunctionCall1(int4_numeric, Int32GetDatum(0));

(remember that this will be allocated in the current memory context; if
you want to keep a copy long-term, you'd want to datumCopy it somewhere
else.)

-- 
Andrew (irc:RhodiumToad)