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 1gDFbs-0004XU-HX for pgsql-general@arkaria.postgresql.org; Thu, 18 Oct 2018 21:13:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gDFbq-00028V-VT for pgsql-general@arkaria.postgresql.org; Thu, 18 Oct 2018 21:13:34 +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_SHA384:256) (Exim 4.89) (envelope-from ) id 1gDFbq-00028N-Mh for pgsql-general@lists.postgresql.org; Thu, 18 Oct 2018 21:13:34 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gDFbm-0002Br-UN for pgsql-general@lists.postgresql.org; Thu, 18 Oct 2018 21:13:34 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id CC29420B8; Thu, 18 Oct 2018 23:13:28 +0200 (CEST) Date: Thu, 18 Oct 2018 23:13:28 +0200 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: convert real to numeric. Message-ID: <20181018211328.jg373dpwc3dczhyo@hjp.at> References: <23387.1539872140@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="oh4kgkp2l4k2w45t" Content-Disposition: inline In-Reply-To: <23387.1539872140@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 --oh4kgkp2l4k2w45t Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2018-10-18 10:15:40 -0400, Tom Lane wrote: > Alessandro Aste writes: > > I need to convert an SQL field from real to numeric, but I=E2=80=99m ge= tting a > > strange behavior. > > select amount, amount::numeric, amount::numeric(16,4), > > amount::varchar::numeric from mytable where id =3D 32560545; > > Result: > > 17637.75, 17637.8, 17637.8000, 17637.75 >=20 > You realize of course that "real" (a/k/a float4) can only be trusted > to six decimal digits in the first place. When I try this, I get 24 bits, actually. Using decimal digits when talking about binary numbers is misleading. [...] > You could ju-jitsu the system into duplicating that behavior by casting > to text (which invokes float4out) and then to numeric: >=20 > regression=3D# set extra_float_digits to 1; > SET > regression=3D# select '17637.75'::real, '17637.75'::real::numeric; > float4 | numeric=20 > ----------+--------- > 17637.75 | 17637.8 > (1 row) >=20 > regression=3D# select '17637.75'::real, '17637.75'::real::text::numeric; > float4 | numeric =20 > ----------+---------- > 17637.75 | 17637.75 > (1 row) I suggest casting first to float8 and then to numeric. The conversion =66rom 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. > I think, however, that you ought to spend some time contemplating > the fact that that extra digit is partially garbage. If we assume that 17637.75 was the result of rounding a more precise value to a float4, then the real value was somewhere between 17637.7490234375 and 17637.7509765625. Rounding to 17637.8 introduces an error almost 50 times larger. > I'm not really convinced that doing it like this rather than doing the > standard conversion is a good idea. You can't manufacture precision > where there is none It may be that the real value of that number is only known to +/- 0.1. Or maybe only to +/- 100. But postgresql can't know that, and gratuitously adding additional rounding errors doesn't help. 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 --oh4kgkp2l4k2w45t Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAlvI93QACgkQ8g5IURL+ KF1d9w//TUt2+zNZlwqAPs3mmWJ2m/aIntWx1Rq+Ij1+sbctwAzAauM9SSkyVtFO /YPtdSKk6CX/U89rL4ibCGV7VG6RfESDdodgG2EG4HdAGIYliPU3rc2+FgeKoU4L GkSZ7JdFHKSfR1vc4GC2OeLysLxszFm97WhYWxbp4Ez4L8LnLUtej45Beq1254Vf hQExhJ6yHQIaFn9czqBZqm46vbUBv4ye0XlM3API0vxlYmIUyNc/wFJGM3LJ/Rni jt/BBOHzEJGWObCxtDNbUmVT1zQS5d3dIP5r1ew1rWPHQocqY8h9/omHiTFjJRB8 EoiLbJt3wpGiUXEsL2X/BMkV/r6Y+zet9uac/Ir4d6wNZNPDu1vWNUQaQBCeZIv3 bVQBxqj6ln3xmbGvGPGHmKzerAQ6t/WHXxgp15LitkpJWv6N+Im5jNgni25cykJI h7xyxhMsAg+yj2dTjKexX1+eIuOiKCDonGFv5AJBuhB6FzOmp7xmdaiZjo/YaIz9 1trW8ArswsITDjYuu8VtM8NGl97Nfej5G/g1BonkOy80KKyd98qDMGfnAHZOlmsL 8iJm4Nm84gMobcxigcCv0oeMj9l71nD7V5hjoC9Dos9twqui7ZkGWt+rfksY1vgx 90UE7q9KQ9NYOM1oFeKPCQ4Dzg3ZYw+5ODUgsNcqOBnMLhnfGlQ= =aNV6 -----END PGP SIGNATURE----- --oh4kgkp2l4k2w45t--