Re: [HACKERS] float output precision questions
Peter Eisentraut wrote: 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). Do we have some mathematical guarantee that this is sufficient and necessary? If so, then it might do. It is necessary if you want to do this type of (huge amount of number storage) application: [Some client] (insert) [PostgreSQL] (query) [Some client] (double number a)(float8 number)---(double number b) In order for a=b this is necessary. With current float8 output this is not allways true. Here is the help for that particular Matlab function: NUM2MSTR Convert number to string in maximum precision. S = NUM2MSTR(N) converts real numbers of input matrix N to string output vector S, in maximum precision. See also NUM2STR. If you want I can try to contact the guys who coded this function to know if this is sufficient. Thanks, 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 ---(end of broadcast)--- TIP 4: Don't 'kill -9' the postmaster
Re: [HACKERS] float output precision questions
Stephan Szabo wrote: On Tue, 29 Oct 2002, Peter Eisentraut wrote: Pedro Miguel Frazao Fernandes Ferreira writes: 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. How do you define maximum precision and same? With simple test programs in C, using two digits more than DBL_DIG for printf specifier, it's easy for me to find numbers that change decimal string representation in the decimal representation - double - decimal representation conversion(*). The final double you get from the second conversion should be the same as the first, but is that what you need or do you need a stronger guarantee than that? When I say same I am talking about having a number 'stored' in double format in some client, inserting it in PostgreSQL float8 field and get it to the client as it was before: [Some client] (insert) [PostgreSQL] (query) [Some client] (double number a)(float8 number)---(double number b) same is so that a==b is true. With current float8 output this is not allways true. I believe this should allways be true for numbers which are originally stored in double format. Thanks, 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 ---(end of broadcast)--- TIP 5: Have you checked our extensive FAQ? http://www.postgresql.org/users-lounge/docs/faq.html
Re: [HACKERS] float output precision questions
Bruno Wolff III wrote: On Tue, Oct 29, 2002 at 23:19:05 +0100, Peter Eisentraut [EMAIL PROTECTED] wrote: There isn't a way right now, but it's planned to be able to dump floating-point numbers in some binary form (like printf(%A)) to be able to restore them exactly. Not sure how that would satisfy the needs of client interfaces, though. Why not print it as a floating binary number instead of a floating decimal number? I would think that would give you better portability than a system specific binary representation. Having a way to get the binary storage representation for float numbers would be good and would suffice within the same float number standard. Thanks, 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 ---(end of broadcast)--- TIP 5: Have you checked our extensive FAQ? http://www.postgresql.org/users-lounge/docs/faq.html
Re: [HACKERS] float output precision questions
Stephan Szabo wrote: On Wed, 30 Oct 2002, Pedro Miguel Frazao Fernandes Ferreira wrote: Stephan Szabo wrote: On Tue, 29 Oct 2002, Peter Eisentraut wrote: Pedro Miguel Frazao Fernandes Ferreira writes: 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. How do you define maximum precision and same? With simple test programs in C, using two digits more than DBL_DIG for printf specifier, it's easy for me to find numbers that change decimal string representation in the decimal representation - double - decimal representation conversion(*). The final double you get from the second conversion should be the same as the first, but is that what you need or do you need a stronger guarantee than that? When I say same I am talking about having a number 'stored' in double format in some client, inserting it in PostgreSQL float8 field and get it to the client as it was before: [Some client] (insert) [PostgreSQL] (query) [Some client] (double number a)(float8 number)---(double number b) same is so that a==b is true. With current float8 output this is not allways true. I believe this should allways be true for numbers which are originally stored in double format. The problem is that there are two competing needs here. One is the above, the other other is that you get something that has the same decimal representation (within the float's ability to store the number). Right now the system does the latter since for most people, that seems to be the guarantee they want. Look at this example: 1.79769313486231571e+308 is the largest floating point number representable by a C double in x86. In C this is possible: #include stdio.h #include stdlib.h int main(void) { double v; char a[30]; v=1.79769313486231571e+308; printf( Stored double number: %25.18g\n,v); sprintf(a,%25.18g,v); printf(Converted to string: %s\n,a); v=atof(a); printf(Converted from string to double: %25.18g\n,v); } Using standard PostgreSQL query output it would not be possible to get this number, which has representation as a double. I fetched the PostgreSQL source from Debian, changed src/backend/utils/adt/float.c to do sprintf(ascii, %25.18g, num) instead of sprintf(ascii, %.*g, DBL_DIG, num), compiled and installed. Now I can get the number as is. I understand that if people insert a value of 1.1 in a double, they want to get 1.1 without knowing that in fact the stored number is 1.10009. But do you understand that if some people insert, for example, a value of 1.79769313486231571e+308 they dont want to get 1.79769313486232e+308 which does not compare equal (in Matlab or C) to the first ? This is a bug. This would probably make sense as an option, so why don't you look at the past discussions and see if you can come up with a solution that keeps everyone happy (and preferably implement it, but...) :) but ??? I have a sugestion: To have parameters, say DOUBLE_FORMAT and FLOAT_FORMAT, which could have option values of 'SHORT' and 'LONG'. Option 'SHORT' would be default and produce the standard sprintf(ascii,... Option 'LONG' would produce sprintf(ascii, %25.18g, num). Other way would be to have number parameters to be used in the sprintf calls, in place of 25 and 18, in the format string. ---(end of broadcast)--- TIP 4: Don't 'kill -9' the postmaster -- -- 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 ---(end of broadcast)--- TIP 6: Have you searched our list archives? http://archives.postgresql.org
[HACKERS] float output precision questions
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,'999.9') 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 ---(end of broadcast)--- TIP 4: Don't 'kill -9' the postmaster
Re: [HACKERS] float output precision questions
Sorry. I forgot to thank for any help from all of you in the previous message. Thanks! :) Just one more thing: I now I can go to the source and change the code which converts floats to strings, and have my problem solved. But this wont be general. Others might need this kind of application. I could post this interface to postgres interfaces site. Do you think this is ok ? Thanks again ! Pedro M. Ferreira Pedro Miguel Frazao Fernandes Ferreira wrote: 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,'999.9') 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 ---(end of broadcast)--- TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED]