C++ std::exp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

In C++, std::exp(x) calculates e to the power of x. The number e is about 2.71828. It appears in growth and decay: money with continuous interest, a cooling cup of tea, and a virus that spreads.

exp() and std::log() are opposites: log(exp(x)) gives back x. The function grows very fast, so a large x quickly becomes too big for a double and the result is inf.

std::exp() is declared in <cmath> and is part of the group Exponential and logarithm. Exponentials and logarithms undo each other. A logarithm of zero is -inf, and a logarithm of a negative number is NaN.

Syntax

Include <cmath> and call std::exp. Start with these lines.

C++
double a = std::exp(1.0);    // double version
float  b = std::exp(1.0f);   // float version
CallWhat it does
std::exp(1.0)Passes a double and returns a double. Here the result is 2.71828.
std::exp(1.0f)Passes a float, so the float overload runs and returns a float.
std::exp(0)An integer is converted to double. Returns 1 as a double.

Write std::exp so the C++ overloads for float, double, and long double are used.

Library declarations
C++
float       exp(float x);
double      exp(double x);
long double exp(long double x);
double      exp(Integer x);   // for integer types

Parameters

ParameterDescription
xRequired. The exponent. It can be positive, negative, or zero. An integer is converted to double.

Return Value

Return ValueDescription
exThe exponential of x, always greater than zero.
infThe result is too big for the type (about x > 709.78 for a double). This is an overflow.
0The result is too small to store, such as exp(-800). This is an underflow.

C++ Examples

Example 1 — Common Values of exp()

Print e to a few powers, including zero and a negative power.

C++
#include <cmath>
#include <iostream>

int main() {
    double values[] = {0.0, 1.0, 2.0, -1.0};

    for (double v : values) {
        std::cout << "exp(" << v << ") = " << std::exp(v) << '\n';
    }
    return 0;
}

How It Works

  1. The loop sends each exponent in the array to std::exp().
  2. exp(0) is 1, because anything to the power of zero is one. exp(1) is the number e itself, 2.71828.
  3. exp(2) is e * e, or 7.38906. A negative exponent gives a fraction: exp(-1) is 1 / e, or 0.367879.

Example 2 — Continuous Growth Over Time

A population of 100 grows at a rate of 0.05 per year. The formula is start * exp(rate * time).

C++
#include <cmath>
#include <iomanip>
#include <iostream>

int main() {
    double start = 100.0;
    double rate = 0.05;

    std::cout << std::fixed << std::setprecision(2);
    for (int t : {0, 10, 20}) {
        std::cout << "Year " << t << ": " << start * std::exp(rate * t) << '\n';
    }
    return 0;
}

How It Works

  1. The exponent is rate * t. In year 0 it is 0, and exp(0) is 1, so the population is still 100.
  2. In year 10 the exponent is 0.5. exp(0.5) is about 1.6487, so the population is 164.87.
  3. In year 20 the exponent is 1, so the population is exactly 100 * e, or 271.83. std::fixed keeps two decimals.

Example 3 — Overflow and Underflow

Find out what happens when the exponent is too big or too small for a double.

C++
#include <cmath>
#include <iostream>

int main() {
    std::cout << "exp(709)  = " << std::exp(709.0) << '\n';

    double big = std::exp(710.0);
    std::cout << "exp(710) is inf: " << std::isinf(big) << '\n';
    std::cout << "exp(-800) = " << std::exp(-800.0) << '\n';
    return 0;
}

How It Works

  1. exp(709) is the biggest power of e that fits in a double. It prints as 8.21841e+307.
  2. One step further, exp(710) is too big and becomes inf. std::isinf() prints 1, and the program does not crash.
  3. A very negative exponent gives a number too tiny to store, so exp(-800) becomes 0.

Frequently Asked Questions

std::exp(x) returns e raised to the power x. The number e is about 2.71828, so exp(1.0) is 2.71828 and exp(0.0) is 1.
e to the power x as float, double, or long double. A very large x gives inf, and a very negative x gives 0.
The number e is about 2.718281828. There is no named constant before C++20, but std::exp(1.0) gives it. In C++20 you can use std::numbers::e from <numbers>.
exp(x) is e to the power x. pow(a, b) is a to the power b, for any base. exp(x) is faster and more exact than pow(2.718281828, x).
Include <cmath> and write std::exp(). That always works and picks the C++ overloads. The global name ::exp often works too, but the standard does not promise it, and it may only take a double.

Next: exp2()

Keep going through the <cmath> functions. Two raised to a power.

exp2() tutorial →

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.

13 people found this page helpful