function value = r4_cin ( x )

%*****************************************************************************80
%
%% R4_CIN evaluates the alternate cosine integral Cin of an R4 argument.
%
%  Discussion:
%
%    CIN(X) = gamma + log(X)
%      + integral ( 0 <= T <= X ) ( cos ( T ) - 1 ) / T  dT
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    28 September 2011
%
%  Author:
%
%    Original FORTRAN77 version by Wayne Fullerton.
%    MATLAB version by John Burkardt.
%
%  Reference:
%
%    Wayne Fullerton,
%    Portable Special Function Routines,
%    in Portability of Numerical Software,
%    edited by Wayne Cowell,
%    Lecture Notes in Computer Science, Volume 57,
%    Springer 1977,
%    ISBN: 978-3-540-08446-4,
%    LC: QA297.W65.
%
%  Parameters:
%
%    Input, real X, the argument.
%
%    Output, real VALUE, the cosine integral Cin evaluated at X.
%
  persistent cincs
  persistent eul
  persistent ncin
  persistent xmin

  eul = 0.57721566490153286;

  if ( isempty ( ncin ) )

    cincs = [ ...
      0.3707450175090968874, ...
     -0.0589357489636444683, ...
      0.0053818964211356912, ...
     -0.0002986005284196214, ...
      0.0000109557257532162, ...
     -0.0000002840545487735, ...
      0.0000000054697399488, ...
     -0.0000000000812418746, ...
      0.0000000000009586859, ...
     -0.0000000000000092027, ...
      0.0000000000000000733 ]';

    ncin = r4_inits ( cincs, 11, 0.1 * r4_mach ( 3 ) );
    xmin = sqrt ( r4_mach ( 1 ) );

  end

  absx = abs ( x );

  if ( absx <= xmin )

    value = 0.0;

  elseif ( x <= 4.0 )

    value = x * x * r4_csevl ( ( x * x - 8.0 ) * 0.125, cincs, ncin );

  else

    [ f, g ] = r4_sifg ( x );
    sinx = sin ( absx );
    value = - f * sinx + g * cos ( absx ) + log ( absx ) + eul;

  end

  return
end
