The tanh() function computes the hyperbolic tangent of a real number. Built from exponentials—(ex − e−x) / (ex + e−x)—it passes through zero at x = 0 and smoothly saturates toward ±1 as |x| grows.
01
Hyperbolic
Not circular.
02
math.h
Link with -lm.
03
Any real x
Full domain.
04
tanh(0)=0
Through origin.
05
(−1, 1)
Bounded out.
06
vs tan
Different.
Fundamentals
Definition and Usage
Hyperbolic tangent is defined as tanh(x) = (ex − e−x) / (ex + e−x), equivalently sinh(x) / cosh(x). Unlike circular tan(), which is unbounded and takes radians, tanh(x) accepts any real input and always returns a value strictly between −1 and 1.
The input x is any real number—not a degree or radian angle in the circular sense. Think of it as a hyperbolic parameter. At x = 0, numerator and denominator cancel to give tanh(0) = 0.
💡
Beginner Tip
Do not confuse tanh() with tan(). tan needs radians and can shoot to infinity. tanh takes any real number and gently levels off near ±1. The inverse is atanh(y) for |y| < 1.
When x = 0, numerator and denominator are both 2, so tanh(0) = 2/2 = 0. Both tanh and circular tan are zero here—but only here at x = 0 do they share that value by coincidence.
📈 Practical Patterns
Symmetry, sinh/cosh ratio, and comparison with tan.
Example 3 — Odd Function: tanh(−x) = −tanh(x)
Negative inputs flip the sign—like sinh, unlike cosh.
Hyperbolic tangent is the ratio of hyperbolic sine and cosine. In production code, prefer tanh() directly—it handles edge cases and precision better than dividing two large values manually.
Example 5 — tanh() vs tan() at the Same Input
See why these are different functions despite similar names.
C
#include <math.h>
#include <stdio.h>
int main(void) {
double vals[] = { 0.0, 0.5, 1.0, 1.5 };
size_t i;
printf(" x tanh(x) tan(x)\n");
for (i = 0; i < 4; i++) {
double x = vals[i];
printf("%4.1f %8.4f %8.4f\n", x, tanh(x), tan(x));
}
return 0;
}
They only agree at x = 0. For other inputs, tan grows without bound while tanh stays inside (−1, 1). Note: tan expects radians; tanh does not use angle units at all.
Applications
🚀 Common Use Cases
Machine learning — activation functions and bounded outputs in neural networks.
Statistics — Fisher’s z-transformation and logistic-related formulas.
Signal processing — soft limiting and smooth saturation curves.
Physics — rapidity in special relativity (hyperbolic angle).
Pair with atanh() — invert tanh for values strictly between −1 and 1.
🧠 How tanh() Works
1
Receive real x
Any finite real number (positive, negative, or zero).
Input
2
Compute exponentials
Evaluate ex and e−x, form the ratio (ex − e−x) / (ex + e−x).
Compute
3
Return tanh(x)
Result is a double strictly between −1 and 1.
Output
=
🔢
Hyperbolic tangent
Use atanh(y) to reverse for |y| < 1.
Important
📝 Notes
Defined as (ex − e−x) / (ex + e−x) or sinh(x) / cosh(x).
Domain: all real numbers.
Range: (−1, 1)—never exactly ±1 for finite x.
tanh(0) = 0; odd function: tanh(-x) == -tanh(x).
Not the same as tan()—no degree/radian conversion needed.
As |x| grows, result approaches ±1 asymptotically.
Performance
⚡ Optimization
tanh() is implemented in the platform math library with careful handling of large |x| (where the result is already near ±1). Avoid rewriting it as sinh(x) / cosh(x) in hot loops—direct tanh is faster and more numerically stable. Use tanhf when float precision is sufficient to save memory bandwidth.
Wrap Up
Conclusion
tanh() computes hyperbolic tangent from exponentials, accepts any real input, and returns values strictly between −1 and 1. It is distinct from circular tan() and pairs with atanh() as its inverse.
Continue with trunc() for truncation toward zero, or review sinh() and cosh() for the partner hyperbolic functions.
Pair with sinh() and cosh() for hyperbolic identities
❌ Don’t
Confuse tanh() with tan()
Convert degrees/radians before calling tanh()
Expect output to reach exactly ±1 for finite inputs
Assume the input is an “angle in radians” in the trig sense
Divide sinh/cosh manually in performance-critical code without profiling
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about tanh()
Hyperbolic tangent in C, explained simply.
5
Core concepts
🔢01
Hyperbolic
Not tan().
Basics
📚02
tanh(0)=0
Origin.
Anchor
📈03
(−1, 1)
Bounded.
Range
📄04
sinh/cosh
Ratio form.
Identity
🔄05
Inverse
atanh(y).
Pair
❓ Frequently Asked Questions
tanh(x) returns the hyperbolic tangent of x, computed as (e^x - e^(-x)) / (e^x + e^(-x)). For example, tanh(0) is 0.0 and tanh(0.5) is about 0.462. Output always lies strictly between -1 and 1.
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double tanh(double x).
Neither in the usual trig sense. tanh(x) takes any real number x as a hyperbolic argument. You do not convert degrees to radians before calling tanh() the way you do for tan().
The result is always between -1 and 1 (open interval). tanh(0) is exactly 0. As x grows large and positive, tanh(x) approaches 1; as x grows large and negative, it approaches -1.
tan() is circular tangent (input in radians, unbounded output with asymptotes). tanh() is hyperbolic (any real input, output bounded in (-1, 1)). They share a similar name but solve different math problems.
tanhf(x) takes float and returns float. tanhl(x) takes long double and returns long double. Use the variant matching your variable type.
Did you know?
The logistic sigmoid σ(x) = 1 / (1 + e−x) can be rewritten as (1 + tanh(x/2)) / 2. Many neural-network libraries use tanh directly as an activation because it maps any real input to a smooth, bounded (−1, 1) output.