Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s6jWW-001RBh-T8 for pgsql-bugs@arkaria.postgresql.org; Tue, 14 May 2024 04:12:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1s6jWW-0091ZZ-Qn for pgsql-bugs@arkaria.postgresql.org; Tue, 14 May 2024 04:12:20 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s6jWW-0091YW-Ic for pgsql-bugs@lists.postgresql.org; Tue, 14 May 2024 04:12:20 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s6jWT-00089T-9l for pgsql-bugs@lists.postgresql.org; Tue, 14 May 2024 04:12:19 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 44E4CFhS915578; Tue, 14 May 2024 00:12:15 -0400 From: Tom Lane To: David Rowley cc: Huw Rogers , pgsql-bugs@lists.postgresql.org Subject: Re: numeric calculation bug as of 16.2-2 In-reply-to: References: Comments: In-reply-to David Rowley message dated "Tue, 14 May 2024 15:30:34 +1200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <915576.1715659935.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 14 May 2024 00:12:15 -0400 Message-ID: <915577.1715659935@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk David Rowley writes: > On Tue, 14 May 2024 at 14:53, Huw Rogers wrote: >> test=3D# select ('170141183460469231731687303715884105727'::numeric / '= 9223372036854775808'::numeric) * '9223372036854775808'::numeric; >> ?column? >> ----------------------------------------- >> 170141183460469231731687303715884105728 > I don't have enough experience in NUMERIC to tell if this is a bug or > not. It is not. If you think that using numeric (or any other general-purpose arithmetic code) means you'll always get exact answers for every calculation, I have a bridge in Brooklyn I'd like to sell you. The specific problem with the example you give is that you're using fractional-power-of-2 numbers and expecting them to be exactly representable in numeric's base-10 arithmetic. That's not happening. Amusingly, type float8 (which is binary at bottom) can represent such numbers exactly, so that this works: =3D# select ((2^127)/(2^63))*(2^63) =3D (2^127); ?column? = ---------- t (Use pg_typeof to verify that the subexpressions are type float8.) Nonetheless, float8 has a well-deserved reputation for being imprecise with the decimal fractions that people commonly work with. That's just the opposite side of the same coin: conversion between the two bases is inexact, unless you are willing to work with an unlimited number of fractional digits, which in practice nobody is. BTW, just as a point of order, I cannot reproduce your complaint: =3D# select ((2^127::numeric)/(2^63::numeric))*(2^63::numeric) =3D (2^127:= :numeric); ?column? = ---------- t (1 row) =3D# select (2^127::numeric), (2^63::numeric); ?column? | ?column? = -----------------------------------------+--------------------- 170141183460469231731687303715884105728 | 9223372036854775808 (1 row) =3D# select (170141183460469231731687303715884105728/9223372036854775808)*= 9223372036854775808 =3D 170141183460469231731687303715884105728; ?column? = ---------- t (1 row) I don't know where you got '170141183460469231731687303715884105727' from, but that seems off-by-one. This doesn't invalidate my larger point though. regards, tom lane