pg.ddx.io pgsql-odbc@postgresql.org mailing list archive
help / color / mirror / Atom feedPrecision problems with storage of float/double
3+ messages / 2 participants
[nested] [flat]
* Precision problems with storage of float/double
@ 2014-06-15 16:51 John Smith <nukemd@hotmail.com>
0 siblings, 1 reply; 3+ messages in thread
From: John Smith @ 2014-06-15 16:51 UTC (permalink / raw)
To: pgsql-odbc
Hi,
I have found that floats and doubles that I save to the database with the ODBC driver may lose precision. The exact bit pattern of my double/float is not what is actually saved in the database. This causes difficulties in situations where you need to compare current record contents with the actual data that was inserted.
It appears to be related to conversion from the double/float to text before it is sent to the database.
I made a simple patch that appears to fix the issue (by adding more decimal digits in the string conversion), and a test case that tests the issue.
=
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc
Attachments:
float-save.patch (application/octet-stream, 638B) download
diff --git a/convert.c b/convert.c
index ca01a57..6b14339 100644
--- a/convert.c
+++ b/convert.c
@@ -4275,7 +4275,7 @@ mylog("C_WCHAR=%s(%d)\n", buffer, used);
if (_finite(dbv))
#endif /* WIN32 */
{
- sprintf(param_string, "%.15g", dbv);
+ sprintf(param_string, "%.18g", dbv);
set_server_decimal_point(param_string);
}
#ifdef WIN32
@@ -4294,7 +4294,7 @@ mylog("C_WCHAR=%s(%d)\n", buffer, used);
if (_finite(flv))
#endif /* WIN32 */
{
- sprintf(param_string, "%.6g", flv);
+ sprintf(param_string, "%.10g", flv);
set_server_decimal_point(param_string);
}
#ifdef WIN32
float-save-test.c (text/plain, 2.8K) download
/*
* Tests saving a value of type SQLDOUBLE and then retrieving it back.
*/
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
int main(int argc, char** argv)
{
SQLRETURN rc;
HSTMT hstmt = SQL_NULL_HSTMT;
SQLCHAR *sql;
//Raw byte data for a double of value 2.0990256436450165
unsigned char double_data[sizeof(SQLDOUBLE)] = { 0x96, 0x5d, 0xe7, 0xf4, 0xcd, 0xca, 0x00, 0x40 };
SQLDOUBLE double_get = 0.0;
//Raw byte data for a float of value 1.90480649
unsigned char float_data[sizeof(SQLREAL)] = { 0xb3, 0xd0, 0xf3, 0x3f };
SQLREAL float_get = 0.0f;
test_connect();
rc = SQLAllocHandle(SQL_HANDLE_STMT, conn, &hstmt);
if (!SQL_SUCCEEDED(rc))
{
print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
exit(1);
}
sql = "DROP TABLE IF EXISTS float_save";
rc = SQLExecDirect(hstmt, sql, SQL_NTS);
CHECK_STMT_RESULT(rc, "SQLExecDirect failed while dropping previous table", hstmt);
sql = "CREATE TABLE float_save (mydouble double precision, myfloat float4)";
rc = SQLExecDirect(hstmt, sql, SQL_NTS);
CHECK_STMT_RESULT(rc, "SQLExecDirect failed while creating table", hstmt);
sql = "INSERT INTO float_save values(?, ?)";
rc = SQLPrepare(hstmt, sql, SQL_NTS);
CHECK_STMT_RESULT(rc, "SQLPrepare failed", hstmt);
rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DOUBLE, 0, 0, double_data, sizeof(SQLDOUBLE), NULL);
CHECK_STMT_RESULT(rc, "SQLBindParam (SQLDOUBLE) failed", hstmt);
rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_FLOAT, SQL_REAL, 0, 0, float_data, sizeof(SQLREAL), NULL);
CHECK_STMT_RESULT(rc, "SQLBindParam (SQLREAL) failed", hstmt);
rc = SQLExecute(hstmt);
CHECK_STMT_RESULT(rc, "SQLExec failed", hstmt);
rc = SQLFreeStmt(hstmt, SQL_CLOSE);
CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);
sql = "SELECT mydouble, myfloat FROM float_save";
rc = SQLPrepare(hstmt, sql, SQL_NTS);
CHECK_STMT_RESULT(rc, "SQLPrepare failed", hstmt);
rc = SQLBindCol(hstmt, 1, SQL_C_DOUBLE, &double_get, 0, NULL);
CHECK_STMT_RESULT(rc, "SQLBindCol (SQLDOUBLE) failed", hstmt);
rc = SQLBindCol(hstmt, 2, SQL_C_FLOAT, &float_get, 0, NULL);
CHECK_STMT_RESULT(rc, "SQLBindCol (SQLREAL) failed", hstmt);
rc = SQLExecute(hstmt);
CHECK_STMT_RESULT(rc, "SQLExec failed", hstmt);
rc = SQLFetch(hstmt);
CHECK_STMT_RESULT(rc, "SQLFetch failed", hstmt);
int fail = 0;
if (memcmp(double_data, &double_get, sizeof(SQLDOUBLE)) != 0)
{
printf("Fetched double value differs from saved double value.\n");
fail = 1;
}
if (memcmp(float_data, &float_get, sizeof(SQLREAL)) != 0)
{
printf("Fetched float value differs from saved float value.\n");
fail = 1;
}
if (fail)
exit(1);
SQLFreeStmt(hstmt, SQL_CLOSE);
SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
test_disconnect();
}
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Precision problems with storage of float/double
@ 2014-06-17 03:24 Inoue, Hiroshi <inoue@tpf.co.jp>
parent: John Smith <nukemd@hotmail.com>
0 siblings, 1 reply; 3+ messages in thread
From: Inoue, Hiroshi @ 2014-06-17 03:24 UTC (permalink / raw)
To: John Smith <nukemd@hotmail.com>; +Cc: pgsql-odbc
(2014/06/16 1:51), John Smith wrote:
> Hi,
>
> I have found that floats and doubles that I save to the database with the ODBC driver may lose precision. The exact bit pattern of my double/float is not what is actually saved in the database. This causes difficulties in situations where you need to compare current record contents with the actual data that was inserted.
>
> It appears to be related to conversion from the double/float to text before it is sent to the database.
>
> I made a simple patch that appears to fix the issue (by adding more decimal digits in the string conversion), and a test case that tests the issue.
Thanks.
As far as I examined, 9 decimal digits for 32-bit reals and 17 decimal
digits for 64-bit doubles are sufficient to guarantee the complete
recovery.
Is it OK to change decimal digits in your patch to those ones?
Hmm I noticed that server's output should be changed in addition
to this change. The setting of extra_float_digits should be changed
from 2 to 3.
regards,
Hiroshi Inoue
--
I am using the free version of SPAMfighter.
SPAMfighter has removed 10915 of my spam emails to date.
Get the free SPAMfighter here: http://www.spamfighter.com/len
Do you have a slow PC? Try a Free scan
http://www.spamfighter.com/SLOW-PCfighter?cid=sigen
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Precision problems with storage of float/double
@ 2014-06-17 16:12 John Smith <nukemd@hotmail.com>
parent: Inoue, Hiroshi <inoue@tpf.co.jp>
0 siblings, 0 replies; 3+ messages in thread
From: John Smith @ 2014-06-17 16:12 UTC (permalink / raw)
To: Inoue, Hiroshi <inoue@tpf.co.jp>; +Cc: pgsql-odbc
Hi Hiroshi
I googled this a little, and I think you are right. 17 and 9 should be enough. I don't have any problems with you changing the patch.
----------------------------------------
> Date: Tue, 17 Jun 2014 12:24:51 +0900
> From: inoue@tpf.co.jp
> To: nukemd@hotmail.com
> CC: pgsql-odbc@postgresql.org
> Subject: Re: [ODBC] Precision problems with storage of float/double
>
> (2014/06/16 1:51), John Smith wrote:
>> Hi,
>>
>> I have found that floats and doubles that I save to the database with the ODBC driver may lose precision. The exact bit pattern of my double/float is not what is actually saved in the database. This causes difficulties in situations where you need to compare current record contents with the actual data that was inserted.
>>
>> It appears to be related to conversion from the double/float to text before it is sent to the database.
>>
>> I made a simple patch that appears to fix the issue (by adding more decimal digits in the string conversion), and a test case that tests the issue.
>
> Thanks.
>
> As far as I examined, 9 decimal digits for 32-bit reals and 17 decimal
> digits for 64-bit doubles are sufficient to guarantee the complete
> recovery.
> Is it OK to change decimal digits in your patch to those ones?
>
> Hmm I noticed that server's output should be changed in addition
> to this change. The setting of extra_float_digits should be changed
> from 2 to 3.
>
> regards,
> Hiroshi Inoue
>
>
>
> --
> I am using the free version of SPAMfighter.
> SPAMfighter has removed 10915 of my spam emails to date.
> Get the free SPAMfighter here: http://www.spamfighter.com/len
>
> Do you have a slow PC? Try a Free scan
> http://www.spamfighter.com/SLOW-PCfighter?cid=sigen
>
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2014-06-17 16:12 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-06-15 16:51 Precision problems with storage of float/double John Smith <nukemd@hotmail.com>
2014-06-17 03:24 ` Inoue, Hiroshi <inoue@tpf.co.jp>
2014-06-17 16:12 ` John Smith <nukemd@hotmail.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