function value = toms243 ( z )

%*****************************************************************************80
%
%% TOMS243 computes the natural logarithm for complex values.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    03 January 2018
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    David Collens,
%    Algorithm 243: Logarithm of a Complex Number,
%    Communications of the Association for Computing Machinery,
%    Volume 7, Number 11, November 1964, page 660.
%
%  Parameters:
%
%    Input, complex Z, the argument of the function.
%
%    Output, complex VALUE, the value of the function.
%
  a = real ( z );
  b = imag ( z );

  if ( a == 0.0 && b == 0.0 )
    c = NaN ( );
    d = NaN ( );
  else
    e = a / 2.0;
    f = b / 2.0;
    if ( abs ( e ) < 0.5 && abs ( f ) < 0.5 )
      c = abs ( 2.0 * a ) + abs ( 2.0 * b );
      d = 8.0 * ( a / c ) * a + 8.0 * ( b / c ) * b;
      c = 0.5 * ( log ( c ) + log ( d ) ) - log ( sqrt ( 8.0 ) );
    else
      c = abs ( e / 2.0 ) + abs ( f / 2.0 );
      d = 0.5 * ( e / c ) * e + 0.5 * ( f / c ) * f;
      c = 0.5 * ( log ( c ) + log ( d ) ) + log ( sqrt ( 8.0 ) );
    end

    if ( ( a ~= 0.0 ) && abs ( f ) <= abs ( e ) )
      if ( sign ( a ) ~= -1.0 )
        d = atan ( b / a );
      elseif ( sign ( b ) ~= -1.0 )
        d = atan ( b / a ) + pi;
      else
        d = atan ( b / a ) - pi;
      end
    else
      d = - atan ( a / b ) + pi / 2.0 * sign ( b );
    end

  end

  value = c + d*i;

  return
end
