Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gDxdd-00026I-Ur for pgsql-general@arkaria.postgresql.org; Sat, 20 Oct 2018 20:14:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gDxdc-0008Hj-51 for pgsql-general@arkaria.postgresql.org; Sat, 20 Oct 2018 20:14:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gDxdb-0008HJ-PO for pgsql-general@lists.postgresql.org; Sat, 20 Oct 2018 20:14:19 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gDxdT-00064U-JL for pgsql-general@lists.postgresql.org; Sat, 20 Oct 2018 20:14:17 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id DE28E20C0; Sat, 20 Oct 2018 22:14:08 +0200 (CEST) Date: Sat, 20 Oct 2018 22:14:08 +0200 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: convert real to numeric. Message-ID: <20181020201408.rawxnmcjci6xj3yu@hjp.at> References: <23387.1539872140@sss.pgh.pa.us> <20181018211328.jg373dpwc3dczhyo@hjp.at> <30660.1539903493@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="7o3zpoz2oubfj3vm" Content-Disposition: inline In-Reply-To: <30660.1539903493@sss.pgh.pa.us> User-Agent: NeoMutt/20170113 (1.7.2) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --7o3zpoz2oubfj3vm Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2018-10-18 18:58:13 -0400, Tom Lane wrote: > "Peter J. Holzer" writes: > > On 2018-10-18 10:15:40 -0400, Tom Lane wrote: > >> You could ju-jitsu the system into duplicating that behavior by casting > >> to text (which invokes float4out) and then to numeric: >=20 > > I suggest casting first to float8 and then to numeric. The conversion > > from float4 to float8 is exact, and any rounding error introduced by the > > float8->numeric conversion is certainly much smaller than the > > uncertainty of the original float4 value. >=20 > I do not think that the OP will care for the results of that. The problem > is that now the output function will think that the result is worth > printing to 16 digits, and the last ten or so of those will be garbage. You are thinking about "printing" (i.e., presentation to a user), I am thinking about "converting" (i.e. storing the value as accurately as possible in a different presentation). These are different things, You should only think about "printing" when you actually print a value. Depending on the inherent accuracy of the value and the purpose of the display the best way to display the float4 value 17637.740234375 may be '17637.740234375' (the exact value), '17637.74' (the minimum number of decimal digits necessary to reconstruct the exact value), '17638' (we don't care about fractions), '17,700' (three digits ought to be enough for anybody), '17.7k' (the same, but more compact) or anything between. It is for the application programmer to decide how to display a value, because the programmer knows what it means, where it comes from and what the user is supposed to do with that information. The runtime environment doesn't know this. So it shouldn't throw away accuracy. Often even parts of the application don't know this. So they shouldn't either. > As an example, even though the cited value happens to work nicely: >=20 > regression=3D# select '17637.75'::float4::float8; > float8 =20 > ---------- > 17637.75 > (1 row) >=20 > nearby ones don't: >=20 > regression=3D# select '17637.74'::float4::float8; > float8 =20 > ----------------- > 17637.740234375 > (1 row) >=20 > Yeah, in some sense that's a valid representation of the stored float4, It is indeed the precise value which is stored (9030523 / 512). > but it likely has little to do with the originally presented value. It is much closer to the original value than 17637.7 (the relative errors are about 1.33e-8 and 2.27e-6, so the error introduced by PostgreSQLs default rounding is 170 times greater). I think using FLT_DIG and DBL_DIG for converting from binary to decimal is wrong. They represent the "number of decimal digits, q, such that any floating-point number with q decimal digits can be rounded into a floating-point number with p radix b digits and back again without change to the q decimal digits" (ISO/IEC 9899:2011), which is not the same as the number of decimal digits sufficient to convert a binary to decimal and back again. Unfortunately, the latter is not a constant and converting binary to decimal isn't trivial - nevertheless the problem has been solved (at least for IEEE-754 arithmetic), so I might take a stab at it (even with my limited numerical skills). hp --=20 _ | Peter J. Holzer | we build much bigger, better disasters now |_|_) | | because we have much more sophisticated | | | hjp@hjp.at | management tools. __/ | http://www.hjp.at/ | -- Ross Anderson --7o3zpoz2oubfj3vm Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAlvLjIwACgkQ8g5IURL+ KF1ErA/6AzAOtEy+Jtd15eBZESN/xovTZPMDbGWT23Eamg2FrrBJGLVX4hW5jUHw 3cPeDuu7pMLot/ddG3PMb2xzWMlprwSptQUhS7C0S6Vu/SPlbk0+Zendb+/TpXfm qHTdL2soRlXbDIJyCYHVAd9jq0KeFFkzb778RDNuVVV6tIuYag1opMveALtrZlqd xiiA6UhyisG4WAyb3C5g7uI9sZzdtkvSRvcjcSOHvagI3oqFi+GhfeYyw0CWcNAE YBOQnnoiyGm2YWqO9gdNcMILxkLYDxPS5/qmHEJsgXcRPWdXLP8ZC/64hXtJY0RD ZQBq88lkp+4zGgu8yQsZbrnfRnUevutvB2qITgPU0a7hvHuwrR9wt5BgEYugLwUz EDCS+pjZPI+JHZee9eKhG19Yd3jSB3DzCH5OAhhINoQjoP/gPXbvW0UeAzpLOOY5 IM5KWQYLPuDJpQbnAZ++ohXUXDfINyeThAIzovaO3MFYXrFTeQLDptIGMNx/Ne+z wrCUsXmWo69b1iTuYs53AncNx2heaYqe0fKjiv+Ve7J5+a5W6VaMaSlmJp2orh3F 3RlN/hnJHmf2/nSF4Gctj3qHbW8IXynbKX1zd6GZ+Sbkrxv+dGXE6qX0QJ3o8U/h ffxWZUs4/uoegm5AI8fN09KrHdcxsKvkJRNQuq5OSnnF1el3wJ8= =v9g3 -----END PGP SIGNATURE----- --7o3zpoz2oubfj3vm--