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 1mdYhv-0003Vk-QP for pgsql-novice@arkaria.postgresql.org; Thu, 21 Oct 2021 14:06:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mdYhu-0004Nx-6A for pgsql-novice@arkaria.postgresql.org; Thu, 21 Oct 2021 14:06:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mdYht-0004Nl-Uw for pgsql-novice@lists.postgresql.org; Thu, 21 Oct 2021 14:06:09 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mdYhr-0004ux-EM for pgsql-novice@lists.postgresql.org; Thu, 21 Oct 2021 14:06:09 +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 19LE64Xr2404796; Thu, 21 Oct 2021 10:06:04 -0400 From: Tom Lane To: Nuno Sousa cc: pgsql-novice@lists.postgresql.org Subject: Re: Numeric Scale Differences In-reply-to: References: Comments: In-reply-to Nuno Sousa message dated "Thu, 21 Oct 2021 14:47:48 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2404794.1634825164.1@sss.pgh.pa.us> Date: Thu, 21 Oct 2021 10:06:04 -0400 Message-ID: <2404795.1634825164@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Nuno Sousa writes: > I've run into an interesting issue when trying to do numeric math. I > expected the following to return the same number of decimal digits in the > fractional part: > select scale(1/0.6::numeric), scale(1/6::numeric); Um ... why did you expect that? The inputs to the divisions have different scales: # select scale(0.6::numeric), scale(6::numeric); scale | scale -------+------- 1 | 0 (1 row) so I find it unsurprising that the outputs do too. Now the fact that the output scales differ by 4 not 1 is indeed an implementation artifact. It stems from the numeric type working with base-10000 digits, so that scales that are multiples of 4 are most efficient, hence division will always choose such an output scale. regards, tom lane