function  c = cos_taylor ( x )

%*****************************************************************************80
%
%% cos_taylor approximates cosine(x) using a power series.
%
%  Discussion:
%
%    Around x = 40.0, the approximation breaks down.
%
%  Modified:
%
%    03 September 2019
%
%  Author:
%
%    Original MATLAB version, for sin(x), by Cleve Moler.
%    Modifications by John Burkardt.
%
%  Reference:
%
%    Cleve Moler,
%    Numerical Computing with MATLAB,
%    SIAM, 2004,
%    ISBN13: 978-0-898716-60-3,
%    LC: QA297.M625,
%    ebook: http://www.mathworks.com/moler/chapters.html
%
%  Input:
%
%    real x, the argument.
%
%  Output:
%
%    real c, the cos(x), approximated using a power series.
%
  n = 0;
  c = 0.0;

  while ( true )

    if ( n == 0 )
      t = 1.0;
    else
      t = - x.^2 / n / ( n - 1 ) .* t;
    end
%
%  When x is a vector, all the entries have to satisfy this test.
%
    if ( ( c + t ) == c )
      break
    end
%   fprintf ( 1, '  %d  %26.16g  %26.16g\n', n, c, t );
    c = c + t;
 
    n = n + 2;

  end

  return
end

