float output precision questions - Mailing list pgsql-hackers

From Pedro Miguel Frazao Fernandes Ferreira
Subject float output precision questions
Date
Msg-id 3DBE7262.4020705@ualg.pt
Whole thread Raw
Responses Re: float output precision questions
Re: float output precision questions
List pgsql-hackers
Hi All,

I wrote a Matlab interface to PostgreSQL based on libpq. It is working 
fine, supports multiple connections, supports all data types and arrays. 
It is a C program to do the interface and some Matlab wrapper functions 
around it to do the job at application level.

Matlab has an ODBC toolbox but we dont want to buy it since the 
interface does the job and we have been using PostgreSQL for long time.
We want to use PostgreSQl to store numeric data from simulation, 
computation and data acquisition sources. Basically a big bunch of float 
numbers.

There is still one problem, regarding float output formatting in querys 
and dumps, which essential for this type of application.

If I have a float8 field (named real8 below) in a table and insert the 
following,

insert into test(real8) values (4503599627370496);
INSERT 21192 1

A select produces,

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

This is the string I would get from libpq's PQgetvalue(). PQftype() 
correctly returns float8. pg_dump produces the same result. If I convert 
PQgetvalue() to a C double I wont get the same number I inserted. If I 
do a restore from a dump I also wont have the same number. I can see 
that the original number is correctly stored because,

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

This way PQftype wont tell this is a float8.

Is there a way to set query output precision to maximum precision ?
For the type of application I mentioned this is crucial. People want to 
get the 'same' numbers, from querys or dumps, as they inserted them.

Matlab has a toolbox fucntion, claiming maximum precision, to convert 
from its double type (PostgreSQL float8) to string which does a 
sprintf('%25.18g',number).

Would it be possible to have a a parameter which could be changed by a 
SET command, in order to control output precision ? I searched the docs 
but could not find this.

-- 
----------------------------------------------------------------------
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-hackers by date:

Previous
From: mlw
Date:
Subject: Re: Memory leaks
Next
From: Pedro Miguel Frazao Fernandes Ferreira
Date:
Subject: Re: float output precision questions