Re: Greatest Common Divisor

From: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
To: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
Cc: Vik Fearing <vik(dot)fearing(at)2ndquadrant(dot)com>, Chapman Flack <chap(at)anastigmatix(dot)net>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Greatest Common Divisor
Date: 2020-01-03 08:43:40
Message-ID: alpine.DEB.2.21.2001030929470.13581@pseudo
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


> Normally gcd() returns a positive integer, and gcd(a,0) = gcd(a,a) =
> abs(a). But since abs(INT_MIN) cannot be represented as a 32-bit
> integer, both those cases should throw an integer-out-of-range error.

I'm also in favor of that option, rather than sending a negative result as
a result.

About lcm(a, b): a / gcd(a, b) * b, at least if a & b are positive. If
not, some thoughts are needed:-)

Returning a NUMERIC as suggested by Tom would solve the overflow problem
by sending it back to the user who has to cast. This looks ok to me.

Maybe we could provide "int4 lcm(int2, int2)", "int8 lcm(int4, int4)", as
ISTM that there cannot be overflows on those (eg for the later: lcm <=
a*b, a & b are 31 non-signed bits, 62 bits are needed, 63 are available).

--
Fabien.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Khandekar 2020-01-03 08:50:09 Re: logical decoding : exceeded maxAllocatedDescs for .spill files
Previous Message Ibrar Ahmed 2020-01-03 08:29:23 Re: Commit fest manager for 2020-01