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 1sOuXG-001w6k-Ka for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Jul 2024 07:36:15 +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 1sOuXE-006hvW-3e for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Jul 2024 07:36:12 +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 1sOuXC-006hvN-Of for pgsql-hackers@lists.postgresql.org; Wed, 03 Jul 2024 07:36:12 +0000 Received: from fout4-smtp.messagingengine.com ([103.168.172.147]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sOuX8-000BMm-32 for pgsql-hackers@postgresql.org; Wed, 03 Jul 2024 07:36:10 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfout.nyi.internal (Postfix) with ESMTP id 1DE49138057E for ; Wed, 3 Jul 2024 03:36:03 -0400 (EDT) Received: from imap48 ([10.202.2.98]) by compute6.internal (MEProxy); Wed, 03 Jul 2024 03:36:03 -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 :message-id:mime-version:reply-to:subject:subject:to:to; s=fm2; t=1719992163; x=1720078563; bh=u+bVYIttgCDJe+jXp5LTW89zysyYunUi V9IziR7LNHM=; b=NulYmCaBMXJ8/gKy7ntfxcD6/lxWHlMIVMKa3YkUflQjtc+E eg2dPS7Fc22Yv7UtZKX1BJ9H/SJ0F5Rr/mn0dbROnGh4p3iOmeS+kMa+GhNQIcFb qfz2N+65CT/hyiHBVFnjuDvSXHUx22cHs1jhVmO8CwaT5peJg6cypGSwnr1+sUIU 9/Mqx4RMObW0liKcaieAqeMkvxVpyzmqnMLpepj1pURVkHqsGCsljqQm7enonMAU cTa5ZRaR3d/XjJmxpo0HrF0ToxBjE8HdSzdXRqf+gEmicUDDd8HhZ9NxdgVM1ZCW 5a12KjlmSepSEs3zrz31aBMd93xH0KZLhJvCPw== 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:message-id :mime-version:reply-to:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1719992163; x=1720078563; bh=u+bVYIttgCDJe+jXp5LTW89zysyYunUiV9I ziR7LNHM=; b=ed+2yFC/BRFjPiJChypehNOiAcE52gPPGd7dIiLZNttfemcY+YW x2OZFF9TKWEKpoqtcyu4SkVaykqe6Xgu3YUwk4jCT7IJu4uzojFY8yOu/rFOIQPR n1xAzy4HTQPQBPb6K4xAgWm4nHno0wkdvNkmPV4N49XV1HLINDCqoHucPnowq1L4 xyaglv9CLBAcuQtjRjeeBk02A4YT2IYExQcWaFGndGBnc2Y9gOdZ4XeZyVSaB0gg ccwYqq3flZl+X5PXgitnie/+GamRoaJUPkx2BrxWAlxlcf+3KUuO9bOxyMWzRok2 kM/NUWqo3q6FQOZAAyw6nHnLM/rYVk1EpSg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudeigdduvddtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfffhffvufgtsehttdertd erredtnecuhfhrohhmpedflfhovghlucflrggtohgsshhonhdfuceojhhovghlsegtohhm phhilhgvrhdrohhrgheqnecuggftrfgrthhtvghrnhepudehledtheffjeekgfdujedtge ehfeeugfethffhjeejtddvjeffueekveegfeeunecuvehluhhsthgvrhfuihiivgeptden ucfrrghrrghmpehmrghilhhfrhhomhepjhhovghlsegtohhmphhilhgvrhdrohhrgh X-ME-Proxy: Feedback-ID: ic6394509:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 72F7131A0065; Wed, 3 Jul 2024 03:36:02 -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: <9c0e3469-13e6-41c4-86ec-4f3f01c66e47@app.fastmail.com> Date: Wed, 03 Jul 2024 09:35:07 +0200 From: "Joel Jacobson" To: pgsql-hackers Subject: numeric.c: Should MUL_GUARD_DIGITS be increased from 2 to 3? Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello hackers, I have discovered a peculiar behavior in mul_var() when it is called with rscale=0, but the input variables have many decimal digits, resulting in a product with a .5 decimal part. Given that no decimals are requested by the caller, I would expect the result to be rounded up. However, it is instead truncated or rounded down. To investigate this further, I added an SQL-callable function, numeric_mul_patched(), which takes rscale_adjustment as a third input parameter. This allows calling mul_var() with varying rscale values. Here is an example demonstrating the issue: SELECT 5.51574061794 * 0.99715659165; 5.5000571150105152442010 -- exact result SELECT numeric_mul_patched(5.51574061794,0.99715659165,-22); The output debug information before and after modifying MUL_GUARD_DIGITS from 2 to 3 is as follows: -#define MUL_GUARD_DIGITS 2 +#define MUL_GUARD_DIGITS 3 -make_result(): NUMERIC w=0 d=11 POS 0005 5157 4061 7940 +make_result(): NUMERIC w=0 d=11 POS 0005 5157 4061 7940 -make_result(): NUMERIC w=-1 d=11 POS 9971 5659 1650 +make_result(): NUMERIC w=-1 d=11 POS 9971 5659 1650 -before round_var: VAR w=1 d=0 POS 0000 0005 4999 8742 +before round_var: VAR w=1 d=0 POS 0000 0005 5000 5710 3944 -after round_var: VAR w=1 d=0 POS 0000 0005 +after round_var: VAR w=1 d=0 POS 0000 0006 -make_result(): NUMERIC w=0 d=0 POS 0005 +make_result(): NUMERIC w=0 d=0 POS 0006 numeric_mul_patched --------------------- - 5 + 6 (1 row) As shown above, changing MUL_GUARD_DIGITS from 2 to 3 corrects the rounding error, ensuring the correct result of 6 instead of 5. Although this is likely only a potential issue as current callers of mul_var() don't use it in this specific way, it would be beneficial to fix it for correctness and ensure mul_var() adheres to its contract. I encountered this issue while working on an optimization of mul_var() in a different thread. Initially, I thought there was an error in my code due to the different result, but I later realized it was a rounding error in the original code. Not submitting a patch yet, since I might have misunderstood something here. Maybe this is all fine after all. Guidance appreciated. Regards, Joel