Re: Greatest Common Divisor
Fabien COELHO <coelho@cri.ensmp.fr>
From: Fabien COELHO <coelho@cri.ensmp.fr>
To: Vik Fearing <vik.fearing@2ndquadrant.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>, Stephen Frost <sfrost@snowman.net>,
Dean Rasheed <dean.a.rasheed@gmail.com>,
Chapman Flack <chap@anastigmatix.net>,
PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2020-01-04T08:35:43Z
Lists: pgsql-hackers
Bonjour Vik, >> The point of swapping is to a void possibly expensive modulo, but this >> should be done on absolute values, otherwise it may not achieve its >> purpose as stated by the comment? > > Ah, true. How widespread are these architectures that need this special > treatment? Is it really worth handling? Dunno. AFAICR it was with sparc architectures 25 years ago. Also I do not like much relying on the subtleties of C99 % wrt negative numbers to have the algorithm work, I'd be much at ease to deal with sign and special values at the beginning of the function and proceed with positive numbers afterwards. >> I'm unsure about gcd(INT_MIN, 0) should error. Possibly 0 would be nicer? > > > What justification for that do you have? ISTM that the current implementation has: \forall int4 n, n \neq MIN_INT4, \gcd(n, 0) = 0 ? In which case applying the same rule for min int seems ok. -- Fabien Coelho - CRI, MINES ParisTech
Commits
-
Add functions gcd() and lcm() for integer and numeric types.
- 13661ddd7eae 13.0 landed