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 1oj1So-0002Cs-PO for pgsql-general@arkaria.postgresql.org; Thu, 13 Oct 2022 16:53:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oj1Sn-0000Pb-EE for pgsql-general@arkaria.postgresql.org; Thu, 13 Oct 2022 16:53:41 +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 1oj1Sn-0000KL-2S for pgsql-general@lists.postgresql.org; Thu, 13 Oct 2022 16:53:41 +0000 Received: from mout-u-107.mailbox.org ([2001:67c:2050:101:465::107]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oj1Sk-0007hy-Gn for pgsql-general@postgresql.org; Thu, 13 Oct 2022 16:53:40 +0000 Received: from smtp202.mailbox.org (smtp202.mailbox.org [10.196.197.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-107.mailbox.org (Postfix) with ESMTPS id 4MpFvr1fLvz9sSn; Thu, 13 Oct 2022 18:53:28 +0200 (CEST) Date: Thu, 13 Oct 2022 18:53:27 +0200 (CEST) From: Erik Wienhold To: Adrian Klaver , pgsql-general Message-ID: <603008316.190107.1665680007674@office.mailbox.org> In-Reply-To: <12a40226-70ac-3a3b-3d3a-fdaf9e32d312@aklaver.com> References: <12a40226-70ac-3a3b-3d3a-fdaf9e32d312@aklaver.com> Subject: Re: Exponentiation confusion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Priority: 3 Importance: Normal List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 13/10/2022 18:20 CEST Adrian Klaver wrote: > > In trying to answer an SO question I ran across this: > > Postgres version 14.5 > > select 10^(-1 * 18); > ?column? > ---------- > 1e-18 > > select 10^(-1 * 18::numeric); > ?column? > -------------------- > 0.0000000000000000 > > > Same for power: > > select power(10, -18); > power > ------- > 1e-18 > (1 row) > > select power(10, -18::numeric); > power > -------------------- > 0.0000000000000000 > > > Why is the cast throwing off the result? power has two overloads: https://www.postgresql.org/docs/14/functions-math.html#id-1.5.8.9.6.2.2.19.1.1.1 Calling power(numeric, numeric) is what I expect in that case instead of downcasting the exponent argument to double precision, thus losing precision. select pg_typeof(power(10, -18)), pg_typeof(power(10, -18::numeric)); pg_typeof | pg_typeof ------------------+----------- double precision | numeric (1 row) Determining the right function is described in https://www.postgresql.org/docs/14/typeconv-func.html -- Erik