C Math tanh() Function

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
<math.h>

What You’ll Learn

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.

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.

📝 Syntax

Standard C declaration:

C
double tanh(double x);

Related variants

C
float tanhf(float x);             /* <math.h> */
long double tanhl(long double x); /* <math.h> */

Parameters

  • x — any real number (hyperbolic argument).

Return Value

  • Hyperbolic tangent of x as double.
  • tanh(0) returns 0.0.
  • tanh(0.5) returns about 0.462.
  • Result is always in the open interval (−1, 1).

Headers and linking

  • #include <math.h>
  • Compile: gcc tanh.c -std=c11 -o tanh -lm

⚡ Quick Reference

CallMeaningResult (approx.)
tanh(0)Origin0.0
tanh(0.5)From reference0.462
tanh(1)Standard value0.762
tanh(-0.5)Odd function-0.462
atanh(tanh(x))Inverse pairx
Forward
tanh(x)

Hyperbolic tan

Inverse
atanh(y)

|y| < 1

Identity
sinh(x)/cosh(x)

Ratio form

Not this
tan(x)

Circular trig

Examples Gallery

Compile every example with gcc file.c -std=c11 -o out -lm. Hyperbolic tangent pairs naturally with sinh(), cosh(), and atanh().

📚 Getting Started

Compute hyperbolic tangent and print the result.

Example 1 — Basic Hyperbolic Tangent

Classic example from the reference: tanh(0.5).

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double x = 0.5;
    double result = tanh(x);

    printf("The hyperbolic tangent of %.2f is %.4f\n", x, result);

    return 0;
}

How It Works

The library evaluates (e0.5 − e−0.5) / (e0.5 + e−0.5) internally and returns about 0.462.

Example 2 — tanh(0) Equals 0

Hyperbolic tangent passes through the origin.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    printf("tanh(0) = %.1f\n", tanh(0.0));

    return 0;
}

How It Works

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.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double x = 0.5;

    printf("tanh( %.1f) = %.6f\n",  x, tanh(x));
    printf("tanh(%.1f) = %.6f\n", -x, tanh(-x));

    return 0;
}

How It Works

Swapping the sign of x swaps the sign of the result. This mirrors circular tan, which is also an odd function.

Example 4 — tanh(x) = sinh(x) / cosh(x)

Verify the ratio identity and compare with the library call.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double x = 1.0;
    double ratio = sinh(x) / cosh(x);
    double library = tanh(x);

    printf("x = %.1f\n", x);
    printf("sinh/cosh: %.6f\n", ratio);
    printf("tanh():    %.6f\n", library);

    return 0;
}

How It Works

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;
}

How It Works

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.

🚀 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.

📝 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.

⚡ 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.

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.

💡 Best Practices

✅ Do

  • Include <math.h> and link with -lm
  • Use tanh() for bounded, smooth saturation curves
  • Remember tanh(0) = 0 and output in (−1, 1)
  • Use atanh() to invert hyperbolic tangent
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about tanh()

Hyperbolic tangent in C, explained simply.

5
Core concepts
📚 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.

Explore C Math Functions

Continue with trunc() or browse the full math function reference.

Math Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful