pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Working with fixed-point calculations in C
4+ messages / 3 participants
[nested] [flat]

Commits from this thread:
  2021-12-08 0862317b12 a96a1d6562 d393831eea 8a291f4e50 6f0e6ab04d 3d60343197  Doc: improve xfunc-c-type-table.
* Working with fixed-point calculations in C
@ 2021-12-07 22:39  Oskar Stenberg <oskar_stenberg@outlook.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Oskar Stenberg @ 2021-12-07 22:39 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Hi+ACE

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,+AKA-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.

Link to the documentation:
https://www.postgresql.org/docs/current/xfunc-c.html+ACM-XFUNC-C-BASETYPE
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

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Working with fixed-point calculations in C
@ 2021-12-08 03:41  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Oskar Stenberg <oskar_stenberg@outlook.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Tom Lane @ 2021-12-08 03:41 UTC (permalink / raw)
  To: Oskar Stenberg <oskar_stenberg@outlook.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

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





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Working with fixed-point calculations in C
@ 2021-12-08 11:03  Peter J. Holzer <hjp-pgsql@hjp.at>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 4+ messages in thread

From: Peter J. Holzer @ 2021-12-08 11:03 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

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!"

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* RE: Working with fixed-point calculations in C
@ 2021-12-08 14:18  Oskar Stenberg <oskar_stenberg@outlook.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 4+ messages in thread

From: Oskar Stenberg @ 2021-12-08 14:18 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

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

^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2021-12-08 14:18 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-12-07 22:39 Working with fixed-point calculations in C Oskar Stenberg <oskar_stenberg@outlook.com>
2021-12-08 03:41 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-12-08 11:03   ` Peter J. Holzer <hjp-pgsql@hjp.at>
2021-12-08 14:18   ` Oskar Stenberg <oskar_stenberg@outlook.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox