function value = r8_nth_root ( x, n )

%*****************************************************************************80
%
%% R8_NTH_ROOT returns the nth-root of an R8.
%
%  Discussion:
%
%    The nth root of X is x^(1/n)
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    03 August 2016
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real X, the number whose Nth root is desired.
%
%    integer N, the index of the root.
%
%  Output:
%
%    real VALUE, the Nth root of X.
%

%
%  Potential Error 1: 0^0
%  But we will use it as 1.
%
  if ( x == 0.0 && n == 0 )
%   value = NaN ( 1, 1 );
    value = 1.0;
    return
%   fprintf ( 1, '\n' );
%   fprintf ( 1, 'R8_NTH_ROOT - Fatal error!\n' );
%   fprinff ( 1, '  Ambiguous case 0^0\n' );
%   error ( 'R8_NTH_ROOT - Fatal error!' );
  end
%
%  Error 2: 0^(negative power)
%
  if ( x == 0.0 && n < 0 )
    value = NaN ( 1, 1 );
    return
%   fprintf ( 1, '\n' );
%   fprintf ( 1, 'R8_NTH_ROOT - Fatal error!\n' );
%   fprinff ( 1, '  0^(negative power)\n' );
%   error ( 'R8_NTH_ROOT - Fatal error!' );
  end
%
%  Error 3: (negative)^(even strictly positive root)
%
  if ( x < 0.0 && mod ( n, 2 ) == 0 && 0 < n )
    value = NaN ( 1, 1 );
    return
%   fprintf ( 1, '\n' );
%   fprintf ( 1, 'R8_NTH_ROOT - Fatal error!\n' );
%   fprinff ( 1, '  (negative)^(even root)\n' );
%   error ( 'R8_NTH_ROOT - Fatal error!' );
  end
%
%  X^0 = 1
%
  if ( n == 0 )

    value = 1.0;
%
%  X^1 = X
%
  elseif ( n == 1 )

    value = x;
%
%  X^(-1) = 1/X
%
  elseif ( n == -1 )

    value = 1.0 ./ x;

  else
  
    e = 1.0 / abs ( n );

    if ( 0.0 < x )
      value = x .^ e;
    elseif ( x == 0.0 )
      value = 0.0;
    else
      value = - ( abs ( x ) ) .^ e;
    end

    if ( n < 0 )
      value = 1.0 ./ value;
    end

  end

  return
end
