Precision loss when INSERTing float values

Remy Blank <[email protected]>
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
Hello,

I have noticed a loss of precision when inserting float values into a
table column of type DOUBLE PRECISION. The usual precision of such a
column is 15 decimal digits. Inserting values through pgsql actually
keeps the 15 digits, but values inserted through psycopg are truncated
after 12 decimal digits.

This is due to using PyObject_Str() to convert the float into a string.
Python's float.__str__() only returns 12 decimal digits:

  >>> v = 1234567890.12345678
  >>> repr(v)
  '1234567890.1234567'
  >>> str(v)
  '1234567890.12'

It would be better to use PyObject_Repr() instead, as float.__repr__()
returns all significant digits. And indeed, the following patch (tested
against psycopg 2.0.12) fixes the issue:

--- psycopg/adapter_pfloat.c.orig       2009-11-08 23:06:57 +0100
+++ psycopg/adapter_pfloat.c    2009-11-08 23:07:15 +0100
@@ -44,7 +44,7 @@
     else if (isinf(n))
         return PyString_FromString("'Infinity'::float");
     else
-        return PyObject_Str(self->wrapped);
+        return PyObject_Repr(self->wrapped);
 }

 static PyObject *

As a (pure Python) workaround, if you don't need infinite and NaN
values, you can register an adapter for float:

  register_adapter(float, lambda value: AsIs(repr(value)))

For reference, this issue has been discovered here:

  http://trac.edgewall.org/ticket/6466#comment:17

(Yes, I know I just lost all credibility :-)

-- Remy

_______________________________________________
Psycopg mailing list
Psycopg-IAPFreCvJWPBWskQ1e/[email protected]
http://lists.initd.org/mailman/listinfo/psycopg
signature.asc (application/pgp-signature, 198 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.11 (GNU/Linux)

iEYEARECAAYFAkr3WUcACgkQCeNfIyhvXjIOxQCgxoX8kwtSKpzwrzjU6S1jBEu9
GZEAn1zoBKVJ9Y/3Yx8KmfuEXod9OsRI
=DMOq
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.