Re: Formatting query output - Mailing list pgsql-general

From Pedro Miguel Frazao Fernandes Ferreira
Subject Re: Formatting query output
Date
Msg-id 3DBE5D4E.6050202@ualg.pt
Whole thread Raw
In response to Formatting query output  (Pedro Miguel Frazao Fernandes Ferreira <pfrazao@ualg.pt>)
Responses Re: Formatting query output  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general

Martijn van Oosterhout wrote:
> On Mon, Oct 28, 2002 at 02:56:20PM +0000, Pedro Miguel Frazao Fernandes Ferreira wrote:
>
>>Hi All,
>>
>>I am using libpq to interface PostgreSQL with Matlab.
>>My question is about formatting query results:
>>
>>If I have a table field which is of type float8 and I insert, for
>>example, a value of 4503599627370496, when I do a select on this table
>>field I get 4.5035996273705e+15.
>
>
> I think the reason for that is that float8s can only store about 16
> significant digits anyway so you're cutting pretty close to the line.
> Remember, floating point numbers are accurate but not precise. Do you really
> need that extra digit of precision?

Ok, but PostgreSQL stores the number correctly:
(real8 is the field name)

select to_char(real8,'9999999999999999999.99999') from test;
        to_char
----------------------
      4503599627370496
(1 row)

Query output formatting for float8 does the following:

select real8 from test;
         real8
---------------------
  4.5035996273705e+15
(1 row)


I have a simple C interface between PostgreSQL and Matlab which is a
Matrix based computation and simulation software. I want to use
PostgreSQL to store data from my simulations. When I store a float8
number in a database, I am supposed to fetch the number as inserted. It
is not a matter of wanting a determined precision. Matlab data types
match to some PostgreSQL data types in size.
As you can see above the number is stored correctly, its just the output
precision for float8 in querys that cuts the number for output. I can do
it with the to_char() function but then libpq PQftype() function will
not return a float8 type for this field.

I believe there is (should be ?) some runtime parameter to control the
output precision for floats, but I still can not find anything in the
doc's. Do you know anything like this ?

>
> Maybe you should be using numeric() type for this. Actually, it looks like a
> credit card number, perhaps you should use a string if you're not actually
> doing calculations on it.
>

I think now you understand there is no need for numeric in this case,
and that strings are not a correct storage solution.

Thank you very much for your reply.

Best regards,
Pedro M. Ferreira
--
----------------------------------------------------------------------
Pedro Miguel Frazao Fernandes Ferreira
Universidade do Algarve
Faculdade de Ciencias e Tecnologia
Campus de Gambelas
8000-117 Faro
Portugal
Tel./Fax:  (+351) 289 800950 / 289 819403
http://w3.ualg.pt/~pfrazao


pgsql-general by date:

Previous
From: Christoph Dalitz
Date:
Subject: Re: supplying password to psql on command line?
Next
From: Philip Rhoades
Date:
Subject: Functions resembling stored procedures with Crystal Reports