Re: Greatest Common Divisor

From: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
To: Vik Fearing <vik(dot)fearing(at)2ndquadrant(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Stephen Frost <sfrost(at)snowman(dot)net>, Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(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-04 09:34:10
Message-ID: alpine.DEB.2.21.2001041003090.31333@pseudo
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


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.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Dean Rasheed 2020-01-04 09:37:09 Re: Greatest Common Divisor
Previous Message Vik Fearing 2020-01-04 09:30:55 Re: Greatest Common Divisor