On Sat, 4 Jan 2020 at 17:55, Vik Fearing <vik.fear...@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