bug-sather
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

extended_gcd for INTI


From: K.Kodama
Subject: extended_gcd for INTI
Date: Mon, 29 Jan 2001 08:12:20 +0900

"extended_gcd" in INTI have a problem.

----- Sather version -----
$ sacomp -version
ICSI Sather compiler version: 1.2b
Default platform: unix
Installed platforms:
        unix
Home directory: /usr/local/lib/Sather
There is no class named MAIN.
-------------------

On INTI::extended_gcd, 
if a=0 or b=0 then failed for  a.extended_gcd(b, out f1, out f2);
And, code of:
     self_factor := c1/(abs*c.abs);
      i_factor := c1/(abs*c.abs);
may be error.

I think following code is better.
        ----
        extended_gcd(o:SAME,out f1: SAME, out f2:SAME):SAME is
                -- gcd = self*f1 + o*f2
                a:SAME:=self.abs; b:SAME:=o.abs; q:SAME;
                x:SAME:=#(1); y:SAME:=#(0); u:SAME:=#(0); v:SAME:=#(1);
                loop
                        if b.is_zero then f1:=x; f2:=y; return a; end;
                        q:=a/b; a:=a-q*b; x:=x-q*u; y:=y-q*v;
                        if a.is_zero then f1:=u; f2:=v; return b; end;
                        q:=b/a; b:=b-q*a; u:=u-q*x; v:=v-q*y;
                end;
        end;
        ----

And, I prefer following code for gcd, because simple and little faster.
        ----
        gcd(o:SAME):SAME is
                a:SAME:=self.abs; b:SAME:=o.abs;
                loop
                        if b.is_zero then return a; end;
                        a:=a%b;
                        if a.is_zero then return b; end;
                        b:=b%a;
                end;
        end;
        ----
-- 
K.Kodama(address@hidden)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]