Signature
cosh(float $num): floatPurpose
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. Unlikecos(), 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 exactly1, and this is the minimum of the function. For any real input the result is at least1.- The function is symmetric:
cosh(-$x)equalscosh($x)mathematically. - The result grows very quickly. For large magnitudes, such as
1000, the result is too big for a float and becomesINF. - Passing
NANyieldsNAN, 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.
echo cosh(0), "\n";
echo round(cosh(1), 6), "\n";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.
$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);{"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.
$result = cosh(1000);
var_dump(is_infinite($result));
var_dump($result > 0);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 ofINForNANoften points back to an earliercosh()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 from1may be hard to see in a float.
Common mistakes
- Treating it as cosine.
cos()works with angles and returns values between-1and1.cosh()never returns less than1for real input. - Passing degrees. Hyperbolic functions do not take degrees or radians. Converting with
deg2rad()first is almost always wrong. - 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. - Ignoring overflow. If user input can be large, check for
INFinstead of assuming the result is finite. - Expecting an inverse. To go from a hyperbolic cosine back to the argument, use
acosh(). Note thatcosh()is not one-to-one over all reals, so the inverse returns only the non-negative argument.
Related functions
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
echo cosh(0), "\n";
echo round(cosh(1), 6), "\n";
1 1.543081
$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);
{"cosh":3.76219569,"formula":3.76219569}
$result = cosh(1000);
var_dump(is_infinite($result));
var_dump($result > 0);
bool(true) bool(true)