Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hqRwD-0005Y4-Rh for psycopg@arkaria.postgresql.org; Thu, 25 Jul 2019 00:48:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hqRwC-0006p8-JK for psycopg@arkaria.postgresql.org; Thu, 25 Jul 2019 00:48:52 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hqRwB-0006kI-HJ for psycopg@lists.postgresql.org; Thu, 25 Jul 2019 00:48:52 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hqRw7-0000p2-Vi for psycopg@postgresql.org; Thu, 25 Jul 2019 00:48:51 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 71DC021B2C; Wed, 24 Jul 2019 20:48:46 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Wed, 24 Jul 2019 20:48:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=f JjIl32UmTltJC9fVJhvE6h818ssvtOeWzkd5uvoL7E=; b=Hdoc0wo/yZ71IEQSR yEOTOZkXGAx3+O3YRo1Y2qz5Mvssu7f7UxNMw8GUzxulPCDCxBTyugnyAC3GxLiW IJmUbgOX87AON7QpDLr2UlTXD5tEX8LUnRFO2i3NYMsVtZMBKOJdlXHjoufsZtEA LPjcOAZpgjbEBif+o95hP17vGud62+Nwq6PyyDVo8Ewi4C0cOWwiEr8tE3NuigV3 0/ZBzYWtTQgrw0Q2N7cxcVs/Cu7DpCbBf06u4X5YQU9x5vqvudySsjM+v7OD2/XU YnQFR7+0JQwwdZcZt5WLRXSozqW8DXRzKlhrd7TEy2iDKoOBsgvYFfHgWWFCslM4 NlN5g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=fJjIl32UmTltJC9fVJhvE6h818ssvtOeWzkd5uvoL 7E=; b=rIrrXIersuZLLnqRLeUCBYe3ltZukSDi7NE/cSgGL8aVEBUcTAxCA/oAY KBBxI9UhVlYe2Q0C/KNKuA7LXCFEkaf7El82nnGFekp/PgRcs8D3OhpAonvpm4Es D4GHSl4sFcvmwUbBf/H0kbnbZgtWaUeccGSau4zM9jOCHJTh3wEFN5Yi7YC/D1f4 3xtfWfaOqdIsnCyJ0EpE5cr17GYp4z1efwccWgYps5luxU1YZasQH166nyBx7c48 5fWS+QN/R+lVmfB7qhFh/5+QZy3QJYF6cpniAsd3HFoxLASM1o3joLj6dJw88RfP LL5Vty/6bXOuuPZQof2LkJsvQjBbw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddrkedugdefgecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesthekre dttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhmqeenucffohhmrghinhepphhoshhtghhrvghsqh hlrdhorhhgnecukfhppeejuddrvdduvddrudehtddrvddtvdenucfrrghrrghmpehmrghi lhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlh hushhtvghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id B1920380075; Wed, 24 Jul 2019 20:48:45 -0400 (EDT) Subject: Re: Rounding numbers from copy expert To: John Abraham Cc: psycopg@postgresql.org References: <8DC745DF-DDF9-4697-BAAF-23ABE78A1EB9@hbaspecto.com> <046E2360-462C-4B55-8034-160E4612E493@hbaspecto.com> From: Adrian Klaver Message-ID: <07979616-0e6d-411f-9cb4-5c7b9fd8c1bf@aklaver.com> Date: Wed, 24 Jul 2019 17:48:44 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: <046E2360-462C-4B55-8034-160E4612E493@hbaspecto.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 7/24/19 5:35 PM, John Abraham wrote: > >> On Jul 24, 2019, at 4:33 PM, Adrian Klaver wrote: >> >>> On 7/24/19 4:28 PM, John Abraham wrote: >>> Hmm, I have two machines connecting to the same database running the same query inside copy_expert. >>> Both seem to have psycopg2 2.7.5 (based on pip3 freeze). >>> One seems to be rounding the double precision number 696538951.985381 to 6.96538944E8 whereas the other machine isn't rounding it. >> >> First: >> >> https://www.postgresql.org/docs/11/datatype-numeric.html#DATATYPE-FLOAT >> >> 8.1.3. Floating-Point Types >> "The data types real and double precision are inexact, variable-precision numeric types. ... >> >> Inexact means that some values cannot be converted exactly to the internal format and are stored as approximations, so that storing and retrieving a value might show slight discrepancies. Managing these errors and how they propagate through calculations is the subject of an entire branch of mathematics and computer science and will not be discussed here, except for the following points: ..." > > > One of my proudest moments in my very first job in the late 80s was solving in just a few minutes a lack-of-precision problem my boss was fighting with for more than a day because he was subtracting two similar floating point numbers. I’m enjoying the irony of fighting a similar problem now, myself, 30 years later. > > Anyway, in this case it’s not the precision, it’s the repeatability. If you run the same program twice on two computers with the same software, you *should* get *exactly* the same answer. > >> >> Second: >> >> Where are you actually 'looking' at the values? > > I’m seeing the difference in a .CSV file produced by copy_expert, which as far as I can tell just streams the stdout from a database ‘copy to stdout’ command to a text file. I’m trying to figure out why the ‘copy to stdout’ SQL command would give different interpretations of a number. Not sure that it makes a difference, but in your OP you said you where doing: "... why copy_expert (which basically does a 'copy to stdin' query) ..." So how similar/dissimilar are the two computers you are doing this on?: 1) OS and version? 2) Machine architecture? 3) Python version and build? > >> >> >>> This is causing auditability/repeatability problems, as the difference is actually significant in this context. >>> Is there some thing about the way psycopg2 connects to the database, or the way psycopg2 is configured, that could be causing this rounding? Is there some way to express a preference for scientific notation? >>> I'm at a complete loss as to why copy_expert (which basically does a 'copy to stdin' query) would be different when called from different machines. My next step in the madness is to see if the locale has an effect, one is set to en_CA.UTF-8 and the other is set to en_US.UTF-8. But, perhaps someone else has a better idea. >>> Thanks for any help. >>> -- >>> John Abraham >>> jea@hbaspecto.com >>> 403-232-1060 >> >> >> -- >> Adrian Klaver >> adrian.klaver@aklaver.com > > > — > John Abraham > -- Adrian Klaver adrian.klaver@aklaver.com