cosh

Returns the hyperbolic cosine of a number, defined as (exp(x) + exp(-x)) / 2.

Signature
cosh(float $num): float
  • Added in PHP 4.1.0

Parameters

ParameterTypeDescription
$numfloatThe argument to process. A plain number, not an angle in degrees.

Return value

floatThe hyperbolic cosine of $num. Always 1.0 or greater; returns INF once the true result exceeds the float range.

Errors

Throws a TypeError if $num is not a float or a value that can be coerced to float; it never throws for large inputs, returning INF instead.

cosh() returns the hyperbolic cosine of $num, defined as (exp($num) + exp(-$num)) / 2. Unlike cos(), it is neither periodic nor bounded: it equals exactly 1 at zero and grows exponentially in both directions.

When to reach for it

The classic case is the catenary — the shape a chain, power line or suspension cable takes under its own weight, y = a * cosh(x / a). It also shows up in the analytic solution of certain differential equations, in relativistic physics (converting rapidity to a Lorentz factor), and in the log-cosh loss used as a smooth stand-in for mean squared error.

What trips people up

The argument is a plain real number, not an angle. There is no degree variant, and feeding it deg2rad() output is almost always a sign you wanted cos() instead. Because the result grows like exp($num) / 2, it overflows a 64-bit float at roughly $num = 711 and quietly returns INF rather than raising an error, so guard unbounded input with is_infinite().

cosh() is an even function: cosh(-$x) and cosh($x) are bit-for-bit identical. That makes it a quick sanity check when you suspect a sign error upstream. Its inverse is acosh(), which is only defined for arguments of 1 or more.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Model the sag of a hanging cable or power line with y = a * cosh(x / a).
  • Compute log-cosh loss as a smooth, outlier-tolerant alternative to squared error.
  • Convert a rapidity value to a Lorentz factor in relativistic physics code.

Frequently asked questions

What is the difference between cos and cosh in PHP?

cos() is the circular cosine: it takes an angle in radians and always returns a value between -1 and 1. cosh() is the hyperbolic cosine: it takes a plain number, never dips below 1, and grows exponentially in both directions.

Does cosh accept degrees?

No. cosh() takes a dimensionless real number, not an angle, so deg2rad() has no role here. If you were about to convert degrees, you almost certainly want cos() rather than cosh().

Why does cosh return INF?

Because cosh(x) grows like exp(x)/2, it exceeds the maximum 64-bit float at roughly x = 711 and returns INF instead of erroring. Test the input range up front, or check the result with is_infinite().

More examples

The fundamental hyperbolic identity

cosh(x)^2 - sinh(x)^2 is exactly 1 in mathematics. In floating point it comes out as 0.99999999999997, which is why the check uses a tolerance instead of ===. At 12 decimal places the printf still rounds to 1.000000000000, so the residual error only shows up in the tolerance check itself — subtracting two large nearly equal numbers has cost about three digits of precision.

Example · php
<?php
$x = 3.0;
$identity = cosh($x) ** 2 - sinh($x) ** 2;
printf("cosh^2 - sinh^2 = %.12f\n", $identity);
var_dump(abs($identity - 1.0) < 1e-9);

log-cosh loss for a small prediction set

log(cosh(error)) behaves like half the squared error for small errors and like the absolute error for large ones, so one bad prediction does not dominate the total. The output is 0.168502 for these four pairs.

Example · php
<?php
$actual    = [3.0, -0.5, 2.0, 7.0];
$predicted = [2.5,  0.0, 2.0, 8.0];

$sum = 0.0;
foreach ($actual as $i => $y) {
    $sum += log(cosh($predicted[$i] - $y));
}
printf("log-cosh loss: %.6f\n", $sum / count($actual));

Building a catenary table

The output is symmetric around x=0 because cosh is an even function: the value at -20 matches the value at 20 exactly. The lowest point is 0 at x=0, confirming the curve is anchored at the origin.

Example · php
<?php
$a = 20.0; // cable tension parameter
foreach ([-20, -10, 0, 10, 20] as $x) {
    printf("x=%4d  y=%8.4f\n", $x, $a * cosh($x / $a) - $a);
}

Discussion

  • Be the first to comment on this lesson.