Re: Greatest Common Divisor - Mailing list pgsql-hackers

From Dean Rasheed
Subject Re: Greatest Common Divisor
Date
Msg-id CAEZATCU=wvc1hvseoUmB2CDE540Ct=qcOUe57ggfKNhqK042HQ@mail.gmail.com
Whole thread Raw
In response to Re: Greatest Common Divisor  (Vik Fearing <vik.fearing@2ndquadrant.com>)
Responses Re: Greatest Common Divisor  (Vik Fearing <vik.fearing@2ndquadrant.com>)
List pgsql-hackers
On Sat, 4 Jan 2020 at 17:55, Vik Fearing <vik.fearing@2ndquadrant.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



pgsql-hackers by date:

Previous
From: Tomas Vondra
Date:
Subject: Re: Commit fest manager for 2020-01
Next
From: Justin Pryzby
Date:
Subject: Re: allow disabling indexscans without disabling bitmapscans