Thread: Working with fixed-point calculations in C

Working with fixed-point calculations in C

From
Oskar Stenberg
Date:
Hi!

I need to make some fixed-point calculations inside the C code that I'm developing as an extension to PostgreSQL. I was reading a bit, and if I understood the datatype NUMBER correctly, it seems to be just what I'm looking for, a fixed-point datatype. (I couldn't actually find any thing in the documentation that specifically mentions that it is a fixed point datatype. But it seems to work the same. So I might be wrong here and if so please tell me)

Anyway, I thought that it would be good to store the decimal numbers in the database as the datatype NUMBER, and then just use the C version of it when I need to work with it in the code. But when I look at the documentation for writing C functions, I can't find the C version of it? Is that something that just hasn't made it to the documentation, or is there a reason for it not being listed there?

Was hoping that I wouldn't need to reinvent the wheel here or include another library to handle this, as it increases the risk of creating bugs. But if I have to I'll do it.

You have to scroll down a little and you will find a table matching PostgreSQL:s datatypes with the datatypes in C

Best regards
Oskar

Re: Working with fixed-point calculations in C

From
Tom Lane
Date:
Oskar Stenberg <oskar_stenberg@outlook.com> writes:
> I need to make some fixed-point calculations inside the C code that I'm
> developing as an extension to PostgreSQL. I was reading a bit, and if I
> understood the datatype NUMBER correctly, it seems to be just what I'm
> looking for, a fixed-point datatype. (I couldn't actually find any
> thing in the documentation that specifically mentions that it is a
> fixed point datatype. But it seems to work the same. So I might be
> wrong here and if so please tell me)

I think what you are looking for is NUMERIC.  (The type name NUMBER
is an Oracle-ism, I believe.  I'm not sure how closely that corresponds
to our NUMERIC.)

> Link to the documentation:
> https://www.postgresql.org/docs/current/xfunc-c.html#XFUNC-C-TYPE-TABLE

Hmm, that table seems a bit incomplete/out of date.  While it's
not really meant to cover every type, surely it should mention
bigint, numeric, and timestamptz ...

            regards, tom lane



Re: Working with fixed-point calculations in C

From
"Peter J. Holzer"
Date:
On 2021-12-07 22:41:38 -0500, Tom Lane wrote:
> I think what you are looking for is NUMERIC.  (The type name NUMBER
> is an Oracle-ism, I believe.  I'm not sure how closely that corresponds
> to our NUMERIC.)

Not very. Oracle NUMBER is a decimal floating point number with a 38
digit mantissa stored in a variable length format.

        hp

--
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachment

RE: Working with fixed-point calculations in C

From
Oskar Stenberg
Date:
Hi, thanks for your answer!  


Yeah, that's correct, I meant NUMERIC and not NUMBER. It was late and I must have accidentally mixed up the names... But thanks for pointing that out! :) But just to clarify, the data type NUMERIC is a fixed point number and not a floating point number?


Alright, so the documentation is just out of date, that's actually what I was hoping for! And while it would be great if it could be updated! Do you in the meantime happen to know where the datatype NUMERIC is defined in the code? And is it the same name in C as in postgres? Or is it something else?


Best Regards 

Oskar


-------- Originalmeddelande --------
Från: Tom Lane <tgl@sss.pgh.pa.us>
Datum: 2021-12-08 04:41 (GMT+01:00)
Till: Oskar Stenberg <oskar_stenberg@outlook.com>
Kopia: pgsql-general@lists.postgresql.org
Ämne: Re: Working with fixed-point calculations in C

Oskar Stenberg <oskar_stenberg@outlook.com> writes:
> I need to make some fixed-point calculations inside the C code that I'm
> developing as an extension to PostgreSQL. I was reading a bit, and if I
> understood the datatype NUMBER correctly, it seems to be just what I'm
> looking for, a fixed-point datatype. (I couldn't actually find any
> thing in the documentation that specifically mentions that it is a
> fixed point datatype. But it seems to work the same. So I might be
> wrong here and if so please tell me)

I think what you are looking for is NUMERIC.  (The type name NUMBER
is an Oracle-ism, I believe.  I'm not sure how closely that corresponds
to our NUMERIC.)

> Link to the documentation:
> https://www.postgresql.org/docs/current/xfunc-c.html#XFUNC-C-TYPE-TABLE

Hmm, that table seems a bit incomplete/out of date.  While it's
not really meant to cover every type, surely it should mention
bigint, numeric, and timestamptz ...

                        regards, tom lane