pg.ddx.io  pgsql-odbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: John Smith <nukemd@hotmail.com>
To: pgsql-odbc@postgresql.org <pgsql-odbc@postgresql.org>
Subject: Precision problems with storage of float/double
Date: Sun, 15 Jun 2014 18:51:32 +0200
Message-ID: <DUB121-W17AD73873B390AF3AEF384D3170@phx.gbl> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-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(); }

view thread (3+ messages)  latest in thread

Message-ID: <DUB121-W17AD73873B390AF3AEF384D3170@phx.gbl>
Permalink:  ../DUB121-W17AD73873B390AF3AEF384D3170@phx.gbl/
Also on:    postgresql.org/message-id/DUB121-W17AD73873B390AF3AEF384D3170@phx.gbl

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-odbc@postgresql.org
  Cc: nukemd@hotmail.com
  Subject: Re: Precision problems with storage of float/double
  In-Reply-To: <DUB121-W17AD73873B390AF3AEF384D3170@phx.gbl>
  References: <DUB121-W17AD73873B390AF3AEF384D3170@phx.gbl>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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