Re: Greatest Common Divisor
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Vik Fearing <vik.fearing@2ndquadrant.com>
Cc: Fabien COELHO <coelho@cri.ensmp.fr>, 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-04T00:34:04Z
Lists: pgsql-hackers
Vik Fearing <vik.fearing@2ndquadrant.com> writes: > On 04/01/2020 01:21, Tom Lane wrote: >> Zero is the "correct" answer for that, isn't it, independently of overflow >> considerations? > I would say not. Oh, right, I was misremembering the identity gcd(a,0) as being 0 not a. Never mind that then. > The correct answer is INT_MIN but we've decided a > negative result is not desirable. Agreed. On the other hand, we could stave off overflow the same way we discussed for lcm: make it return int8. We're still stuck with the special case for INT64_MIN in gcd64 of course, so maybe that's just inconsistent rather than being worthwhile. [ thinks for a bit... ] In practice, I imagine few people use gcd on negative values, so doing weird things with the datatype choices is probably not better than throwing an error for this case. regards, tom lane
Commits
-
Add functions gcd() and lcm() for integer and numeric types.
- 13661ddd7eae 13.0 landed