function g = gcd2 ( a, b )

%
%  Check input.
%  Lunatic MATLAB function isinteger() doesn't do what you think!
%  Lunatic MATLAB function int() doesn't do what you think!
%
  if ( a ~= round ( a ) )
    error ( 'gcd2(): input a is not an integer!' )
  end

  if ( a == 0 )
    error ( 'gcd2(): input a is zero!' )
  end

  if ( b ~= round ( b ) )
    error ( 'gcd2(): input b is not an integer!' )
  end

  if ( b == 0 )
    error ( 'gcd2(): input b is zero!' )
  end
%
%  Clean up input.
%
  a = abs ( a );
  b = abs ( b );

  c = max ( a, b );
  d = min ( a, b );
%
%  Replace larger of (a,b) by a mod b.
%
  while ( true )

    if ( c < d )
      d = mod ( d, c );
      if ( d == 0 )
        g = c;
        break;
      end
    else
      c = mod ( c, d );
      if ( c == 0 )
        g = d;
        break;
      end
    end

  end

  return
end

