numeric

Started by Juan Pablo L.over 10 years ago8 messagesgeneral
Jump to latest
#1Juan Pablo L.
jpablolorenzetti@hotmail.com

Hi, i m writing a C module (extension), the procedure has a parameter that is of type numeric,
inside the function i can not read the parameter or so it seems, this what is do:

float8 db_balance,in_chgval;

in_chgval = PG_GETARG_FLOAT8(2);

elog(INFO,"in_chgval = %0.2f",in_chgval);

The above elog, always shows cero, more over, i m also trying to read a
numeric from the database and substracted from the in_chgval parameter,
the operation always gives 0,, so apparently i can not read a numeric from a
data base:

db_balance = DatumGetFloat8(SPI_getbinval(SPI_tuptable->vals[0],SPI_tuptable->tupdesc,1,&is_null));

elog(INFO,"db_balance(%f) - in_chgval(%f) = %f",db_balance,in_chgval,(db_balance - in_chgval));

it shows all 0 ..... please any advise would be really appreciated. thank you!!

#2Juan Pablo L.
jpablolorenzetti@hotmail.com
In reply to: Juan Pablo L. (#1)
numeric data type

Hi, i m writing a C module (extension), the procedure has a parameter that is of type numeric,
inside the function i can not read the parameter or so it seems, this what is do:

float8 db_balance,in_chgval;

in_chgval = PG_GETARG_FLOAT8(2);

elog(INFO,"in_chgval = %0.2f",in_chgval);

The above elog, always shows cero, more over, i m also trying to read a
numeric from the database and substracted from the in_chgval parameter,
the operation always gives 0,, so apparently i can not read a numeric from a
data base:

db_balance = DatumGetFloat8(SPI_getbinval(SPI_tuptable->vals[0],SPI_tuptable->tupdesc,1,&is_null));

elog(INFO,"db_balance(%f) - in_chgval(%f) = %f",db_balance,in_chgval,(db_balance - in_chgval));

it shows all 0 ..... please any advise would be really appreciated. thank you!!

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Juan Pablo L. (#2)
Re: numeric data type

"Juan Pablo L." <jpablolorenzetti@hotmail.com> writes:

Hi, i m writing a C module (extension), the procedure has a parameter that is of type numeric,
inside the function i can not read the parameter or so it seems, this what is do:

float8 db_balance,in_chgval;

in_chgval = PG_GETARG_FLOAT8(2);

elog(INFO,"in_chgval = %0.2f",in_chgval);

If the C code is written that way, the function has to be declared to take
type float8 (a/k/a double precision), not numeric.

The parser will insert a conversion from numeric to float8 automatically,
so the function will still work with a numeric data column. You might
lose some precision in the conversion though.

regards, tom lane

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#4Juan Pablo L.
jpablolorenzetti@hotmail.com
In reply to: Tom Lane (#3)
Re: numeric data type

thank you for your answer, the function is declared as:

FUNCTION wtt_discount_account(IN in_phonenumber varchar(20),IN in_balanceid integer,IN in_chgval numeric(10,2))

i chose numeric because is supposed to be better for numbers/money operations, supposed to be exact,
i would not want to loose precision because that is money, is there any other way which does not
involve loosing precision ? thankS!!

Show quoted text

From: tgl@sss.pgh.pa.us
To: jpablolorenzetti@hotmail.com
CC: pgsql-general@postgresql.org
Subject: Re: [GENERAL] numeric data type
Date: Tue, 22 Sep 2015 16:07:36 -0400

"Juan Pablo L." <jpablolorenzetti@hotmail.com> writes:

Hi, i m writing a C module (extension), the procedure has a parameter that is of type numeric,
inside the function i can not read the parameter or so it seems, this what is do:

float8 db_balance,in_chgval;

in_chgval = PG_GETARG_FLOAT8(2);

elog(INFO,"in_chgval = %0.2f",in_chgval);

If the C code is written that way, the function has to be declared to take
type float8 (a/k/a double precision), not numeric.

The parser will insert a conversion from numeric to float8 automatically,
so the function will still work with a numeric data column. You might
lose some precision in the conversion though.

regards, tom lane

#5Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Juan Pablo L. (#4)
Re: numeric data type

Juan Pablo L. wrote:

thank you for your answer, the function is declared as:

FUNCTION wtt_discount_account(IN in_phonenumber varchar(20),IN in_balanceid integer,IN in_chgval numeric(10,2))

i chose numeric because is supposed to be better for numbers/money operations, supposed to be exact,
i would not want to loose precision because that is money, is there any other way which does not
involve loosing precision ? thankS!!

What you need is to use PG_GETARG_NUMERIC, then, and use a Numeric *
variable rather than float8.

--
�lvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#6Juan Pablo L.
jpablolorenzetti@hotmail.com
In reply to: Alvaro Herrera (#5)
Re: numeric data type

Hi Alvaro, thank you for your answer, PG_GETARG_NUMERIC does not exist .. cant find it in the source code and when running i get
undefined symbol: PG_GETARG_NUMERIC.

Show quoted text

Date: Tue, 22 Sep 2015 18:11:26 -0300
From: alvherre@2ndquadrant.com
To: jpablolorenzetti@hotmail.com
CC: tgl@sss.pgh.pa.us; pgsql-general@postgresql.org
Subject: Re: [GENERAL] numeric data type

Juan Pablo L. wrote:

thank you for your answer, the function is declared as:

FUNCTION wtt_discount_account(IN in_phonenumber varchar(20),IN in_balanceid integer,IN in_chgval numeric(10,2))

i chose numeric because is supposed to be better for numbers/money operations, supposed to be exact,
i would not want to loose precision because that is money, is there any other way which does not
involve loosing precision ? thankS!!

What you need is to use PG_GETARG_NUMERIC, then, and use a Numeric *
variable rather than float8.

--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#7Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Juan Pablo L. (#6)
Re: numeric data type

Juan Pablo L. wrote:

Hi Alvaro, thank you for your answer, PG_GETARG_NUMERIC does not exist .. cant find it in the source code and when running i get
undefined symbol: PG_GETARG_NUMERIC.

#include "utils/numeric.h"

--
�lvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#8Juan Pablo L.
jpablolorenzetti@hotmail.com
In reply to: Alvaro Herrera (#7)
Re: numeric data type

Alvaro, thank you, that worked.

Show quoted text

Date: Tue, 22 Sep 2015 18:57:38 -0300
From: alvherre@2ndquadrant.com
To: jpablolorenzetti@hotmail.com
CC: tgl@sss.pgh.pa.us; pgsql-general@postgresql.org
Subject: Re: [GENERAL] numeric data type

Juan Pablo L. wrote:

Hi Alvaro, thank you for your answer, PG_GETARG_NUMERIC does not exist .. cant find it in the source code and when running i get
undefined symbol: PG_GETARG_NUMERIC.

#include "utils/numeric.h"

--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services