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 1ilT3O-00060w-GV for pgsql-hackers@arkaria.postgresql.org; Sun, 29 Dec 2019 07:31:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ilT3N-00009a-3S for pgsql-hackers@arkaria.postgresql.org; Sun, 29 Dec 2019 07:31:57 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ilT3M-00009T-Mw for pgsql-hackers@lists.postgresql.org; Sun, 29 Dec 2019 07:31:56 +0000 Received: from courriel.mines-paristech.fr ([77.158.173.149] helo=antispam-1.ensmp.fr) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ilT3F-0004d6-Bz for pgsql-hackers@lists.postgresql.org; Sun, 29 Dec 2019 07:31:55 +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 6CDAE57CC1; Sun, 29 Dec 2019 08:30:28 +0100 (CET) Received: from pc54.home (agrenoble-653-1-42-9.w92-129.abo.wanadoo.fr [92.129.130.9]) (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 3585D20143; Sun, 29 Dec 2019 08:30:28 +0100 (CET) Date: Sun, 29 Dec 2019 08:30:26 +0100 (CET) From: Fabien COELHO X-X-Sender: fabien@pseudo To: Vik Fearing cc: PostgreSQL Hackers Subject: Re: Greatest Common Divisor In-Reply-To: Message-ID: References: User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="2056645051-1661852082-1577604628=:889" X-CLAMAV-SCAN: ok X-VRSPAM-SCORE: -100 X-VRSPAM-STATE: legit X-VRSPAM-CAUSE: gggruggvucftvghtrhhoucdtuddrgedufedrvdefvddgtdekucetufdoteggodetrfcurfhrohhfihhlvgemucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpeffhffvufgjkfhffgggtgesmhdtreertderjeenucfhrhhomhephfgrsghivghnucevqffgnffjqfcuoegtohgvlhhhohestghrihdrvghnshhmphdrfhhrqeenucfkphepledvrdduvdelrddufedtrdelnecurfgrrhgrmhepmhhouggvpehsmhhtphhouhht X-VRSPAM-EXTCAUSE: mhhouggvpehsmhhtphhouhht List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --2056645051-1661852082-1577604628=:889 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8BIT Bonjour Vik, >> Should there be a NUMERIC version as well? I'd say maybe yes. > > I thought about that, too, but also decided against it for this patch. Hmmm. ISTM that int functions are available for numeric? >> I'm wondering what it should do on N, 0 and 0, 0. Raise an error? >> Return 0? Return 1? return N? There should be some logic and comments >> explaining it. > > Well, gcd(N, 0) is N, and gcd(0, 0) is 0, so I don't see an issue here? I think that there should be a comment. >> I'd test with INT_MIN and INT_MAX. > > Okay, I'll add tests for those, instead of the pretty much random values > I have now. > >> C modulo operator (%) is a pain because it is not positive remainder >> (2 % -3 == -1 vs 2 % 3 == 2, AFAICR). > > This does not seem to be the case... Indeed, I tested quickly with python, but it has yet another behavior as shown above, what a laugh! So with C: 2 % -3 == 2, -2 % 3 == -2 Note that AFAICS there is no integer i so that 3 * i - (-2) == -2. >> It does not seem that fixing the sign afterwards is the right thing to >> do. I'd rather turn both arguments positive before doing the descent. > > Why isn't it the right thing to do? Because I do not trust C modulo as I had a lot of problems with it? :-) If it works, but it should deserve a clear comment explaining why. >> Which raises an issue with INT_MIN by the way, which has no positive:-( > > That's an argument against abs-ing the input values.  It's only an issue > with gcd(INT_MIN, INT_MIN) which currently returns INT_MIN. That should be an error instead, because -INT_MIN cannot be represented? > Any other value with INT_MIN will be 1 or something lower than INT_MAX. Looks ok. >> Also, the usual approach is to exchange args so that the largest is >> first, because there may be a software emulation if the hardware does >> not implement modulo. At least it was the case with some sparc >> processors 25 years ago:-) > > The args will exchange themselves. Yep, but after a possibly expensive software-emulated modulo operation? -- Fabien. --2056645051-1661852082-1577604628=:889--