Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1infp7-0007mR-3F for pgsql-hackers@arkaria.postgresql.org; Sat, 04 Jan 2020 09:34:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1infp5-0006K9-Sc for pgsql-hackers@arkaria.postgresql.org; Sat, 04 Jan 2020 09:34:19 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1infp5-0006Ju-Ji for pgsql-hackers@lists.postgresql.org; Sat, 04 Jan 2020 09:34:19 +0000 Received: from courriel.mines-paristech.fr ([77.158.173.149] helo=antispam-1.ensmp.fr) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1infoy-0003nu-TF for pgsql-hackers@lists.postgresql.org; Sat, 04 Jan 2020 09:34:18 +0000 Received: from smtp-5.mines-paristech.fr (smtp.mines-paristech.fr [77.158.173.146]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by antispam-1.ensmp.fr (antispam.mines-paristech.fr) with ESMTPS id BD72255EB4; Sat, 4 Jan 2020 10:34:11 +0100 (CET) Received: from pseudo (149.64.203.77.rev.sfr.net [77.203.64.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp-5.mines-paristech.fr (Postfix) with ESMTPSA id 4BB0A2005E; Sat, 4 Jan 2020 10:34:11 +0100 (CET) Date: Sat, 4 Jan 2020 10:34:10 +0100 (CET) From: Fabien COELHO X-X-Sender: fabien@pseudo To: Vik Fearing cc: Tom Lane , Stephen Frost , Dean Rasheed , Chapman Flack , PostgreSQL Hackers Subject: Re: Greatest Common Divisor In-Reply-To: <32b8af34-8f87-c30f-5555-2325bd140a59@2ndquadrant.com> Message-ID: References: <5E08D947.9000902@anastigmatix.net> <20200102145712.GN3195@tamriel.snowman.net> <16242.1577977961@sss.pgh.pa.us> <6855727c-3836-21cc-18f9-e79227b99079@2ndquadrant.com> <32b8af34-8f87-c30f-5555-2325bd140a59@2ndquadrant.com> User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed X-CLAMAV-SCAN: ok X-VRSPAM-SCORE: -100 X-VRSPAM-STATE: legit X-VRSPAM-CAUSE: gggruggvucftvghtrhhoucdtuddrgedufedrvdeghedgtdeiucetufdoteggodetrfcurfhrohhfihhlvgemucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpeffhffvufgjkfhffgggtgesthdtredttdervdenucfhrhhomhephfgrsghivghnucevqffgnffjqfcuoegtohgvlhhhohestghrihdrvghnshhmphdrfhhrqeenucfkphepjeejrddvtdefrdeigedrudegleenucfrrghrrghmpehmohguvgepshhmthhpohhuth X-VRSPAM-EXTCAUSE: mhhouggvpehsmhhtphhouhht List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Bonjour Vik, > Here is an updated patch fixing that. As I said, welcome to arithmetic:-) Patch v5 applies cleanly. Doc: I'd consider using an example the result of which is 42 instead of 21, for obvious geek motivations. Possibly gcd(2142, 462) should be ok. I got it wrong with my previous comment on gcd(int_min, 0). I'm okay with erroring on int_min. Code comments: gcd(n, 0) = abs(n), not n? About the code. Add unlikely() where appropriate. I'd deal with int_min and 0 at the beginning and then proceed with absoluting the values, rather than the dance around a1/arg1, a2/arg2, and other arg2 = -arg2, and arg1 = -arg1 anyway in various places, which does not make the code that easy to understand. Pseudo code could be: if ((a1 == min && (a2 == min || a2 == 0)) || (a2 == min && a1 == 0)) error; a1 = abs(a1), a2 = abs(a2); euclids; return; Note: in the numeric code you abs the value, ISTM consistent to do it as well in the other implementations. Would it make sense that NAN is returned on NUMERIC when the computation cannot be performed, eg on non integer values? Why the positive constraint on LCM(NUMERIC, NUMERIC)? Why not absoluting? Tests: you can make LCM fail on much smaller values for int2/4/8, you do not need to start around max_int. -- Fabien.