function [ d, p ] = r8_to_digits_sexagesimal ( r, n )

%*****************************************************************************80
%
%% r8_to_digits_sexagesimal() computes N sexagesimal digits representing R.
%
%  Discussion:
%
%    R = d(1) * 60^p + d(2) * 60^(p-1) + ... + d(n) * 60^(p+1-n) approximately
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    22 June 2022
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real R, the number to be represented.
%
%    integer N: the number of digits to compute.
%
%  Output:
%
%    integer D(N): the sexagesimal digits.
%
%    integer P: the power of 60 multiplying the first digit.
%
  d = zeros ( n, 1 );
  p = 0;

  if ( r == 0.0 )
    return
  end
%
%  Try to deal with negative input.
%
  if ( r < 0.0 )
    sgn = -1.0;
    r = - r;
  else
    sgn = +1.0;
  end
%
%  Determine P such that 60^P <= R < 60^(P+1)
%
  rcopy = r;
  base = 60.0;
  while ( base <= rcopy )
    rcopy = rcopy / base;
    p = p + 1;
  end

  while ( rcopy < 1.0 )
    rcopy = rcopy * base;
    p = p - 1;
  end
%
%  Now read off N digits.
%
  rcopy = r;
  div = base^p;
  for i = 1 : n
    d(i) = floor ( rcopy / div );
    rcopy = mod ( rcopy, div );
    div = div / base;
  end
%
%  Multiply by sgn.
%
  d = sgn * d;

  return
end

