Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oj1sb-0004N4-4O for pgsql-general@arkaria.postgresql.org; Thu, 13 Oct 2022 17:20:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oj1sZ-0001e9-Pv for pgsql-general@arkaria.postgresql.org; Thu, 13 Oct 2022 17:20:19 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oj1pU-0003bL-8W for pgsql-general@lists.postgresql.org; Thu, 13 Oct 2022 17:17:08 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oj1pR-0007su-O2 for pgsql-general@postgresql.org; Thu, 13 Oct 2022 17:17:07 +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 29DHGww3979361; Thu, 13 Oct 2022 13:16:58 -0400 From: Tom Lane To: Erik Wienhold cc: Dean Rasheed , Adrian Klaver , pgsql-general Subject: Re: Exponentiation confusion In-reply-to: <603008316.190107.1665680007674@office.mailbox.org> References: <12a40226-70ac-3a3b-3d3a-fdaf9e32d312@aklaver.com> <603008316.190107.1665680007674@office.mailbox.org> Comments: In-reply-to Erik Wienhold message dated "Thu, 13 Oct 2022 18:53:27 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <979359.1665681418.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 13 Oct 2022 13:16:58 -0400 Message-ID: <979360.1665681418@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Erik Wienhold writes: > On 13/10/2022 18:20 CEST Adrian Klaver wrote= : >> select power(10, -18::numeric); >> power >> -------------------- >> 0.0000000000000000 >> = >> Why is the cast throwing off the result? > Calling power(numeric, numeric) is what I expect in that case instead of > downcasting the exponent argument to double precision, thus losing preci= sion. An inexact result isn't surprising, but it shouldn't be *that* inexact. It looks to me like numeric.c's power_var_int() code path is setting the result rscale without considering the possibility that the result will have negative weight (i.e. be less than one). The main code path in power_var() does adjust for that, so for example regression=3D# select power(10, -18.00000001::numeric); power = ------------------------------------- 0.000000000000000000999999976974149 (1 row) but with an exact-integer exponent, not so much --- you just get 16 digits which isn't enough. I'm inclined to think that we should push the responsibility for choosing its rscale into power_var_int(), because internally that already does estimate the result weight, so with a little code re-ordering we won't need duplicative estimates. Don't have time to work on that right now though ... Dean, are you interested in fixing this? regards, tom lane