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 1sQSnO-009xgz-4T for pgsql-hackers@arkaria.postgresql.org; Sun, 07 Jul 2024 14:23:18 +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 1sQSnM-00CdAa-Mf for pgsql-hackers@arkaria.postgresql.org; Sun, 07 Jul 2024 14:23:16 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sQSnL-00CdAS-7D for pgsql-hackers@lists.postgresql.org; Sun, 07 Jul 2024 14:23:16 +0000 Received: from fout3-smtp.messagingengine.com ([103.168.172.146]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sQSnI-000rid-6x for pgsql-hackers@postgresql.org; Sun, 07 Jul 2024 14:23:14 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfout.nyi.internal (Postfix) with ESMTP id 2F9BD1380462; Sun, 7 Jul 2024 10:23:10 -0400 (EDT) Received: from imap48 ([10.202.2.98]) by compute6.internal (MEProxy); Sun, 07 Jul 2024 10:23:10 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=compiler.org; h= cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1720362190; x=1720448590; bh=ztjTj9Pi1o Dy7EoNwpET/zy8YgT59K4rSFL3s1n9a00=; b=bW6XHUtrUIAHa8SxMTamEaxn0s xx/697GOgxu0Oi9slP+zSrYVhrQuksMWYr8JJ1zPRp8IYqHTm611Nv5t01np433W KOFpZZWefvKstb/rG7IPhSgWVD/sbMEfut1vF9GCGCmWXjHIXJZ9Lo4dUU+Oiypm DYwXz1LnKef6usrjCSLKj4Hs5+MIBecafAPQbS1/cDVEDbhGWb8vRxxyl+uM3yrF DwQxzyR/Lm+egw8aFCQYGDmvD779vwNkjEN1z4MeECWc87DRJDFKvXidJ9k4ifnD JcUuQHqwSx6zKmx8/XewoSWCtQk9pwVyp8tb3dpOhEOqnPF6IImsqOPK9NtQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1720362190; x=1720448590; bh=ztjTj9Pi1oDy7EoNwpET/zy8YgT5 9K4rSFL3s1n9a00=; b=GyJHpSdCAmBRgll/Ar9YfX6yIc+igLGF/LrOLgbwfK34 ziYnQPErjxcGgsLDNkeleHtIjtJRxn77Aj9JLHJ2mLsaa64HIcbQ84OaYovdSlNb A+5dlhs+ZpWn1nE8MmcuLtHmNuBcnO19kyJiw2e9kjBa/7V3uFokk7XrBOEmj9WY S2OLQ5p1LWJRrf7CLS2ZQCGFoPEja3N8aIdonwdsC+7BQ9eOnLzJRxQ5S5/naGtC Nbah39jqRZ8hBoudJKGLtlAHJh/gAPGRu2PV66YMEY07TZFVYrlOz7oszS/ZyD9Q r3mQjvw6uJ9RkREaDeXiznTrdjWK4gEkw/frxN2d1g== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdehgdejjecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefofgggkfgjfhffhffvufgtsehttdertderredtnecuhfhrohhmpedflfhovghl ucflrggtohgsshhonhdfuceojhhovghlsegtohhmphhilhgvrhdrohhrgheqnecuggftrf grthhtvghrnhepkeehhedufffhvdeuhfeihfdvjeejueduhffgudehgeevieetkeevvdej vedvudelnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh epjhhovghlsegtohhmphhilhgvrhdrohhrgh X-ME-Proxy: Feedback-ID: ic6394509:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id C3A5331A0065; Sun, 7 Jul 2024 10:23:09 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-566-g3812ddbbc-fm-20240627.001-g3812ddbb MIME-Version: 1.0 Message-Id: In-Reply-To: References: Date: Sun, 07 Jul 2024 16:22:48 +0200 From: "Joel Jacobson" To: "Dean Rasheed" , pgsql-hackers Subject: Re: Incorrect results from numeric round() and trunc() Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sun, Jul 7, 2024, at 13:28, Dean Rasheed wrote: > The numeric round() and trunc() functions clamp the scale argument to > the range between +/- NUMERIC_MAX_RESULT_SCALE, which is +/- 2000. > That's a long way short of the actual allowed range of type numeric, > so they produce incorrect results when rounding/truncating more than > 2000 digits before or after the decimal point. For example, > round(1e-5000, 5000) returns 0 instead of 1e-5000. > > Attached is a patch fixing that, using the actual documented range of > type numeric. > > I've also tidied up a bit by replacing all instances of SHRT_MAX with > a new constant NUMERIC_WEIGHT_MAX, whose name more accurately > describes the limit, as used in various other overflow checks. > > In doing so, I also noticed a comment in power_var() which claimed > that ln_dweight could be as low as -SHRT_MAX (-32767), which is wrong. > It can only be as small as -NUMERIC_DSCALE_MAX (-16383), though that > doesn't affect the point being made in that comment. > > I'd like to treat this as a bug-fix and back-patch it, since the > current behaviour is clearly broken. Fix seems straightforward to me. I agree it should be back-patched. Regards, Joel