Mathematics7 min readExamples run on PHP 8.5.10

cosh()

Learn how PHP 8.4 cosh() computes the hyperbolic cosine of a number, with its signature, return values, edge cases, examples and common mistakes.

Signature

cosh(float $num): float
Available since PHP 4.1.07.48.08.18.28.38.48.5All versions →

Purpose

cosh() returns the hyperbolic cosine of a number. It is the hyperbolic counterpart of the ordinary cosine, but instead of describing points on a circle it describes points on a hyperbola. In terms of the exponential function, the value is (exp($num) + exp(-$num)) / 2.

You will meet this function in physics and engineering formulas, such as the shape of a hanging cable (a catenary), in some statistics and machine-learning formulas, and in general numeric code that already uses sinh() and tanh().

Signature

Parameters and defaults

  • $num: the value to process. It is a float and has no default, so you must always supply it. Unlike cos(), the argument is not an angle in radians. It is simply the input to the exponential formula above.

If you pass an integer, PHP converts it to a float. Without declare(strict_types=1), a numeric string such as "2" is coerced as well. A non-numeric string such as "abc" throws a TypeError.

Return value and failure cases

The function always returns a float. It does not return false and does not throw for ordinary numeric input. A few properties are useful to remember:

  • cosh(0) is exactly 1, and this is the minimum of the function. For any real input the result is at least 1.
  • The function is symmetric: cosh(-$x) equals cosh($x) mathematically.
  • The result grows very quickly. For large magnitudes, such as 1000, the result is too big for a float and becomes INF.
  • Passing NAN yields NAN, since there is no meaningful numeric answer.

Example 1: the basic values

The first example shows the minimum value at zero and a typical value at one. Rounding keeps the displayed output stable.

Example
<?php
echo cosh(0), "\n";
echo round(cosh(1), 6), "\n";
Output
1
1.543081

Example 2: checking the definition

The manual defines the function with exp(). Computing both forms and rounding them to the same precision shows they agree. Compare floats after rounding rather than with ===, because tiny differences in the last binary digits are normal in floating-point math.

Example
<?php
$x = 2;
$builtin = cosh($x);
$formula = (exp($x) + exp(-$x)) / 2;

echo json_encode([
    'cosh' => round($builtin, 8),
    'formula' => round($formula, 8),
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
Output
{"cosh":3.76219569,"formula":3.76219569}

Example 3: overflow to infinity

Because the function grows exponentially, large inputs overflow. You can detect this with is_infinite() before using the result in further calculations.

Example
<?php
$result = cosh(1000);
var_dump(is_infinite($result));
var_dump($result > 0);
Output
bool(true)
bool(true)

Edge cases

  • Overflow: inputs with a large absolute value produce INF. Infinity then spreads through later arithmetic, so a final answer of INF or NAN often points back to an earlier cosh() call.
  • Even function: negative and positive inputs of the same size give the same result, so the sign of the input is lost. If you need the sign, use sinh() instead.
  • Precision: the result is a binary floating-point number. Use round() for display, and use a tolerance when comparing two computed values.
  • Near zero: for tiny inputs the result is very close to 1, so the difference from 1 may be hard to see in a float.

Common mistakes

  1. Treating it as cosine. cos() works with angles and returns values between -1 and 1. cosh() never returns less than 1 for real input.
  2. Passing degrees. Hyperbolic functions do not take degrees or radians. Converting with deg2rad() first is almost always wrong.
  3. Comparing floats exactly. Comparing cosh($x) to a hand-written formula with === can fail because of rounding. Round both sides or compare the difference with a small tolerance.
  4. Ignoring overflow. If user input can be large, check for INF instead of assuming the result is finite.
  5. Expecting an inverse. To go from a hyperbolic cosine back to the argument, use acosh(). Note that cosh() is not one-to-one over all reals, so the inverse returns only the non-negative argument.
  • cos(): the ordinary cosine of an angle in radians.
  • acosh(): the inverse hyperbolic cosine.
  • sinh(): the hyperbolic sine.
  • tanh(): the hyperbolic tangent.
  • exp(): the exponential function used in the definition.

Recap

cosh(float $num): float returns (exp($num) + exp(-$num)) / 2. The result is always a float, never below 1 for real inputs, symmetric around zero, and it overflows to INF for large magnitudes. Round results for display, compare floats with care, and do not confuse it with cos().

Tested examples

Basic values at 0 and 1
<?php
echo cosh(0), "\n";
echo round(cosh(1), 6), "\n";
Output
1
1.543081
Comparing cosh() with its exp() definition
<?php
$x = 2;
$builtin = cosh($x);
$formula = (exp($x) + exp(-$x)) / 2;

echo json_encode([
    'cosh' => round($builtin, 8),
    'formula' => round($formula, 8),
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
Output
{"cosh":3.76219569,"formula":3.76219569}
Overflow to infinity for large input
<?php
$result = cosh(1000);
var_dump(is_infinite($result));
var_dump($result > 0);
Output
bool(true)
bool(true)