function y = cosh(xin)
%COSH	Hyperbolic cosine.
%	COSH(X) is the hyperbolic cosine of the elements of X.

%	J.M. Hammond, 6-17-92
%	Copyright (c) 1984-94 by The MathWorks, Inc.

%       21 Oct 92 (jmh) -- fixed for matrix inputs
%        2 Sep 93 (jmh) -- fixed for null matrix inputs

% Complex argument
if any(any(imag(xin))) ~= 0             % 2 Sep 93 (jmh)
    x = real(xin);
    y = imag(xin);
    y = cosh(x).*cos(y) + i*sinh(x).*sin(y);

% Real argument
else
    % Reference: W. J. Cody and W. Waite, "Software Manual
    % for the Elementary Functions", 1980, chapter 12.

    lnv  = 0.6931610107421875;     % v is close to 2, log(v) is exact
    xbar = 7.083964185322641e+02;  % log(1/realmin)
    vf   = 0.138302778796019e-04;  % v/2-1, where v is as above

    x = abs(xin);
    y = zeros(size(x));

    % Use regular formula
    k = (x <= xbar) | isnan(x);
    if any(any(k))			% 21 Oct 92 (jmh)
        y(k) = (exp(x(k)) + exp(-x(k)))/2;
    end

    % Dodge overflow problem for answers close to realmax
    k = (xbar < x);
    if any(any(k))			% 21 Oct 92 (jmh)
        x(k) = x(k) - lnv;
        y(k) = exp(x(k)) + vf .* exp(x(k));
    end
end
