Re: Greatest Common Divisor

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: Vik Fearing <vik(dot)fearing(at)2ndquadrant(dot)com>
Cc: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Stephen Frost <sfrost(at)snowman(dot)net>, 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 19:08:54
Message-ID: CAEZATCU=wvc1hvseoUmB2CDE540Ct=qcOUe57ggfKNhqK042HQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sat, 4 Jan 2020 at 17:55, Vik Fearing <vik(dot)fearing(at)2ndquadrant(dot)com> wrote:
> On 04/01/2020 10:37, Dean Rasheed wrote:
> >
> > BTW, there is actually no need to restrict the inputs to integral
> > values because GCD is something that has a perfectly natural extension
> > to floating point inputs (see for example [1]). Moreover, since we
> > already have a mod(numeric, numeric) that works for arbitrary inputs,
> > Euclid's algorithm just works.
> > [...]
> > If it were more work to support non-integer inputs, I'd say that it's
> > not worth the effort, but since it's actually less work to just allow
> > it, then why not?
>
>
> Okay, I allow that now, but I've still left it for lcm. I can't find
> anything anywhere that defines lcm for floating point (I do find it for
> fractions) and the result of abs(a*b)/gcd(a,b) certainly doesn't match
> "lowest" in the examples I tried.
>

Here's another article on the subject:
https://www.math-only-math.com/hcf-and-lcm-of-decimals.html

It works because gcd(a*10^n, b*10^n) = gcd(a, b)*10^n, and therefore
lcm(a*10^n, b*10^n) = lcm(a, b)*10^n, so the results will just have
their decimal points shifted. For example:

gcd(54, 24) = 6
lcm(54, 24) = 216 = 4*54 = 9*24

gcd(5.4, 2.4) = 0.6
lcm(5.4, 2.4) = 21.6 = 4*5.4 = 9*2.4

that is the lowest common integer multiple of the two decimal inputs.

Regards,
Dean

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Justin Pryzby 2020-01-04 19:34:16 Re: allow disabling indexscans without disabling bitmapscans
Previous Message Tomas Vondra 2020-01-04 19:04:25 Re: Commit fest manager for 2020-01