Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!b1g2000hsg.googlegroups.com!not-for-mail
From: Greg Heath <heath@alumni.brown.edu>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Largest Common Factor
Date: Wed, 30 Apr 2008 11:04:26 -0700 (PDT)
Organization: http://groups.google.com
Lines: 33
Message-ID: <e0c79be4-6a54-45e6-87d3-b6e6610374d5@b1g2000hsg.googlegroups.com>
References: <fv8j8a$2j8$1@fred.mathworks.com> <19640430.1209534092123.JavaMail.jakarta@nitrogen.mathforum.org> 
NNTP-Posting-Host: 69.141.173.117
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
X-Trace: posting.google.com 1209578667 30545 127.0.0.1 (30 Apr 2008 18:04:27 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Wed, 30 Apr 2008 18:04:27 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: b1g2000hsg.googlegroups.com; posting-host=69.141.173.117; 
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; 
Xref: news.mathworks.com comp.soft-sys.matlab:465987


On Apr 30, 12:01 pm, Greg Heath <he...@alumni.brown.edu> wrote:
> On Apr 30, 1:41 am, Yumnam Kirani Singh <kirani.si...@gmail.com>
> wrote:
>
>> Largest common factor is defined only for integers
>> not for floating point numbers. See hep on gcd.
>
> For my purposes I can truncate FP numbers to any
> practical precision then multiply by the appropriate
> power of 10 to get integers.

Example:

a = 2*pi*sort(rand(128,1));  % realistic data
a = [12.3 20.5 32.8]             % demo data
tol = 4;

% [g h] = GCFgh(a,tol)
% function [g h] = GCFgh(a,tol)

mult = 10^tol;
b = round(mult*a);
c = b(1)*ones(size(a));
d = gcd(b,c);
g = min(d)/mult
h = a/g

a =         12.3         20.5         32.8
g =          4.1
h =            3            5            8

Greg